blob: 7740aa98180cdbaa6f2174a13134bfd29de975b5 [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 Ivanov6b788ee2016-02-17 16:08:03 -080052#include "linker_gdb_support.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070053#include "linker_globals.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054#include "linker_debug.h"
Dimitry Ivanov769b33f2016-07-21 11:33:40 -070055#include "linker_dlwarning.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070056#include "linker_main.h"
Dimitry Ivanovb943f302016-08-03 16:00:10 -070057#include "linker_namespaces.h"
Dmitriy Ivanov18870d32015-04-22 13:10:04 -070058#include "linker_sleb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020059#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080060#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080061#include "linker_reloc_iterators.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070062#include "linker_utils.h"
tony.ys_liub4474402015-07-29 18:00:22 +080063
Elliott Hughes939a7e02015-12-04 15:27:46 -080064#include "android-base/strings.h"
Dimitry Ivanovb996d602016-07-11 18:11:39 -070065#include "android-base/stringprintf.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000066#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080067
Elliott Hughes1801db32015-06-08 18:04:00 -070068// Override macros to use C++ style casts.
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080069#undef ELF_ST_TYPE
70#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
71
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -070072static android_namespace_t* g_anonymous_namespace = &g_default_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070073
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070074static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
75static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020076
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070077static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -070078static LinkerTypeAllocator<LinkedListEntry<android_namespace_t>> g_namespace_list_allocator;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070079
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070080#if defined(__LP64__)
Jiyong Park1daa18e2017-01-31 17:32:46 +090081static const char* const kSystemLibDir = "/system/lib64";
82static const char* const kSystemNdkLibDir = "/system/lib64/ndk";
83static const char* const kSystemVndkLibDir = "/system/lib64/vndk";
84static const char* const kSystemVndkExtLibDir = "/system/lib64/vndk-ext";
85static const char* const kVendorSpHalLibDir = "/vendor/lib64/sameprocess";
86static const char* const kVendorLibDir = "/vendor/lib64";
87static const char* const kAsanSystemLibDir = "/data/lib64";
88static const char* const kAsanSystemNdkLibDir = "/data/lib64/ndk";
89static const char* const kAsanSystemVndkLibDir = "/data/lib64/vndk";
90static const char* const kAsanSystemVndkExtLibDir = "/data/lib64/vndk-ext";
91static const char* const kAsanVendorSpHalLibDir = "/data/vendor/lib64/sameprocess";
92static const char* const kAsanVendorLibDir = "/data/vendor/lib64";
Elliott Hughes011bc0b2013-10-08 14:27:10 -070093#else
Jiyong Park1daa18e2017-01-31 17:32:46 +090094static const char* const kSystemLibDir = "/system/lib";
95static const char* const kSystemNdkLibDir = "/system/lib/ndk";
96static const char* const kSystemVndkLibDir = "/system/lib/vndk";
97static const char* const kSystemVndkExtLibDir = "/system/lib/vndk-ext";
98static const char* const kVendorSpHalLibDir = "/vendor/lib/sameprocess";
99static const char* const kVendorLibDir = "/vendor/lib";
100static const char* const kAsanSystemLibDir = "/data/lib";
101static const char* const kAsanSystemNdkLibDir = "/data/lib/ndk";
102static const char* const kAsanSystemVndkLibDir = "/data/lib/vndk";
103static const char* const kAsanSystemVndkExtLibDir = "/data/lib/vndk-ext";
104static const char* const kAsanVendorSpHalLibDir = "/data/vendor/lib/sameprocess";
105static const char* const kAsanVendorLibDir = "/data/vendor/lib";
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700106#endif
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700107
108static const char* const kDefaultLdPaths[] = {
109 kSystemLibDir,
Jiyong Park1daa18e2017-01-31 17:32:46 +0900110 kSystemNdkLibDir,
111 kSystemVndkExtLibDir,
112 kSystemVndkLibDir,
113 kVendorSpHalLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700114 kVendorLibDir,
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700115 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700116};
David Bartleybc3a5c22009-06-02 18:27:28 -0700117
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700118static const char* const kAsanDefaultLdPaths[] = {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700119 kAsanSystemLibDir,
120 kSystemLibDir,
Jiyong Park1daa18e2017-01-31 17:32:46 +0900121 kAsanSystemNdkLibDir,
122 kSystemNdkLibDir,
123 kAsanSystemVndkExtLibDir,
124 kSystemVndkExtLibDir,
125 kAsanSystemVndkLibDir,
126 kSystemVndkLibDir,
127 kAsanVendorSpHalLibDir,
128 kVendorSpHalLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700129 kAsanVendorLibDir,
130 kVendorLibDir,
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700131 nullptr
132};
133
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700134// Is ASAN enabled?
135static bool g_is_asan = false;
136
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700137static CFIShadowWriter g_cfi_shadow;
138
139CFIShadowWriter* get_cfi_shadow() {
140 return &g_cfi_shadow;
141}
142
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700143static bool is_system_library(const std::string& realpath) {
144 for (const auto& dir : g_default_namespace.get_default_library_paths()) {
145 if (file_is_in_dir(realpath, dir)) {
146 return true;
147 }
148 }
149 return false;
150}
151
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700152// Checks if the file exists and not a directory.
153static bool file_exists(const char* path) {
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700154 struct stat s;
155
156 if (stat(path, &s) != 0) {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700157 return false;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700158 }
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700159
160 return S_ISREG(s.st_mode);
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700161}
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700162
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800163static std::string resolve_soname(const std::string& name) {
164 // We assume that soname equals to basename here
165
166 // TODO(dimitry): consider having honest absolute-path -> soname resolution
167 // note that since we might end up refusing to load this library because
168 // it is not in shared libs list we need to get the soname without actually loading
169 // the library.
170 //
171 // On the other hand there are several places where we already assume that
172 // soname == basename in particular for any not-loaded library mentioned
173 // in DT_NEEDED list.
174 return basename(name.c_str());
175}
176
177static bool maybe_accessible_via_namespace_links(android_namespace_t* ns, const char* name) {
178 std::string soname = resolve_soname(name);
179 for (auto& ns_link : ns->linked_namespaces()) {
180 if (ns_link.is_accessible(soname.c_str())) {
181 return true;
182 }
183 }
184
185 return false;
186}
187
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700188// TODO(dimitry): The grey-list is a workaround for http://b/26394120 ---
189// gradually remove libraries from this list until it is gone.
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800190static bool is_greylisted(android_namespace_t* ns, const char* name, const soinfo* needed_by) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700191 static const char* const kLibraryGreyList[] = {
192 "libandroid_runtime.so",
193 "libbinder.so",
194 "libcrypto.so",
195 "libcutils.so",
196 "libexpat.so",
197 "libgui.so",
198 "libmedia.so",
199 "libnativehelper.so",
200 "libskia.so",
201 "libssl.so",
202 "libstagefright.so",
203 "libsqlite.so",
204 "libui.so",
205 "libutils.so",
206 "libvorbisidec.so",
207 nullptr
208 };
209
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800210 // If you're targeting N, you don't get the greylist.
211 if (get_application_target_sdk_version() >= __ANDROID_API_N__) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700212 return false;
213 }
214
215 // if the library needed by a system library - implicitly assume it
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800216 // is greylisted unless it is in the list of shared libraries for one or
217 // more linked namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700218 if (needed_by != nullptr && is_system_library(needed_by->get_realpath())) {
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800219 return !maybe_accessible_via_namespace_links(ns, name);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700220 }
221
222 // if this is an absolute path - make sure it points to /system/lib(64)
223 if (name[0] == '/' && dirname(name) == kSystemLibDir) {
224 // and reduce the path to basename
225 name = basename(name);
226 }
227
228 for (size_t i = 0; kLibraryGreyList[i] != nullptr; ++i) {
229 if (strcmp(name, kLibraryGreyList[i]) == 0) {
230 return true;
231 }
232 }
233
234 return false;
235}
236// END OF WORKAROUND
237
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700238static const char* const* g_default_ld_paths;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700239static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800240
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800241static bool g_anonymous_namespace_initialized;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700242
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800243#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700244struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700245 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700246};
247
248static linker_stats_t linker_stats;
249
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800250void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700251 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700252}
253#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800254void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700255}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800256#endif
257
258#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800259uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800260#endif
261
Elliott Hughesbedfe382012-08-14 14:07:59 -0700262static void notify_gdb_of_load(soinfo* info) {
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800263 if (info->is_linker() || info->is_main_executable()) {
264 // gdb already knows about the linker and the main executable.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700265 return;
266 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800267
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800268 link_map* map = &(info->link_map_head);
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000269
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800270 map->l_addr = info->load_bias;
271 // link_map l_name field is not const.
272 map->l_name = const_cast<char*>(info->get_realpath());
273 map->l_ld = info->dynamic;
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000274
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800275 CHECK(map->l_name != nullptr);
276 CHECK(map->l_name[0] != '\0');
277
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800278 notify_gdb_of_load(map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700279}
280
Elliott Hughesbedfe382012-08-14 14:07:59 -0700281static void notify_gdb_of_unload(soinfo* info) {
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800282 notify_gdb_of_unload(&(info->link_map_head));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800283}
284
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700285LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
286 return g_soinfo_links_allocator.alloc();
287}
288
289void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
290 g_soinfo_links_allocator.free(entry);
291}
292
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700293LinkedListEntry<android_namespace_t>* NamespaceListAllocator::alloc() {
294 return g_namespace_list_allocator.alloc();
295}
296
297void NamespaceListAllocator::free(LinkedListEntry<android_namespace_t>* entry) {
298 g_namespace_list_allocator.free(entry);
299}
300
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700301soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
302 struct stat* file_stat, off64_t file_offset,
303 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700304 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200305 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700306 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200307 }
308
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700309 TRACE("name %s: allocating soinfo for ns=%p", name, ns);
310
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700311 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
312 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700313
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700314 solist_add_soinfo(si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200315
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700316 si->generate_handle();
317 ns->add_soinfo(si);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700318
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700319 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200320 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800321}
322
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800323static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700324 if (si == nullptr) {
325 return;
326 }
327
328 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800329 if (!si->is_mapped_by_caller()) {
330 munmap(reinterpret_cast<void*>(si->base), si->size);
331 } else {
332 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
333 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
334 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
335 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700336 }
337
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700338 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700339
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700340 if (!solist_remove_soinfo(si)) {
341 // TODO (dimitry): revisit this - for now preserving the logic
342 // but it does not look right, abort if soinfo is not in the list instead?
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700343 return;
344 }
Elliott Hughes46882792012-08-03 16:49:39 -0700345
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700346 // clear links to/from si
347 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700348
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700349 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700350 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800351}
352
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700353static void parse_path(const char* path, const char* delimiters,
354 std::vector<std::string>* resolved_paths) {
355 std::vector<std::string> paths;
356 split_path(path, delimiters, &paths);
357 resolve_paths(paths, resolved_paths);
358}
359
Elliott Hughescade4c32012-12-20 14:42:14 -0800360static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700361 std::vector<std::string> ld_libary_paths;
362 parse_path(path, ":", &ld_libary_paths);
363 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800364}
365
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700366static bool realpath_fd(int fd, std::string* realpath) {
367 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700368 __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700369 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700370 PRINT("readlink(\"%s\") failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700371 return false;
372 }
373
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700374 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700375 return true;
376}
377
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700378#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700379
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700380// For a given PC, find the .so that it belongs to.
381// Returns the base address of the .ARM.exidx section
382// for that .so, and the number of 8-byte entries
383// in that section (via *pcount).
384//
385// Intended to be called by libc's __gnu_Unwind_Find_exidx().
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800386_Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800387 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800388
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700389 for (soinfo* si = solist_get_head(); si != 0; si = si->next) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700390 if ((addr >= si->base) && (addr < (si->base + si->size))) {
391 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800392 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800393 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700394 }
395 *pcount = 0;
396 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800397}
Elliott Hughes46882792012-08-03 16:49:39 -0700398
Christopher Ferris24053a42013-08-19 17:45:09 -0700399#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700400
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700401// Here, we only have to provide a callback to iterate across all the
402// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700403int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700404 int rv = 0;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700405 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700406 dl_phdr_info dl_info;
407 dl_info.dlpi_addr = si->link_map_head.l_addr;
408 dl_info.dlpi_name = si->link_map_head.l_name;
409 dl_info.dlpi_phdr = si->phdr;
410 dl_info.dlpi_phnum = si->phnum;
411 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
412 if (rv != 0) {
413 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800414 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700415 }
416 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800417}
Elliott Hughes46882792012-08-03 16:49:39 -0700418
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800419
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700420bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700421 soinfo** si_found_in, const soinfo_list_t& global_group,
422 const soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800423 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700424 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700425
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700426 /* "This element's presence in a shared object library alters the dynamic linker's
427 * symbol resolution algorithm for references within the library. Instead of starting
428 * a symbol search with the executable file, the dynamic linker starts from the shared
429 * object itself. If the shared object fails to supply the referenced symbol, the
430 * dynamic linker then searches the executable file and other shared objects as usual."
431 *
432 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
433 *
434 * Note that this is unlikely since static linker avoids generating
435 * relocations for -Bsymbolic linked dynamic executables.
436 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700437 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700438 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700439 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
440 return false;
441 }
442
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700443 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700444 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700445 }
446 }
447
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700448 // 1. Look for it in global_group
449 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700450 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700451 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700452 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700453 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700454 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
455 error = true;
456 return false;
457 }
458
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700459 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700460 *si_found_in = global_si;
461 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700462 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700463
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700464 return true;
465 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700466
467 if (error) {
468 return false;
469 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700470 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700471
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700472 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700473 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700474 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700475 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700476 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700477 // we already did this - skip
478 return true;
479 }
480
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700481 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700482 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700483 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
484 error = true;
485 return false;
486 }
487
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700488 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700489 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700490 return false;
491 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700492
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700493 return true;
494 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700495
496 if (error) {
497 return false;
498 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700499 }
500
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700501 if (s != nullptr) {
502 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
503 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700504 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
505 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700506 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700507 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700508
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700509 *symbol = s;
510 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700511}
512
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700513ProtectedDataGuard::ProtectedDataGuard() {
514 if (ref_count_++ == 0) {
515 protect_data(PROT_READ | PROT_WRITE);
516 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700517
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700518 if (ref_count_ == 0) { // overflow
519 __libc_fatal("Too many nested calls to dlopen()");
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800520 }
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800521}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800522
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800523ProtectedDataGuard::~ProtectedDataGuard() {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700524 if (--ref_count_ == 0) {
525 protect_data(PROT_READ);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800526 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700527}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800528
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700529void ProtectedDataGuard::protect_data(int protection) {
530 g_soinfo_allocator.protect_all(protection);
531 g_soinfo_links_allocator.protect_all(protection);
532 g_namespace_allocator.protect_all(protection);
533 g_namespace_list_allocator.protect_all(protection);
534}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800535
536size_t ProtectedDataGuard::ref_count_ = 0;
537
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700538// Each size has it's own allocator.
539template<size_t size>
540class SizeBasedAllocator {
541 public:
542 static void* alloc() {
543 return allocator_.alloc();
544 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700545
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700546 static void free(void* ptr) {
547 allocator_.free(ptr);
548 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700549
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700550 private:
551 static LinkerBlockAllocator allocator_;
552};
553
554template<size_t size>
555LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
556
557template<typename T>
558class TypeBasedAllocator {
559 public:
560 static T* alloc() {
561 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
562 }
563
564 static void free(T* ptr) {
565 SizeBasedAllocator<sizeof(T)>::free(ptr);
566 }
567};
568
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700569class LoadTask {
570 public:
571 struct deleter_t {
572 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700573 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700574 TypeBasedAllocator<LoadTask>::free(t);
575 }
576 };
577
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700578 static deleter_t deleter;
579
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800580 static LoadTask* create(const char* name,
581 soinfo* needed_by,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700582 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700583 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700584 return new (ptr) LoadTask(name, needed_by, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700585 }
586
587 const char* get_name() const {
588 return name_;
589 }
590
591 soinfo* get_needed_by() const {
592 return needed_by_;
593 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700594
595 soinfo* get_soinfo() const {
596 return si_;
597 }
598
599 void set_soinfo(soinfo* si) {
600 si_ = si;
601 }
602
603 off64_t get_file_offset() const {
604 return file_offset_;
605 }
606
607 void set_file_offset(off64_t offset) {
608 file_offset_ = offset;
609 }
610
611 int get_fd() const {
612 return fd_;
613 }
614
615 void set_fd(int fd, bool assume_ownership) {
616 fd_ = fd;
617 close_fd_ = assume_ownership;
618 }
619
620 const android_dlextinfo* get_extinfo() const {
621 return extinfo_;
622 }
623
624 void set_extinfo(const android_dlextinfo* extinfo) {
625 extinfo_ = extinfo;
626 }
627
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700628 bool is_dt_needed() const {
629 return is_dt_needed_;
630 }
631
632 void set_dt_needed(bool is_dt_needed) {
633 is_dt_needed_ = is_dt_needed;
634 }
635
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700636 const ElfReader& get_elf_reader() const {
637 CHECK(si_ != nullptr);
638 return (*elf_readers_map_)[si_];
639 }
640
641 ElfReader& get_elf_reader() {
642 CHECK(si_ != nullptr);
643 return (*elf_readers_map_)[si_];
644 }
645
646 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
647 return elf_readers_map_;
648 }
649
650 bool read(const char* realpath, off64_t file_size) {
651 ElfReader& elf_reader = get_elf_reader();
652 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
653 }
654
655 bool load() {
656 ElfReader& elf_reader = get_elf_reader();
657 if (!elf_reader.Load(extinfo_)) {
658 return false;
659 }
660
661 si_->base = elf_reader.load_start();
662 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800663 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700664 si_->load_bias = elf_reader.load_bias();
665 si_->phnum = elf_reader.phdr_count();
666 si_->phdr = elf_reader.loaded_phdr();
667
668 return true;
669 }
670
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700671 private:
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800672 LoadTask(const char* name,
673 soinfo* needed_by,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700674 std::unordered_map<const soinfo*, ElfReader>* readers_map)
675 : name_(name), needed_by_(needed_by), si_(nullptr),
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700676 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map),
677 is_dt_needed_(false) {}
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700678
679 ~LoadTask() {
680 if (fd_ != -1 && close_fd_) {
681 close(fd_);
682 }
683 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700684
685 const char* name_;
686 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700687 soinfo* si_;
688 const android_dlextinfo* extinfo_;
689 int fd_;
690 bool close_fd_;
691 off64_t file_offset_;
692 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700693 // TODO(dimitry): needed by workaround for http://b/26394120 (the grey-list)
694 bool is_dt_needed_;
695 // END OF WORKAROUND
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700696
697 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
698};
699
Ningsheng Jiane93be992014-09-16 15:22:10 +0800700LoadTask::deleter_t LoadTask::deleter;
701
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700702template <typename T>
703using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
704
705typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700706typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700707typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700708
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800709enum walk_action_result_t : uint32_t {
710 kWalkStop = 0,
711 kWalkContinue = 1,
712 kWalkSkip = 2
713};
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700714
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700715// This function walks down the tree of soinfo dependencies
716// in breadth-first order and
717// * calls action(soinfo* si) for each node, and
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800718// * terminates walk if action returns kWalkStop
719// * skips children of the node if action
720// return kWalkSkip
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700721//
722// walk_dependencies_tree returns false if walk was terminated
723// by the action and true otherwise.
724template<typename F>
725static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700726 SoinfoLinkedList visit_list;
727 SoinfoLinkedList visited;
728
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700729 for (size_t i = 0; i < root_soinfos_size; ++i) {
730 visit_list.push_back(root_soinfos[i]);
731 }
732
733 soinfo* si;
734 while ((si = visit_list.pop_front()) != nullptr) {
735 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700736 continue;
737 }
738
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800739 walk_action_result_t result = action(si);
740
741 if (result == kWalkStop) {
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700742 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700743 }
744
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700745 visited.push_back(si);
746
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800747 if (result != kWalkSkip) {
748 si->get_children().for_each([&](soinfo* child) {
749 visit_list.push_back(child);
750 });
751 }
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700752 }
753
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700754 return true;
755}
756
757
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800758static const ElfW(Sym)* dlsym_handle_lookup(android_namespace_t* ns,
759 soinfo* root,
760 soinfo* skip_until,
761 soinfo** found,
762 SymbolName& symbol_name,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800763 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700764 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700765 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700766
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700767 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
768 if (skip_lookup) {
769 skip_lookup = current_soinfo != skip_until;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800770 return kWalkContinue;
771 }
772
773 if (!ns->is_accessible(current_soinfo)) {
774 return kWalkSkip;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700775 }
776
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800777 if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700778 result = nullptr;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800779 return kWalkStop;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700780 }
781
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700782 if (result != nullptr) {
783 *found = current_soinfo;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800784 return kWalkStop;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700785 }
786
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800787 return kWalkContinue;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700788 });
789
790 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800791}
792
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800793static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
794 const char* name,
795 const version_info* vi,
796 soinfo** found,
797 soinfo* caller,
798 void* handle);
799
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700800// This is used by dlsym(3). It performs symbol lookup only within the
801// specified soinfo object and its dependencies in breadth first order.
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800802static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si,
803 soinfo** found,
804 const char* name,
805 const version_info* vi) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700806 // According to man dlopen(3) and posix docs in the case when si is handle
807 // of the main executable we need to search not only in the executable and its
808 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
809 //
810 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
811 // libraries and they are loaded in breath-first (correct) order we can just execute
812 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700813 if (si == solist_get_somain()) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800814 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700815 }
816
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700817 SymbolName symbol_name(name);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800818 // note that the namespace is not the namespace associated with caller_addr
819 // we use ns associated with root si intentionally here. Using caller_ns
820 // causes problems when user uses dlopen_ext to open a library in the separate
821 // namespace and then calls dlsym() on the handle.
822 return dlsym_handle_lookup(si->get_primary_namespace(), si, nullptr, found, symbol_name, vi);
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700823}
824
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800825/* This is used by dlsym(3) to performs a global symbol lookup. If the
826 start value is null (for RTLD_DEFAULT), the search starts at the
827 beginning of the global solist. Otherwise the search starts at the
828 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700829 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800830static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
831 const char* name,
832 const version_info* vi,
833 soinfo** found,
834 soinfo* caller,
835 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800836 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800837
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700838 auto& soinfo_list = ns->soinfo_list();
839 auto start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700840
841 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -0700842 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700843 return nullptr;
844 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700845 auto it = soinfo_list.find(caller);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700846 CHECK (it != soinfo_list.end());
847 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700848 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800849 }
850
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700851 const ElfW(Sym)* s = nullptr;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700852 for (auto it = start, end = soinfo_list.end(); it != end; ++it) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700853 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700854 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800855 // if the library is opened by application with target api level < M.
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700856 // See http://b/21565766
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800857 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 &&
858 si->get_target_sdk_version() >= __ANDROID_API_M__) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700859 continue;
860 }
861
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800862 if (!si->find_symbol_by_name(symbol_name, vi, &s)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700863 return nullptr;
864 }
865
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700866 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800867 *found = si;
868 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600869 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800870 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600871
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700872 // If not found - use dlsym_handle_lookup for caller's
873 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700874 // case we already did it.
875 if (s == nullptr && caller != nullptr &&
876 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800877 soinfo* local_group_root = caller->get_local_group_root();
878
879 return dlsym_handle_lookup(local_group_root->get_primary_namespace(),
880 local_group_root,
881 (handle == RTLD_NEXT) ? caller : nullptr,
882 found,
883 symbol_name,
884 vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700885 }
886
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700887 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700888 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
889 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800890 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800891
Elliott Hughescade4c32012-12-20 14:42:14 -0800892 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800893}
894
Kito Chengfa8c05d2013-03-12 14:58:06 +0800895soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800896 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700897 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800898 if (address >= si->base && address - si->base < si->size) {
899 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600900 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800901 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700902 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600903}
904
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700905class ZipArchiveCache {
906 public:
907 ZipArchiveCache() {}
908 ~ZipArchiveCache();
909
910 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
911 private:
912 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
913
914 std::unordered_map<std::string, ZipArchiveHandle> cache_;
915};
916
917bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
918 std::string key(zip_path);
919
920 auto it = cache_.find(key);
921 if (it != cache_.end()) {
922 *handle = it->second;
923 return true;
924 }
925
926 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
927 if (fd == -1) {
928 return false;
929 }
930
931 if (OpenArchiveFd(fd, "", handle) != 0) {
932 // invalid zip-file (?)
Yabin Cui722072d2016-03-21 17:10:12 -0700933 CloseArchive(handle);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700934 close(fd);
935 return false;
936 }
937
938 cache_[key] = *handle;
939 return true;
940}
941
942ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -0700943 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700944 CloseArchive(it.second);
945 }
946}
947
948static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700949 const char* const input_path,
950 off64_t* file_offset, std::string* realpath) {
951 std::string normalized_path;
952 if (!normalize_path(input_path, &normalized_path)) {
953 return -1;
954 }
955
956 const char* const path = normalized_path.c_str();
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700957 TRACE("Trying zip file open from path \"%s\" -> normalized \"%s\"", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +0000958
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700959 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +0000960 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700961 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +0000962 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700963 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +0000964 if (separator == nullptr) {
965 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -0700966 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000967
968 char buf[512];
969 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
970 PRINT("Warning: ignoring very long library path: %s", path);
971 return -1;
972 }
973
974 buf[separator - path] = '\0';
975
976 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700977 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +0000978 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
979 if (fd == -1) {
980 return -1;
981 }
982
983 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700984 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000985 // invalid zip-file (?)
986 close(fd);
987 return -1;
988 }
989
Simon Baldwinaef71952015-01-16 13:22:54 +0000990 ZipEntry entry;
991
Yusuke Sato56f40fb2015-06-25 14:56:07 -0700992 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000993 // Entry was not found.
994 close(fd);
995 return -1;
996 }
997
998 // Check if it is properly stored
999 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1000 close(fd);
1001 return -1;
1002 }
1003
1004 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001005
1006 if (realpath_fd(fd, realpath)) {
1007 *realpath += separator;
1008 } else {
1009 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1010 normalized_path.c_str());
1011 *realpath = normalized_path;
1012 }
1013
Simon Baldwinaef71952015-01-16 13:22:54 +00001014 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001015}
1016
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001017static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
1018 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
1019 if (n < 0 || n >= static_cast<int>(buf_size)) {
1020 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1021 return false;
1022 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001023
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001024 return true;
1025}
1026
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001027static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1028 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001029 const std::vector<std::string>& paths,
1030 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001031 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001032 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001033 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001034 continue;
1035 }
1036
1037 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001038 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001039 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001040 }
1041
1042 if (fd == -1) {
1043 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1044 if (fd != -1) {
1045 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001046 if (!realpath_fd(fd, realpath)) {
1047 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1048 *realpath = buf;
1049 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001050 }
1051 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001052
1053 if (fd != -1) {
1054 return fd;
1055 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001056 }
1057
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001058 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001059}
1060
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001061static int open_library(android_namespace_t* ns,
1062 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001063 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001064 off64_t* file_offset, std::string* realpath) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001065 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001066
Elliott Hughes124fae92012-10-31 14:20:03 -07001067 // 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 -07001068 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001069 int fd = -1;
1070
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001071 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001072 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1073 }
1074
1075 if (fd == -1) {
1076 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001077 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001078 *file_offset = 0;
1079 if (!realpath_fd(fd, realpath)) {
1080 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1081 *realpath = name;
1082 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001083 }
1084 }
1085
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001086 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001087 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001088
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001089 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1090 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 -07001091 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001092 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001093 // Check if the library is accessible
1094 if (fd != -1 && !ns->is_accessible(*realpath)) {
1095 fd = -1;
1096 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001097 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001098
Elliott Hughes124fae92012-10-31 14:20:03 -07001099 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001100 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001101 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001102
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001103 // TODO(dimitry): workaround for http://b/26394120 (the grey-list)
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001104 if (fd == -1 && ns != &g_default_namespace && is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001105 // try searching for it on default_namespace default_library_path
1106 fd = open_library_on_paths(zip_archive_cache, name, file_offset,
1107 g_default_namespace.get_default_library_paths(), realpath);
1108 }
1109 // END OF WORKAROUND
1110
Elliott Hughes124fae92012-10-31 14:20:03 -07001111 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001112}
1113
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001114const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001115#if !defined(__LP64__)
1116 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Elliott Hughes5bc78c82016-11-16 11:35:43 -08001117 if (get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001118 const char* bname = basename(dt_needed);
1119 if (bname != dt_needed) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001120 DL_WARN("library \"%s\" has invalid DT_NEEDED entry \"%s\"", sopath, dt_needed);
1121 add_dlwarning(sopath, "invalid DT_NEEDED entry", dt_needed);
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001122 }
1123
1124 return bname;
1125 }
1126#endif
1127 return dt_needed;
1128}
1129
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001130template<typename F>
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001131static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1132 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1133 if (d->d_tag == DT_NEEDED) {
1134 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1135 }
1136 }
1137}
1138
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001139static bool load_library(android_namespace_t* ns,
1140 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001141 LoadTaskList* load_tasks,
1142 int rtld_flags,
1143 const std::string& realpath) {
1144 off64_t file_offset = task->get_file_offset();
1145 const char* name = task->get_name();
1146 const android_dlextinfo* extinfo = task->get_extinfo();
1147
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001148 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001149 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001150 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001151 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001152 if (file_offset < 0) {
1153 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001154 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001155 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001156
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001157 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001158 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001159 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001160 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001161 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001162 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001163 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1164 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001165 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001166 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001167
1168 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001169 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1170 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001171 auto predicate = [&](soinfo* si) {
1172 return si->get_st_dev() != 0 &&
1173 si->get_st_ino() != 0 &&
1174 si->get_st_dev() == file_stat.st_dev &&
1175 si->get_st_ino() == file_stat.st_ino &&
1176 si->get_file_offset() == file_offset;
1177 };
1178
1179 soinfo* si = ns->soinfo_list().find_if(predicate);
1180
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001181 if (si != nullptr) {
1182 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1183 "will return existing soinfo", name, si->get_realpath());
1184 task->set_soinfo(si);
1185 return true;
1186 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001187 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001188
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001189 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001190 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001191 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001192 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001193
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001194 if (!ns->is_accessible(realpath)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001195 // TODO(dimitry): workaround for http://b/26394120 - the grey-list
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001196
1197 // TODO(dimitry) before O release: add a namespace attribute to have this enabled
1198 // only for classloader-namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001199 const soinfo* needed_by = task->is_dt_needed() ? task->get_needed_by() : nullptr;
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001200 if (is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001201 // print warning only if needed by non-system library
1202 if (needed_by == nullptr || !is_system_library(needed_by->get_realpath())) {
1203 const soinfo* needed_or_dlopened_by = task->get_needed_by();
1204 const char* sopath = needed_or_dlopened_by == nullptr ? "(unknown)" :
1205 needed_or_dlopened_by->get_realpath();
1206 DL_WARN("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the namespace \"%s\""
1207 " - the access is temporarily granted as a workaround for http://b/26394120, note that the access"
1208 " will be removed in future releases of Android.",
1209 name, realpath.c_str(), sopath, ns->get_name());
1210 add_dlwarning(sopath, "unauthorized access to", name);
1211 }
1212 } else {
1213 // do not load libraries if they are not accessible for the specified namespace.
1214 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1215 "(unknown)" :
1216 task->get_needed_by()->get_realpath();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001217
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001218 DL_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
1219 name, needed_or_dlopened_by, ns->get_name());
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001220
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001221 // do not print this if a library is in the list of shared libraries for linked namespaces
1222 if (!maybe_accessible_via_namespace_links(ns, name)) {
1223 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1224 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1225 " permitted_paths=\"%s\"]",
1226 name, realpath.c_str(),
1227 needed_or_dlopened_by,
1228 ns->get_name(),
1229 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1230 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1231 android::base::Join(ns->get_permitted_paths(), ':').c_str());
1232 }
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001233 return false;
1234 }
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001235 }
1236
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001237 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001238 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001239 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001240 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001241
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001242 task->set_soinfo(si);
1243
1244 // Read the ELF header and some of the segments.
1245 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001246 soinfo_free(si);
1247 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001248 return false;
1249 }
1250
1251 // find and set DT_RUNPATH and dt_soname
1252 // Note that these field values are temporary and are
1253 // going to be overwritten on soinfo::prelink_image
1254 // with values from PT_LOAD segments.
1255 const ElfReader& elf_reader = task->get_elf_reader();
1256 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1257 if (d->d_tag == DT_RUNPATH) {
1258 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1259 }
1260 if (d->d_tag == DT_SONAME) {
1261 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1262 }
1263 }
1264
1265 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
1266 load_tasks->push_back(LoadTask::create(name, si, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001267 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001268
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001269 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001270}
1271
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001272static bool load_library(android_namespace_t* ns,
1273 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001274 ZipArchiveCache* zip_archive_cache,
1275 LoadTaskList* load_tasks,
1276 int rtld_flags) {
1277 const char* name = task->get_name();
1278 soinfo* needed_by = task->get_needed_by();
1279 const android_dlextinfo* extinfo = task->get_extinfo();
1280
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001281 off64_t file_offset;
1282 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001283 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001284 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001285 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1286 file_offset = extinfo->library_fd_offset;
1287 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001288
1289 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1290 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1291 "Will use given name.", name);
1292 realpath = name;
1293 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001294
1295 task->set_fd(extinfo->library_fd, false);
1296 task->set_file_offset(file_offset);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001297 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001298 }
1299
1300 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001301 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001302 if (fd == -1) {
1303 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001304 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001305 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001306
1307 task->set_fd(fd, true);
1308 task->set_file_offset(file_offset);
1309
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001310 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001311}
1312
Dimitry Ivanov3bd90612017-02-01 08:54:43 -08001313// Returns true if library was found and false otherwise
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001314static bool find_loaded_library_by_soname(android_namespace_t* ns,
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001315 const char* name, soinfo** candidate) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001316 *candidate = nullptr;
1317
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001318 // Ignore filename with path.
1319 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001320 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001321 }
1322
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001323 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001324 const char* soname = si->get_soname();
1325 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dimitry Ivanov3bd90612017-02-01 08:54:43 -08001326 *candidate = si;
1327 return false;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001328 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001329
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001330 return true;
1331 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001332}
1333
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001334static bool find_library_in_linked_namespace(const android_namespace_link_t& namespace_link,
1335 LoadTask* task,
1336 int rtld_flags) {
1337 android_namespace_t* ns = namespace_link.linked_namespace();
1338
1339 soinfo* candidate;
1340 bool loaded = false;
1341
1342 std::string soname;
1343 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
1344 loaded = true;
1345 soname = candidate->get_soname();
1346 } else {
1347 soname = resolve_soname(task->get_name());
1348 }
1349
1350 if (!namespace_link.is_accessible(soname.c_str())) {
1351 // the library is not accessible via namespace_link
1352 return false;
1353 }
1354
1355 // if library is already loaded - return it
1356 if (loaded) {
1357 task->set_soinfo(candidate);
1358 return true;
1359 }
1360
1361 // try to load the library - once namespace boundary is crossed
1362 // we need to load a library within separate load_group
1363 // to avoid using symbols from foreign namespace while.
1364 //
1365 // All symbols during relocation should be resolved within a
1366 // namespace to preserve library locality to a namespace.
1367 const char* name = task->get_name();
1368 if (find_libraries(ns,
1369 task->get_needed_by(),
1370 &name,
1371 1,
1372 &candidate,
1373 nullptr /* ld_preloads */,
1374 0 /* ld_preload_count*/,
1375 rtld_flags,
1376 nullptr /* extinfo*/,
1377 false /* add_as_children */,
1378 false /* search_linked_namespaces */)) {
1379 task->set_soinfo(candidate);
1380 return true;
1381 }
1382
1383 return false;
1384}
1385
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001386static bool find_library_internal(android_namespace_t* ns,
1387 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001388 ZipArchiveCache* zip_archive_cache,
1389 LoadTaskList* load_tasks,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001390 int rtld_flags,
1391 bool search_linked_namespaces) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001392 soinfo* candidate;
1393
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001394 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001395 task->set_soinfo(candidate);
1396 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001397 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001398
1399 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001400 // of this fact is done by load_library.
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001401 TRACE("[ \"%s\" find_loaded_library_by_soname failed (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001402 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001403
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001404 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags)) {
1405 return true;
1406 }
1407
1408 if (search_linked_namespaces) {
1409 // if a library was not found - look into linked namespaces
1410 for (auto& linked_namespace : ns->linked_namespaces()) {
1411 if (find_library_in_linked_namespace(linked_namespace,
1412 task,
1413 rtld_flags)) {
1414 return true;
1415 }
1416 }
1417 }
1418
1419 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001420}
1421
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001422static void soinfo_unload(soinfo* si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001423static void soinfo_unload(soinfo* soinfos[], size_t count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001424
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001425// TODO: this is slightly unusual way to construct
1426// the global group for relocation. Not every RTLD_GLOBAL
1427// library is included in this group for backwards-compatibility
1428// reasons.
1429//
1430// This group consists of the main executable, LD_PRELOADs
1431// and libraries with the DF_1_GLOBAL flag set.
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001432static soinfo_list_t make_global_group(android_namespace_t* ns) {
1433 soinfo_list_t global_group;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001434 ns->soinfo_list().for_each([&](soinfo* si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001435 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1436 global_group.push_back(si);
1437 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001438 });
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001439
1440 return global_group;
1441}
1442
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001443// This function provides a list of libraries to be shared
1444// by the namespace. For the default namespace this is the global
1445// group (see make_global_group). For all others this is a group
1446// of RTLD_GLOBAL libraries (which includes the global group from
1447// the default namespace).
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001448static soinfo_list_t get_shared_group(android_namespace_t* ns) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001449 if (ns == &g_default_namespace) {
1450 return make_global_group(ns);
1451 }
1452
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001453 soinfo_list_t shared_group;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001454 ns->soinfo_list().for_each([&](soinfo* si) {
1455 if ((si->get_rtld_flags() & RTLD_GLOBAL) != 0) {
1456 shared_group.push_back(si);
1457 }
1458 });
1459
1460 return shared_group;
1461}
1462
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001463static void shuffle(std::vector<LoadTask*>* v) {
1464 for (size_t i = 0, size = v->size(); i < size; ++i) {
1465 size_t n = size - i;
1466 size_t r = arc4random_uniform(n);
1467 std::swap((*v)[n-1], (*v)[r]);
1468 }
1469}
1470
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001471// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1472// not their transitive dependencies) as children of the start_with library.
1473// This is false when find_libraries is called for dlopen(), when newly loaded
1474// libraries must form a disjoint tree.
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001475bool find_libraries(android_namespace_t* ns,
1476 soinfo* start_with,
1477 const char* const library_names[],
1478 size_t library_names_count,
1479 soinfo* soinfos[],
1480 std::vector<soinfo*>* ld_preloads,
1481 size_t ld_preloads_count,
1482 int rtld_flags,
1483 const android_dlextinfo* extinfo,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001484 bool add_as_children,
1485 bool search_linked_namespaces) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001486 // Step 0: prepare.
1487 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001488 std::unordered_map<const soinfo*, ElfReader> readers_map;
1489
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001490 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001491 const char* name = library_names[i];
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001492 load_tasks.push_back(LoadTask::create(name, start_with, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001493 }
1494
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001495 // Construct global_group.
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001496 soinfo_list_t global_group = make_global_group(ns);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001497
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001498 // If soinfos array is null allocate one on stack.
1499 // The array is needed in case of failure; for example
1500 // when library_names[] = {libone.so, libtwo.so} and libone.so
1501 // is loaded correctly but libtwo.so failed for some reason.
1502 // In this case libone.so should be unloaded on return.
1503 // See also implementation of failure_guard below.
1504
1505 if (soinfos == nullptr) {
1506 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1507 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1508 memset(soinfos, 0, soinfos_size);
1509 }
1510
1511 // list of libraries to link - see step 2.
1512 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001513
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001514 auto scope_guard = make_scope_guard([&]() {
1515 for (LoadTask* t : load_tasks) {
1516 LoadTask::deleter(t);
1517 }
1518 });
1519
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001520 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001521 // Housekeeping
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001522 soinfo_unload(soinfos, soinfos_count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001523 });
1524
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001525 ZipArchiveCache zip_archive_cache;
1526
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001527 // Step 1: expand the list of load_tasks to include
1528 // all DT_NEEDED libraries (do not load them just yet)
1529 for (size_t i = 0; i<load_tasks.size(); ++i) {
1530 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07001531 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001532
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001533 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001534 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001535 task->set_dt_needed(is_dt_needed);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001536
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001537 if (!find_library_internal(ns,
1538 task,
1539 &zip_archive_cache,
1540 &load_tasks,
1541 rtld_flags,
1542 search_linked_namespaces || is_dt_needed)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001543 return false;
1544 }
1545
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001546 soinfo* si = task->get_soinfo();
1547
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001548 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001549 needed_by->add_child(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001550
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001551 if (si->is_linked()) {
1552 si->increment_ref_count();
1553 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001554 }
1555
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001556 // When ld_preloads is not null, the first
1557 // ld_preloads_count libs are in fact ld_preloads.
1558 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001559 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001560 }
1561
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001562 if (soinfos_count < library_names_count) {
1563 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001564 }
1565 }
1566
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001567 // Step 2: Load libraries in random order (see b/24047022)
1568 LoadTaskList load_list;
1569 for (auto&& task : load_tasks) {
1570 soinfo* si = task->get_soinfo();
1571 auto pred = [&](const LoadTask* t) {
1572 return t->get_soinfo() == si;
1573 };
1574
1575 if (!si->is_linked() &&
1576 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
1577 load_list.push_back(task);
1578 }
1579 }
1580 shuffle(&load_list);
1581
1582 for (auto&& task : load_list) {
1583 if (!task->load()) {
1584 return false;
1585 }
1586 }
1587
1588 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
1589 for (auto&& task : load_tasks) {
1590 soinfo* si = task->get_soinfo();
1591 if (!si->is_linked() && !si->prelink_image()) {
1592 return false;
1593 }
1594 }
1595
1596 // Step 4: Add LD_PRELOADed libraries to the global group for
1597 // future runs. There is no need to explicitly add them to
1598 // the global group for this run because they are going to
1599 // appear in the local group in the correct order.
1600 if (ld_preloads != nullptr) {
1601 for (auto&& si : *ld_preloads) {
1602 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
1603 }
1604 }
1605
1606
1607 // Step 5: link libraries.
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001608 soinfo_list_t local_group;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001609 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001610 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
1611 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001612 [&] (soinfo* si) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001613 if (ns->is_accessible(si)) {
1614 local_group.push_back(si);
1615 return kWalkContinue;
1616 } else {
1617 return kWalkSkip;
1618 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001619 });
1620
1621 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001622 if (!si->is_linked()) {
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001623 if (!si->link_image(global_group, local_group, extinfo) ||
1624 !get_cfi_shadow()->AfterLoad(si, solist_get_head())) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001625 return false;
1626 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001627 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001628
1629 return true;
1630 });
1631
1632 if (linked) {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001633 local_group.for_each([](soinfo* si) {
1634 if (!si->is_linked()) {
1635 si->set_linked();
1636 }
1637 });
1638
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001639 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001640 }
1641
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001642 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001643}
1644
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001645static soinfo* find_library(android_namespace_t* ns,
1646 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001647 const android_dlextinfo* extinfo,
1648 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001649 soinfo* si;
1650
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001651 if (name == nullptr) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001652 si = solist_get_somain();
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001653 } else if (!find_libraries(ns,
1654 needed_by,
1655 &name,
1656 1,
1657 &si,
1658 nullptr,
1659 0,
1660 rtld_flags,
1661 extinfo,
1662 false /* add_as_children */,
1663 true /* search_linked_namespaces */)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001664 return nullptr;
1665 }
1666
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001667 si->increment_ref_count();
1668
Elliott Hughesd23736e2012-11-01 15:16:56 -07001669 return si;
1670}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001671
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001672static void soinfo_unload(soinfo* root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001673 if (root->is_linked()) {
1674 root = root->get_local_group_root();
1675 }
1676
1677 if (!root->can_unload()) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001678 TRACE("not unloading \"%s\" - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001679 return;
1680 }
1681
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001682 soinfo_unload(&root, 1);
1683}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001684
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001685static void soinfo_unload(soinfo* soinfos[], size_t count) {
1686 // Note that the library can be loaded but not linked;
1687 // in which case there is no root but we still need
1688 // to walk the tree and unload soinfos involved.
1689 //
1690 // This happens on unsuccessful dlopen, when one of
1691 // the DT_NEEDED libraries could not be linked/found.
1692 if (count == 0) {
1693 return;
1694 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001695
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001696 soinfo_list_t unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001697 for (size_t i = 0; i < count; ++i) {
1698 soinfo* si = soinfos[i];
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001699
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001700 if (si->can_unload()) {
1701 size_t ref_count = si->is_linked() ? si->decrement_ref_count() : 0;
1702 if (ref_count == 0) {
1703 unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001704 } else {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001705 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
1706 si->get_realpath(), ref_count);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001707 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001708 } else {
1709 TRACE("not unloading '%s' - the binary is flagged with NODELETE", si->get_realpath());
1710 return;
1711 }
1712 }
1713
1714 // This is used to identify soinfos outside of the load-group
1715 // note that we cannot have > 1 in the array and have any of them
1716 // linked. This is why we can safely use the first one.
1717 soinfo* root = soinfos[0];
1718
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001719 soinfo_list_t local_unload_list;
1720 soinfo_list_t external_unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001721 soinfo* si = nullptr;
1722
1723 while ((si = unload_list.pop_front()) != nullptr) {
1724 if (local_unload_list.contains(si)) {
1725 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001726 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001727
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001728 local_unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001729
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001730 if (si->has_min_version(0)) {
1731 soinfo* child = nullptr;
1732 while ((child = si->get_children().pop_front()) != nullptr) {
1733 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
1734 child->get_realpath(), child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001735
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001736 child->get_parents().remove(si);
1737
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001738 if (local_unload_list.contains(child)) {
1739 continue;
1740 } else if (child->is_linked() && child->get_local_group_root() != root) {
1741 external_unload_list.push_back(child);
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001742 } else if (child->get_parents().empty()) {
1743 unload_list.push_back(child);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001744 }
1745 }
1746 } else {
1747#if !defined(__work_around_b_24465209__)
1748 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
1749#else
1750 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
1751 for_each_dt_needed(si, [&] (const char* library_name) {
1752 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
1753 si->get_realpath(), library_name);
1754
1755 soinfo* needed = find_library(si->get_primary_namespace(),
1756 library_name, RTLD_NOLOAD, nullptr, nullptr);
1757
1758 if (needed != nullptr) {
1759 // Not found: for example if symlink was deleted between dlopen and dlclose
1760 // Since we cannot really handle errors at this point - print and continue.
1761 PRINT("warning: couldn't find %s needed by %s on unload.",
1762 library_name, si->get_realpath());
1763 return;
1764 } else if (local_unload_list.contains(needed)) {
1765 // already visited
1766 return;
1767 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
1768 // external group
1769 external_unload_list.push_back(needed);
1770 } else {
1771 // local group
1772 unload_list.push_front(needed);
1773 }
1774 });
1775#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001776 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001777 }
1778
1779 local_unload_list.for_each([](soinfo* si) {
1780 si->call_destructors();
1781 });
1782
1783 while ((si = local_unload_list.pop_front()) != nullptr) {
1784 notify_gdb_of_unload(si);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001785 get_cfi_shadow()->BeforeUnload(si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001786 soinfo_free(si);
1787 }
1788
1789 while ((si = external_unload_list.pop_front()) != nullptr) {
1790 soinfo_unload(si);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001791 }
1792}
1793
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001794static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
1795 if (sym_ver == nullptr) {
1796 return sym_name;
1797 }
1798
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001799 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001800}
1801
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001802static android_namespace_t* get_caller_namespace(soinfo* caller) {
1803 return caller != nullptr ? caller->get_primary_namespace() : g_anonymous_namespace;
1804}
1805
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001806void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001807 // Use basic string manipulation calls to avoid snprintf.
1808 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1809 // When debug malloc is enabled, this call returns 0. This in turn causes
1810 // snprintf to do nothing, which causes libraries to fail to load.
1811 // See b/17302493 for further details.
1812 // Once the above bug is fixed, this code can be modified to use
1813 // snprintf again.
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001814 size_t required_len = 0;
1815 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
1816 required_len += strlen(g_default_ld_paths[i]) + 1;
1817 }
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001818 if (buffer_size < required_len) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001819 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
1820 "buffer len %zu, required len %zu", buffer_size, required_len);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001821 }
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001822 char* end = buffer;
1823 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
1824 if (i > 0) *end++ = ':';
1825 end = stpcpy(end, g_default_ld_paths[i]);
1826 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001827}
1828
Elliott Hughescade4c32012-12-20 14:42:14 -08001829void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08001830 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08001831}
1832
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001833static std::string android_dlextinfo_to_string(const android_dlextinfo* info) {
1834 if (info == nullptr) {
1835 return "(null)";
1836 }
1837
1838 return android::base::StringPrintf("[flags=0x%" PRIx64 ","
1839 " reserved_addr=%p,"
1840 " reserved_size=0x%zx,"
1841 " relro_fd=%d,"
1842 " library_fd=%d,"
1843 " library_fd_offset=0x%" PRIx64 ","
1844 " library_namespace=%s@%p]",
1845 info->flags,
1846 info->reserved_addr,
1847 info->reserved_size,
1848 info->relro_fd,
1849 info->library_fd,
1850 info->library_fd_offset,
1851 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
1852 (info->library_namespace != nullptr ?
1853 info->library_namespace->get_name() : "(null)") : "(n/a)",
1854 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
1855 info->library_namespace : nullptr);
1856}
1857
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08001858void* do_dlopen(const char* name, int flags,
1859 const android_dlextinfo* extinfo,
1860 const void* caller_addr) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001861 soinfo* const caller = find_containing_library(caller_addr);
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001862 android_namespace_t* ns = get_caller_namespace(caller);
1863
1864 LD_LOG(kLogDlopen,
1865 "dlopen(name=\"%s\", flags=0x%x, extinfo=%s, caller=\"%s\", caller_ns=%s@%p) ...",
1866 name,
1867 flags,
1868 android_dlextinfo_to_string(extinfo).c_str(),
1869 caller == nullptr ? "(null)" : caller->get_realpath(),
1870 ns == nullptr ? "(null)" : ns->get_name(),
1871 ns);
1872
1873 auto failure_guard = make_scope_guard([&]() {
1874 LD_LOG(kLogDlopen, "... dlopen failed: %s", linker_get_error_buffer());
1875 });
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001876
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001877 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001878 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001879 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001880 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001881
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001882 if (extinfo != nullptr) {
1883 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1884 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1885 return nullptr;
1886 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07001887
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001888 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001889 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001890 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
1891 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001892 return nullptr;
1893 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07001894
1895 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
1896 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
1897 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
1898 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
1899 return nullptr;
1900 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001901
1902 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
1903 if (extinfo->library_namespace == nullptr) {
1904 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
1905 return nullptr;
1906 }
1907 ns = extinfo->library_namespace;
1908 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001909 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001910
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001911 std::string asan_name_holder;
1912
1913 const char* translated_name = name;
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001914 if (g_is_asan && translated_name != nullptr && translated_name[0] == '/') {
1915 char translated_path[PATH_MAX];
1916 if (realpath(translated_name, translated_path) != nullptr) {
1917 if (file_is_in_dir(translated_path, kSystemLibDir)) {
1918 asan_name_holder = std::string(kAsanSystemLibDir) + "/" + basename(translated_path);
1919 if (file_exists(asan_name_holder.c_str())) {
1920 translated_name = asan_name_holder.c_str();
1921 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1922 }
1923 } else if (file_is_in_dir(translated_path, kVendorLibDir)) {
1924 asan_name_holder = std::string(kAsanVendorLibDir) + "/" + basename(translated_path);
1925 if (file_exists(asan_name_holder.c_str())) {
1926 translated_name = asan_name_holder.c_str();
1927 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1928 }
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001929 }
1930 }
1931 }
1932
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001933 ProtectedDataGuard guard;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001934 soinfo* si = find_library(ns, translated_name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001935 if (si != nullptr) {
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001936 void* handle = si->to_handle();
1937 LD_LOG(kLogDlopen,
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08001938 "... dlopen calling constructors: realpath=\"%s\", soname=\"%s\", handle=%p",
1939 si->get_realpath(), si->get_soname(), handle);
1940 si->call_constructors();
1941 failure_guard.disable();
1942 LD_LOG(kLogDlopen,
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001943 "... dlopen successful: realpath=\"%s\", soname=\"%s\", handle=%p",
1944 si->get_realpath(), si->get_soname(), handle);
1945 return handle;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001946 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001947
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001948 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001949}
1950
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001951int do_dladdr(const void* addr, Dl_info* info) {
1952 // Determine if this address can be found in any library currently mapped.
1953 soinfo* si = find_containing_library(addr);
1954 if (si == nullptr) {
1955 return 0;
1956 }
1957
1958 memset(info, 0, sizeof(Dl_info));
1959
1960 info->dli_fname = si->get_realpath();
1961 // Address at which the shared object is loaded.
1962 info->dli_fbase = reinterpret_cast<void*>(si->base);
1963
1964 // Determine if any symbol in the library contains the specified address.
1965 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
1966 if (sym != nullptr) {
1967 info->dli_sname = si->get_string(sym->st_name);
1968 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
1969 }
1970
1971 return 1;
1972}
1973
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001974static soinfo* soinfo_from_handle(void* handle) {
1975 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
1976 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
1977 if (it == g_soinfo_handles_map.end()) {
1978 return nullptr;
1979 } else {
1980 return it->second;
1981 }
1982 }
1983
1984 return static_cast<soinfo*>(handle);
1985}
1986
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08001987bool do_dlsym(void* handle,
1988 const char* sym_name,
1989 const char* sym_ver,
1990 const void* caller_addr,
1991 void** symbol) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001992#if !defined(__LP64__)
1993 if (handle == nullptr) {
1994 DL_ERR("dlsym failed: library handle is null");
1995 return false;
1996 }
1997#endif
1998
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001999 soinfo* found = nullptr;
2000 const ElfW(Sym)* sym = nullptr;
2001 soinfo* caller = find_containing_library(caller_addr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002002 android_namespace_t* ns = get_caller_namespace(caller);
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002003 soinfo* si = nullptr;
2004 if (handle != RTLD_DEFAULT && handle != RTLD_NEXT) {
2005 si = soinfo_from_handle(handle);
2006 }
2007
2008 LD_LOG(kLogDlsym,
2009 "dlsym(handle=%p(\"%s\"), sym_name=\"%s\", sym_ver=\"%s\", caller=\"%s\", caller_ns=%s@%p) ...",
2010 handle,
2011 si != nullptr ? si->get_realpath() : "n/a",
2012 sym_name,
2013 sym_ver,
2014 caller == nullptr ? "(null)" : caller->get_realpath(),
2015 ns == nullptr ? "(null)" : ns->get_name(),
2016 ns);
2017
2018 auto failure_guard = make_scope_guard([&]() {
2019 LD_LOG(kLogDlsym, "... dlsym failed: %s", linker_get_error_buffer());
2020 });
2021
2022 if (sym_name == nullptr) {
2023 DL_ERR("dlsym failed: symbol name is null");
2024 return false;
2025 }
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002026
2027 version_info vi_instance;
2028 version_info* vi = nullptr;
2029
2030 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002031 vi_instance.name = sym_ver;
2032 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002033 vi = &vi_instance;
2034 }
2035
2036 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2037 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2038 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002039 if (si == nullptr) {
2040 DL_ERR("dlsym failed: invalid handle: %p", handle);
2041 return false;
2042 }
2043 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002044 }
2045
2046 if (sym != nullptr) {
2047 uint32_t bind = ELF_ST_BIND(sym->st_info);
2048
2049 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2050 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002051 failure_guard.disable();
2052 LD_LOG(kLogDlsym,
2053 "... dlsym successful: sym_name=\"%s\", sym_ver=\"%s\", found in=\"%s\", address=%p",
2054 sym_name, sym_ver, found->get_soname(), *symbol);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002055 return true;
2056 }
2057
2058 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2059 return false;
2060 }
2061
2062 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2063 return false;
2064}
2065
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002066int do_dlclose(void* handle) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002067 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002068 soinfo* si = soinfo_from_handle(handle);
2069 if (si == nullptr) {
2070 DL_ERR("invalid handle: %p", handle);
2071 return -1;
2072 }
2073
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002074 soinfo_unload(si);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002075 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002076}
2077
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002078bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path) {
2079 if (g_anonymous_namespace_initialized) {
2080 DL_ERR("anonymous namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002081 return false;
2082 }
2083
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002084 ProtectedDataGuard guard;
2085
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002086 g_anonymous_namespace_initialized = true;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002087
2088 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002089 // When the caller is nullptr - create_namespace will take global group
2090 // from the anonymous namespace, which is fine because anonymous namespace
2091 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002092 android_namespace_t* anon_ns =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002093 create_namespace(nullptr,
2094 "(anonymous)",
2095 nullptr,
2096 library_search_path,
2097 // TODO (dimitry): change to isolated eventually.
2098 ANDROID_NAMESPACE_TYPE_REGULAR,
2099 nullptr,
2100 &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002101
2102 if (anon_ns == nullptr) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002103 g_anonymous_namespace_initialized = false;
2104 return false;
2105 }
2106
2107 if (!link_namespaces(anon_ns, &g_default_namespace, shared_lib_sonames)) {
2108 g_anonymous_namespace_initialized = false;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002109 return false;
2110 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08002111
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002112 g_anonymous_namespace = anon_ns;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002113
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002114 return true;
2115}
2116
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002117static void add_soinfos_to_namespace(const soinfo_list_t& soinfos, android_namespace_t* ns) {
2118 ns->add_soinfos(soinfos);
2119 for (auto si : soinfos) {
2120 si->add_secondary_namespace(ns);
2121 }
2122}
2123
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002124android_namespace_t* create_namespace(const void* caller_addr,
2125 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002126 const char* ld_library_path,
2127 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002128 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002129 const char* permitted_when_isolated_path,
2130 android_namespace_t* parent_namespace) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002131 if (!g_anonymous_namespace_initialized) {
2132 DL_ERR("cannot create namespace: anonymous namespace is not initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002133 return nullptr;
2134 }
2135
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002136 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002137 // if parent_namespace is nullptr -> set it to the caller namespace
2138 soinfo* caller_soinfo = find_containing_library(caller_addr);
2139
2140 parent_namespace = caller_soinfo != nullptr ?
2141 caller_soinfo->get_primary_namespace() :
2142 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002143 }
2144
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002145 ProtectedDataGuard guard;
2146 std::vector<std::string> ld_library_paths;
2147 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002148 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002149
2150 parse_path(ld_library_path, ":", &ld_library_paths);
2151 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002152 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002153
2154 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2155 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002156 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002157 ns->set_ld_library_paths(std::move(ld_library_paths));
2158 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002159 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002160
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002161 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002162 // If shared - clone the parent namespace
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002163 add_soinfos_to_namespace(parent_namespace->soinfo_list(), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002164 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002165 // If not shared - copy only the shared group
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002166 add_soinfos_to_namespace(get_shared_group(parent_namespace), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002167 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002168
2169 return ns;
2170}
2171
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002172bool link_namespaces(android_namespace_t* namespace_from,
2173 android_namespace_t* namespace_to,
2174 const char* shared_lib_sonames) {
2175 if (namespace_to == nullptr) {
2176 namespace_to = &g_default_namespace;
2177 }
2178
2179 if (namespace_from == nullptr) {
2180 DL_ERR("error linking namespaces: namespace_from is null.");
2181 return false;
2182 }
2183
2184 if (shared_lib_sonames == nullptr || shared_lib_sonames[0] == '\0') {
2185 DL_ERR("error linking namespaces \"%s\"->\"%s\": the list of shared libraries is empty.",
2186 namespace_from->get_name(), namespace_to->get_name());
2187 return false;
2188 }
2189
2190 auto sonames = android::base::Split(shared_lib_sonames, ":");
2191 std::unordered_set<std::string> sonames_set(sonames.begin(), sonames.end());
2192
2193 ProtectedDataGuard guard;
2194 namespace_from->add_linked_namespace(namespace_to, sonames_set);
2195
2196 return true;
2197}
2198
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002199ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002200 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2201 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2202 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002203 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2204 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002205
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002206 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002207}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002208
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002209const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2210 if (source_symver < 2 ||
2211 source_symver >= version_infos.size() ||
2212 version_infos[source_symver].name == nullptr) {
2213 return nullptr;
2214 }
2215
2216 return &version_infos[source_symver];
2217}
2218
2219void VersionTracker::add_version_info(size_t source_index,
2220 ElfW(Word) elf_hash,
2221 const char* ver_name,
2222 const soinfo* target_si) {
2223 if (source_index >= version_infos.size()) {
2224 version_infos.resize(source_index+1);
2225 }
2226
2227 version_infos[source_index].elf_hash = elf_hash;
2228 version_infos[source_index].name = ver_name;
2229 version_infos[source_index].target_si = target_si;
2230}
2231
2232bool VersionTracker::init_verneed(const soinfo* si_from) {
2233 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2234
2235 if (verneed_ptr == 0) {
2236 return true;
2237 }
2238
2239 size_t verneed_cnt = si_from->get_verneed_cnt();
2240
2241 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2242 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2243 size_t vernaux_offset = offset + verneed->vn_aux;
2244 offset += verneed->vn_next;
2245
2246 if (verneed->vn_version != 1) {
2247 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2248 return false;
2249 }
2250
2251 const char* target_soname = si_from->get_string(verneed->vn_file);
2252 // find it in dependencies
2253 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002254 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002255 });
2256
2257 if (target_si == nullptr) {
2258 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002259 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002260 return false;
2261 }
2262
2263 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2264 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2265 vernaux_offset += vernaux->vna_next;
2266
2267 const ElfW(Word) elf_hash = vernaux->vna_hash;
2268 const char* ver_name = si_from->get_string(vernaux->vna_name);
2269 ElfW(Half) source_index = vernaux->vna_other;
2270
2271 add_version_info(source_index, elf_hash, ver_name, target_si);
2272 }
2273 }
2274
2275 return true;
2276}
2277
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002278template <typename F>
2279static bool for_each_verdef(const soinfo* si, F functor) {
2280 if (!si->has_min_version(2)) {
2281 return true;
2282 }
2283
2284 uintptr_t verdef_ptr = si->get_verdef_ptr();
2285 if (verdef_ptr == 0) {
2286 return true;
2287 }
2288
2289 size_t offset = 0;
2290
2291 size_t verdef_cnt = si->get_verdef_cnt();
2292 for (size_t i = 0; i<verdef_cnt; ++i) {
2293 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
2294 size_t verdaux_offset = offset + verdef->vd_aux;
2295 offset += verdef->vd_next;
2296
2297 if (verdef->vd_version != 1) {
2298 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
2299 i, verdef->vd_version, si->get_realpath());
2300 return false;
2301 }
2302
2303 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
2304 // "this is the version of the file itself. It must not be used for
2305 // matching a symbol. It can be used to match references."
2306 //
2307 // http://www.akkadia.org/drepper/symbol-versioning
2308 continue;
2309 }
2310
2311 if (verdef->vd_cnt == 0) {
2312 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
2313 return false;
2314 }
2315
2316 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
2317
2318 if (functor(i, verdef, verdaux) == true) {
2319 break;
2320 }
2321 }
2322
2323 return true;
2324}
2325
2326bool find_verdef_version_index(const soinfo* si, const version_info* vi, ElfW(Versym)* versym) {
2327 if (vi == nullptr) {
2328 *versym = kVersymNotNeeded;
2329 return true;
2330 }
2331
2332 *versym = kVersymGlobal;
2333
2334 return for_each_verdef(si,
2335 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2336 if (verdef->vd_hash == vi->elf_hash &&
2337 strcmp(vi->name, si->get_string(verdaux->vda_name)) == 0) {
2338 *versym = verdef->vd_ndx;
2339 return true;
2340 }
2341
2342 return false;
2343 }
2344 );
2345}
2346
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002347bool VersionTracker::init_verdef(const soinfo* si_from) {
2348 return for_each_verdef(si_from,
2349 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2350 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2351 si_from->get_string(verdaux->vda_name), si_from);
2352 return false;
2353 }
2354 );
2355}
2356
2357bool VersionTracker::init(const soinfo* si_from) {
2358 if (!si_from->has_min_version(2)) {
2359 return true;
2360 }
2361
2362 return init_verneed(si_from) && init_verdef(si_from);
2363}
2364
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002365// TODO (dimitry): Methods below need to be moved out of soinfo
2366// and in more isolated file in order minimize dependencies on
2367// unnecessary object in the linker binary. Consider making them
2368// independent from soinfo (?).
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002369bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2370 const char* sym_name, const version_info** vi) {
2371 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2372 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2373
2374 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2375 *vi = version_tracker.get_version_info(sym_ver);
2376
2377 if (*vi == nullptr) {
2378 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002379 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002380 return false;
2381 }
2382 } else {
2383 // there is no version info
2384 *vi = nullptr;
2385 }
2386
2387 return true;
2388}
2389
Dimitry Ivanov576a3752016-08-09 06:58:55 -07002390#if !defined(__mips__)
2391#if defined(USE_RELA)
2392static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2393 return rela->r_addend;
2394}
2395#else
2396static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
2397 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2398 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
2399 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2400 }
2401 return 0;
2402}
2403#endif
2404
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002405template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002406bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2407 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002408 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2409 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002410 if (rel == nullptr) {
2411 return false;
2412 }
2413
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002414 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2415 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2416
2417 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002418 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002419 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002420 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002421
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002422 DEBUG("Processing \"%s\" relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002423 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002424 continue;
2425 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002426
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002427 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002428 soinfo* lsi = nullptr;
2429
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002430 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002431 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002432 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002433
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002434 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2435 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002436 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002437
2438 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2439 return false;
2440 }
2441
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002442 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002443 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002444 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002445 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002446 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002447 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002448 }
2449
2450 /* IHI0044C AAELF 4.5.1.1:
2451
2452 Libraries are not searched to resolve weak references.
2453 It is not an error for a weak reference to remain unsatisfied.
2454
2455 During linking, the value of an undefined weak reference is:
2456 - Zero if the relocation type is absolute
2457 - The address of the place if the relocation is pc-relative
2458 - The address of nominal base address if the relocation
2459 type is base-relative.
2460 */
2461
2462 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002463 case R_GENERIC_JUMP_SLOT:
2464 case R_GENERIC_GLOB_DAT:
2465 case R_GENERIC_RELATIVE:
2466 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002467#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002468 case R_AARCH64_ABS64:
2469 case R_AARCH64_ABS32:
2470 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002471#elif defined(__x86_64__)
2472 case R_X86_64_32:
2473 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002474#elif defined(__arm__)
2475 case R_ARM_ABS32:
2476#elif defined(__i386__)
2477 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002478#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002479 /*
2480 * The sym_addr was initialized to be zero above, or the relocation
2481 * code below does not care about value of sym_addr.
2482 * No need to do anything.
2483 */
2484 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002485#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002486 case R_X86_64_PC32:
2487 sym_addr = reloc;
2488 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002489#elif defined(__i386__)
2490 case R_386_PC32:
2491 sym_addr = reloc;
2492 break;
2493#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002494 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002495 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002496 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002497 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002498 } else { // We got a definition.
2499#if !defined(__LP64__)
2500 // When relocating dso with text_relocation .text segment is
2501 // not executable. We need to restore elf flags before resolving
2502 // STT_GNU_IFUNC symbol.
2503 bool protect_segments = has_text_relocations &&
2504 lsi == this &&
2505 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2506 if (protect_segments) {
2507 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2508 DL_ERR("can't protect segments for \"%s\": %s",
2509 get_realpath(), strerror(errno));
2510 return false;
2511 }
2512 }
2513#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002514 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002515#if !defined(__LP64__)
2516 if (protect_segments) {
2517 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2518 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2519 get_realpath(), strerror(errno));
2520 return false;
2521 }
2522 }
2523#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002524 }
2525 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002526 }
2527
2528 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002529 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002530 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002531 MARK(rel->r_offset);
2532 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2533 reinterpret_cast<void*>(reloc),
2534 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2535
2536 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002537 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002538 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002539 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002540 MARK(rel->r_offset);
2541 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2542 reinterpret_cast<void*>(reloc),
2543 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2544 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002545 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002546 case R_GENERIC_RELATIVE:
2547 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002548 MARK(rel->r_offset);
2549 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2550 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002551 reinterpret_cast<void*>(load_bias + addend));
2552 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002553 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002554 case R_GENERIC_IRELATIVE:
2555 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002556 MARK(rel->r_offset);
2557 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2558 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002559 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002560 {
2561#if !defined(__LP64__)
2562 // When relocating dso with text_relocation .text segment is
2563 // not executable. We need to restore elf flags for this
2564 // particular call.
2565 if (has_text_relocations) {
2566 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2567 DL_ERR("can't protect segments for \"%s\": %s",
2568 get_realpath(), strerror(errno));
2569 return false;
2570 }
2571 }
2572#endif
2573 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2574#if !defined(__LP64__)
2575 // Unprotect it afterwards...
2576 if (has_text_relocations) {
2577 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2578 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2579 get_realpath(), strerror(errno));
2580 return false;
2581 }
2582 }
2583#endif
2584 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2585 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002586 break;
2587
2588#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002589 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002590 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002591 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002592 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002593 reloc, sym_addr + addend, sym_name);
2594 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002595 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002596 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002597 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002598 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002599 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002600 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002601 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002602 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2603 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002604 if ((min_value <= (sym_addr + addend)) &&
2605 ((sym_addr + addend) <= max_value)) {
2606 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002607 } else {
2608 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002609 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002610 return false;
2611 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002612 }
2613 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002614 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002615 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002616 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002617 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002618 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002619 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002620 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2621 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002622 if ((min_value <= (sym_addr + addend)) &&
2623 ((sym_addr + addend) <= max_value)) {
2624 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002625 } else {
2626 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002627 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002628 return false;
2629 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002630 }
2631 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002632 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002633 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002634 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002635 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002636 reloc, sym_addr + addend, rel->r_offset, sym_name);
2637 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002638 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002639 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002640 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002641 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002642 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002643 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002644 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002645 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2646 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002647 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2648 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2649 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002650 } else {
2651 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002652 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002653 return false;
2654 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002655 }
2656 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002657 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002658 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002659 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002660 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002661 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002662 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002663 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2664 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002665 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2666 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2667 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002668 } else {
2669 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002670 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002671 return false;
2672 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002673 }
2674 break;
2675
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002676 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002677 /*
2678 * ET_EXEC is not supported so this should not happen.
2679 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002680 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002681 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002682 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002683 * R_AARCH64_COPY may only appear in executable objects where e_type is
2684 * set to ET_EXEC.
2685 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002686 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002687 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002688 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002689 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002690 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002691 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002692 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002693 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002694 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002695 break;
2696#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002697 case R_X86_64_32:
2698 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002699 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002700 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2701 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002702 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002703 break;
2704 case R_X86_64_64:
2705 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002706 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002707 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2708 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002709 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002710 break;
2711 case R_X86_64_PC32:
2712 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002713 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002714 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2715 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2716 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002717 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002718 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002719#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002720 case R_ARM_ABS32:
2721 count_relocation(kRelocAbsolute);
2722 MARK(rel->r_offset);
2723 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2724 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2725 break;
2726 case R_ARM_REL32:
2727 count_relocation(kRelocRelative);
2728 MARK(rel->r_offset);
2729 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2730 reloc, sym_addr, rel->r_offset, sym_name);
2731 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2732 break;
2733 case R_ARM_COPY:
2734 /*
2735 * ET_EXEC is not supported so this should not happen.
2736 *
2737 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2738 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002739 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002740 * R_ARM_COPY may only appear in executable objects where e_type is
2741 * set to ET_EXEC.
2742 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002743 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002744 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002745#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002746 case R_386_32:
2747 count_relocation(kRelocRelative);
2748 MARK(rel->r_offset);
2749 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2750 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2751 break;
2752 case R_386_PC32:
2753 count_relocation(kRelocRelative);
2754 MARK(rel->r_offset);
2755 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2756 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2757 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2758 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002759#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002760 default:
2761 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002762 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002763 }
2764 }
2765 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002766}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002767#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002768
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002769// An empty list of soinfos
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002770static soinfo_list_t g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002771
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002772bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002773 /* Extract dynamic section */
2774 ElfW(Word) dynamic_flags = 0;
2775 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002776
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002777 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002778 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002779 if (!relocating_linker) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002780 INFO("[ Linking \"%s\" ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002781 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002782 }
2783
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002784 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002785 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002786 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002787 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002788 return false;
2789 } else {
2790 if (!relocating_linker) {
2791 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002792 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002793 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002794
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002795#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002796 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2797 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002798#endif
2799
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002800 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002801 // Note that: "Except for the DT_NULL element at the end of the array,
2802 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2803 //
2804 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002805 uint32_t needed_count = 0;
2806 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2807 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2808 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2809 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002810 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002811 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002812 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002813
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002814 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002815 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2816 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2817 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2818 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002819 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002820
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002821 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002822 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002823 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002824 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2825 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002826
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002827 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002828 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002829 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002830 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
2831 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002832
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002833 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002834 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002835 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002836 return false;
2837 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002838 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002839
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002840 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002841 break;
2842
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002843 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002844 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002845 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002846
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002847 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002848 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002849 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002850
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002851 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002852 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002853 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002854
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002855 case DT_SYMENT:
2856 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002857 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
2858 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002859 return false;
2860 }
2861 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002862
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002863 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002864#if defined(USE_RELA)
2865 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002866 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002867 return false;
2868 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002869#else
2870 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002871 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002872 return false;
2873 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002874#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002875 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002876
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002877 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002878#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002879 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002880#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002881 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002882#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002883 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002884
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002885 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002886#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002887 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002888#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002889 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002890#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002891 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002892
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002893 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002894#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002895 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002896 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002897#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002898 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2899 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002900
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002901 case DT_DEBUG:
2902 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2903 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002904// FIXME: not working currently for N64
2905// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002906// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002907// read-only, but the DYNAMIC header claims it is writable.
2908#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002909 if ((dynamic_flags & PF_W) != 0) {
2910 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2911 }
Chris Dearman99186652014-02-06 20:36:51 -08002912#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002913 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002914#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002915 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002916 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002917 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002918
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002919 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002920 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002921 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002922
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002923 case DT_ANDROID_RELA:
2924 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2925 break;
2926
2927 case DT_ANDROID_RELASZ:
2928 android_relocs_size_ = d->d_un.d_val;
2929 break;
2930
2931 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002932 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002933 return false;
2934
2935 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002936 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002937 return false;
2938
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002939 case DT_RELAENT:
2940 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002941 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002942 return false;
2943 }
2944 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002945
2946 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002947 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002948 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002949
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002950 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002951 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002952 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002953
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002954 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002955 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002956 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002957
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002958#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002959 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002960 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002961 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002962
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002963 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002964 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002965 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002966
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002967 case DT_RELENT:
2968 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002969 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002970 return false;
2971 }
2972 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002973
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002974 case DT_ANDROID_REL:
2975 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2976 break;
2977
2978 case DT_ANDROID_RELSZ:
2979 android_relocs_size_ = d->d_un.d_val;
2980 break;
2981
2982 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002983 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002984 return false;
2985
2986 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002987 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002988 return false;
2989
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002990 // "Indicates that all RELATIVE relocations have been concatenated together,
2991 // and specifies the RELATIVE relocation count."
2992 //
2993 // TODO: Spec also mentions that this can be used to optimize relocation process;
2994 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002995 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002996 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002997
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002998 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002999 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003000 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003001
3002 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003003 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003004 return false;
3005
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003006#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003007 case DT_INIT:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003008 init_func_ = reinterpret_cast<linker_ctor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003009 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003010 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003011
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003012 case DT_FINI:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003013 fini_func_ = reinterpret_cast<linker_dtor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003014 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_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_INIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003018 init_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003019 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
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_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003023 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003024 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003025
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003026 case DT_FINI_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003027 fini_array_ = reinterpret_cast<linker_dtor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003028 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
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_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003032 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003033 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003034
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003035 case DT_PREINIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003036 preinit_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003037 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
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_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003041 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003042 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003043
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003044 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003045#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003046 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003047 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003048#else
3049 has_text_relocations = true;
3050 break;
3051#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003052
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003053 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003054 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003055 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003056
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003057 case DT_NEEDED:
3058 ++needed_count;
3059 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003060
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003061 case DT_FLAGS:
3062 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003063#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003064 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003065 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003066#else
3067 has_text_relocations = true;
3068#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003069 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003070 if (d->d_un.d_val & DF_SYMBOLIC) {
3071 has_DT_SYMBOLIC = true;
3072 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003073 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003074
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003075 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003076 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003077
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003078 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003079 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 -07003080 }
3081 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003082#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003083 case DT_MIPS_RLD_MAP:
3084 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3085 {
3086 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3087 *dp = &_r_debug;
3088 }
3089 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003090 case DT_MIPS_RLD_MAP_REL:
3091 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003092 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003093 r_debug** dp = reinterpret_cast<r_debug**>(
3094 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003095 *dp = &_r_debug;
3096 }
3097 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003098
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003099 case DT_MIPS_RLD_VERSION:
3100 case DT_MIPS_FLAGS:
3101 case DT_MIPS_BASE_ADDRESS:
3102 case DT_MIPS_UNREFEXTNO:
3103 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003104
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003105 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003106 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003107 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003108
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003109 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003110 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003111 break;
3112
3113 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003114 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003115 break;
3116#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003117 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3118 case DT_BIND_NOW:
3119 break;
3120
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003121 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003122 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3123 break;
3124
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003125 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003126 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3127 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003128 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003129 verdef_cnt_ = d->d_un.d_val;
3130 break;
3131
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003132 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003133 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3134 break;
3135
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003136 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003137 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003138 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003139
Evgenii Stepanov68650822015-06-10 13:38:39 -07003140 case DT_RUNPATH:
3141 // this is parsed after we have strtab initialized (see below).
3142 break;
3143
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003144 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003145 if (!relocating_linker) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003146 DL_WARN("\"%s\" unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003147 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3148 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003149 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003150 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003151 }
3152
Duane Sandbc425c72015-06-01 16:29:14 -07003153#if defined(__mips__) && !defined(__LP64__)
3154 if (!mips_check_and_adjust_fp_modes()) {
3155 return false;
3156 }
3157#endif
3158
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003159 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003160 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003161
3162 // Sanity checks.
3163 if (relocating_linker && needed_count != 0) {
3164 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3165 return false;
3166 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003167 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003168 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003169 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003170 return false;
3171 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003172 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003173 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003174 return false;
3175 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003176 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003177 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003178 return false;
3179 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003180
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003181 // second pass - parse entries relying on strtab
3182 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003183 switch (d->d_tag) {
3184 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003185 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003186 break;
3187 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003188 set_dt_runpath(get_string(d->d_un.d_val));
3189 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003190 }
3191 }
3192
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003193 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003194 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003195 // because they can't find dt_needed library by soname.
3196 // This workaround should keep them working. (applies only
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003197 // for apps targeting sdk version < M). Make an exception for
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003198 // the main executable and linker; they do not need to have dt_soname
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003199 if (soname_ == nullptr &&
3200 this != solist_get_somain() &&
3201 (flags_ & FLAG_LINKER) == 0 &&
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003202 get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003203 soname_ = basename(realpath_.c_str());
3204 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3205 get_realpath(), soname_);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003206 // 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 -07003207 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003208 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003209}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003210
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003211bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3212 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003213
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003214 local_group_root_ = local_group.front();
3215 if (local_group_root_ == nullptr) {
3216 local_group_root_ = this;
3217 }
3218
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003219 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3220 target_sdk_version_ = get_application_target_sdk_version();
3221 }
3222
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003223 VersionTracker version_tracker;
3224
3225 if (!version_tracker.init(this)) {
3226 return false;
3227 }
3228
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003229#if !defined(__LP64__)
3230 if (has_text_relocations) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003231 // Fail if app is targeting M or above.
3232 if (get_application_target_sdk_version() >= __ANDROID_API_M__) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003233 DL_ERR_AND_LOG("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003234 return false;
3235 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003236 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003237 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003238 DL_WARN("\"%s\" has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003239 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003240 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003241 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3242 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003243 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003244 return false;
3245 }
3246 }
3247#endif
3248
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003249 if (android_relocs_ != nullptr) {
3250 // check signature
3251 if (android_relocs_size_ > 3 &&
3252 android_relocs_[0] == 'A' &&
3253 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003254 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003255 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003256 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003257
3258 bool relocated = false;
3259 const uint8_t* packed_relocs = android_relocs_ + 4;
3260 const size_t packed_relocs_size = android_relocs_size_ - 4;
3261
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003262 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003263 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003264 packed_reloc_iterator<sleb128_decoder>(
3265 sleb128_decoder(packed_relocs, packed_relocs_size)),
3266 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003267
3268 if (!relocated) {
3269 return false;
3270 }
3271 } else {
3272 DL_ERR("bad android relocation header.");
3273 return false;
3274 }
3275 }
3276
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003277#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003278 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003279 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003280 if (!relocate(version_tracker,
3281 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003282 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003283 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003284 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003285 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003286 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003287 if (!relocate(version_tracker,
3288 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003289 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003290 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003291 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003292#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003293 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003294 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003295 if (!relocate(version_tracker,
3296 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003297 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003298 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003299 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003300 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003301 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003302 if (!relocate(version_tracker,
3303 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003304 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003305 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003306 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003307#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003308
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003309#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003310 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003311 return false;
3312 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003313#endif
3314
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003315 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003316
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003317#if !defined(__LP64__)
3318 if (has_text_relocations) {
3319 // All relocations are done, we can protect our segments back to read-only.
3320 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3321 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003322 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003323 return false;
3324 }
3325 }
3326#endif
3327
Mingwei Shibe910522015-11-12 07:02:14 +00003328 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3329 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3330 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003331 return false;
3332 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003333
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003334 /* Handle serializing/sharing the RELRO segment */
3335 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3336 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3337 extinfo->relro_fd) < 0) {
3338 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003339 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003340 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003341 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003342 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3343 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3344 extinfo->relro_fd) < 0) {
3345 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003346 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003347 return false;
3348 }
3349 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003350
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003351 notify_gdb_of_load(this);
3352 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003353}
3354
Mingwei Shibe910522015-11-12 07:02:14 +00003355bool soinfo::protect_relro() {
3356 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3357 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3358 get_realpath(), strerror(errno));
3359 return false;
3360 }
3361 return true;
3362}
3363
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003364void init_default_namespace() {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003365 g_default_namespace.set_name("(default)");
3366 g_default_namespace.set_isolated(false);
3367
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003368 soinfo* somain = solist_get_somain();
3369
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003370 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3371 somain->load_bias);
3372 const char* bname = basename(interp);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003373 if (bname && (strcmp(bname, "linker_asan") == 0 || strcmp(bname, "linker_asan64") == 0)) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003374 g_default_ld_paths = kAsanDefaultLdPaths;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07003375 g_is_asan = true;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003376 } else {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003377 g_default_ld_paths = kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003378 }
3379
neo.chae2589f9d2016-10-04 11:00:27 +09003380 char real_path[PATH_MAX];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003381 std::vector<std::string> ld_default_paths;
3382 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
neo.chae2589f9d2016-10-04 11:00:27 +09003383 if (realpath(g_default_ld_paths[i], real_path) != nullptr) {
3384 ld_default_paths.push_back(real_path);
3385 } else {
3386 ld_default_paths.push_back(g_default_ld_paths[i]);
3387 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003388 }
3389
3390 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003391};