blob: d7864b4606aad54b0a109902824a862e3d71c36e [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__)
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -070081static const char* const kSystemLibDir = "/system/lib64";
82static const char* const kVendorLibDir = "/vendor/lib64";
83static const char* const kAsanSystemLibDir = "/data/lib64";
84static const char* const kAsanVendorLibDir = "/data/vendor/lib64";
Elliott Hughes011bc0b2013-10-08 14:27:10 -070085#else
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -070086static const char* const kSystemLibDir = "/system/lib";
87static const char* const kVendorLibDir = "/vendor/lib";
88static const char* const kAsanSystemLibDir = "/data/lib";
89static const char* const kAsanVendorLibDir = "/data/vendor/lib";
Elliott Hughes011bc0b2013-10-08 14:27:10 -070090#endif
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -070091
92static const char* const kDefaultLdPaths[] = {
93 kSystemLibDir,
94 kVendorLibDir,
Dmitriy Ivanov851135b2014-08-29 12:02:36 -070095 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -070096};
David Bartleybc3a5c22009-06-02 18:27:28 -070097
Evgenii Stepanovd640b222015-07-10 17:54:01 -070098static const char* const kAsanDefaultLdPaths[] = {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -070099 kAsanSystemLibDir,
100 kSystemLibDir,
101 kAsanVendorLibDir,
102 kVendorLibDir,
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700103 nullptr
104};
105
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700106// Is ASAN enabled?
107static bool g_is_asan = false;
108
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700109static CFIShadowWriter g_cfi_shadow;
110
111CFIShadowWriter* get_cfi_shadow() {
112 return &g_cfi_shadow;
113}
114
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700115static bool is_system_library(const std::string& realpath) {
116 for (const auto& dir : g_default_namespace.get_default_library_paths()) {
117 if (file_is_in_dir(realpath, dir)) {
118 return true;
119 }
120 }
121 return false;
122}
123
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700124// Checks if the file exists and not a directory.
125static bool file_exists(const char* path) {
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700126 struct stat s;
127
128 if (stat(path, &s) != 0) {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700129 return false;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700130 }
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700131
132 return S_ISREG(s.st_mode);
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700133}
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700134
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800135static std::string resolve_soname(const std::string& name) {
136 // We assume that soname equals to basename here
137
138 // TODO(dimitry): consider having honest absolute-path -> soname resolution
139 // note that since we might end up refusing to load this library because
140 // it is not in shared libs list we need to get the soname without actually loading
141 // the library.
142 //
143 // On the other hand there are several places where we already assume that
144 // soname == basename in particular for any not-loaded library mentioned
145 // in DT_NEEDED list.
146 return basename(name.c_str());
147}
148
149static bool maybe_accessible_via_namespace_links(android_namespace_t* ns, const char* name) {
150 std::string soname = resolve_soname(name);
151 for (auto& ns_link : ns->linked_namespaces()) {
152 if (ns_link.is_accessible(soname.c_str())) {
153 return true;
154 }
155 }
156
157 return false;
158}
159
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700160// TODO(dimitry): The grey-list is a workaround for http://b/26394120 ---
161// gradually remove libraries from this list until it is gone.
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800162static bool is_greylisted(android_namespace_t* ns, const char* name, const soinfo* needed_by) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700163 static const char* const kLibraryGreyList[] = {
164 "libandroid_runtime.so",
165 "libbinder.so",
166 "libcrypto.so",
167 "libcutils.so",
168 "libexpat.so",
169 "libgui.so",
170 "libmedia.so",
171 "libnativehelper.so",
172 "libskia.so",
173 "libssl.so",
174 "libstagefright.so",
175 "libsqlite.so",
176 "libui.so",
177 "libutils.so",
178 "libvorbisidec.so",
179 nullptr
180 };
181
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800182 // If you're targeting N, you don't get the greylist.
183 if (get_application_target_sdk_version() >= __ANDROID_API_N__) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700184 return false;
185 }
186
187 // if the library needed by a system library - implicitly assume it
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800188 // is greylisted unless it is in the list of shared libraries for one or
189 // more linked namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700190 if (needed_by != nullptr && is_system_library(needed_by->get_realpath())) {
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800191 return !maybe_accessible_via_namespace_links(ns, name);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700192 }
193
194 // if this is an absolute path - make sure it points to /system/lib(64)
195 if (name[0] == '/' && dirname(name) == kSystemLibDir) {
196 // and reduce the path to basename
197 name = basename(name);
198 }
199
200 for (size_t i = 0; kLibraryGreyList[i] != nullptr; ++i) {
201 if (strcmp(name, kLibraryGreyList[i]) == 0) {
202 return true;
203 }
204 }
205
206 return false;
207}
208// END OF WORKAROUND
209
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700210static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800211
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800212static bool g_anonymous_namespace_initialized;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700213
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800214#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700215struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700216 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700217};
218
219static linker_stats_t linker_stats;
220
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800221void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700222 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700223}
224#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800225void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700226}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800227#endif
228
229#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800230uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800231#endif
232
Elliott Hughesbedfe382012-08-14 14:07:59 -0700233static void notify_gdb_of_load(soinfo* info) {
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800234 if (info->is_linker() || info->is_main_executable()) {
235 // gdb already knows about the linker and the main executable.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700236 return;
237 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800238
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800239 link_map* map = &(info->link_map_head);
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000240
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800241 map->l_addr = info->load_bias;
242 // link_map l_name field is not const.
243 map->l_name = const_cast<char*>(info->get_realpath());
244 map->l_ld = info->dynamic;
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000245
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800246 CHECK(map->l_name != nullptr);
247 CHECK(map->l_name[0] != '\0');
248
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800249 notify_gdb_of_load(map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700250}
251
Elliott Hughesbedfe382012-08-14 14:07:59 -0700252static void notify_gdb_of_unload(soinfo* info) {
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800253 notify_gdb_of_unload(&(info->link_map_head));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800254}
255
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700256LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
257 return g_soinfo_links_allocator.alloc();
258}
259
260void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
261 g_soinfo_links_allocator.free(entry);
262}
263
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700264LinkedListEntry<android_namespace_t>* NamespaceListAllocator::alloc() {
265 return g_namespace_list_allocator.alloc();
266}
267
268void NamespaceListAllocator::free(LinkedListEntry<android_namespace_t>* entry) {
269 g_namespace_list_allocator.free(entry);
270}
271
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700272soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
273 struct stat* file_stat, off64_t file_offset,
274 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700275 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200276 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700277 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200278 }
279
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700280 TRACE("name %s: allocating soinfo for ns=%p", name, ns);
281
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700282 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
283 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700284
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700285 solist_add_soinfo(si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200286
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700287 si->generate_handle();
288 ns->add_soinfo(si);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700289
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700290 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200291 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800292}
293
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800294static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700295 if (si == nullptr) {
296 return;
297 }
298
299 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800300 if (!si->is_mapped_by_caller()) {
301 munmap(reinterpret_cast<void*>(si->base), si->size);
302 } else {
303 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
304 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
305 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
306 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700307 }
308
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700309 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700310
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700311 if (!solist_remove_soinfo(si)) {
312 // TODO (dimitry): revisit this - for now preserving the logic
313 // but it does not look right, abort if soinfo is not in the list instead?
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700314 return;
315 }
Elliott Hughes46882792012-08-03 16:49:39 -0700316
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700317 // clear links to/from si
318 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700319
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700320 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700321 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800322}
323
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700324static void parse_path(const char* path, const char* delimiters,
325 std::vector<std::string>* resolved_paths) {
326 std::vector<std::string> paths;
327 split_path(path, delimiters, &paths);
328 resolve_paths(paths, resolved_paths);
329}
330
Elliott Hughescade4c32012-12-20 14:42:14 -0800331static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700332 std::vector<std::string> ld_libary_paths;
333 parse_path(path, ":", &ld_libary_paths);
334 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800335}
336
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700337static bool realpath_fd(int fd, std::string* realpath) {
338 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700339 __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700340 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700341 PRINT("readlink(\"%s\") failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700342 return false;
343 }
344
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700345 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700346 return true;
347}
348
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700349#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700350
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700351// For a given PC, find the .so that it belongs to.
352// Returns the base address of the .ARM.exidx section
353// for that .so, and the number of 8-byte entries
354// in that section (via *pcount).
355//
356// Intended to be called by libc's __gnu_Unwind_Find_exidx().
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800357_Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800358 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800359
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700360 for (soinfo* si = solist_get_head(); si != 0; si = si->next) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700361 if ((addr >= si->base) && (addr < (si->base + si->size))) {
362 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800363 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800364 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700365 }
366 *pcount = 0;
367 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800368}
Elliott Hughes46882792012-08-03 16:49:39 -0700369
Christopher Ferris24053a42013-08-19 17:45:09 -0700370#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700371
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700372// Here, we only have to provide a callback to iterate across all the
373// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700374int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700375 int rv = 0;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700376 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700377 dl_phdr_info dl_info;
378 dl_info.dlpi_addr = si->link_map_head.l_addr;
379 dl_info.dlpi_name = si->link_map_head.l_name;
380 dl_info.dlpi_phdr = si->phdr;
381 dl_info.dlpi_phnum = si->phnum;
382 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
383 if (rv != 0) {
384 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800385 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700386 }
387 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800388}
Elliott Hughes46882792012-08-03 16:49:39 -0700389
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800390
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700391bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700392 soinfo** si_found_in, const soinfo_list_t& global_group,
393 const soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800394 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700395 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700396
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700397 /* "This element's presence in a shared object library alters the dynamic linker's
398 * symbol resolution algorithm for references within the library. Instead of starting
399 * a symbol search with the executable file, the dynamic linker starts from the shared
400 * object itself. If the shared object fails to supply the referenced symbol, the
401 * dynamic linker then searches the executable file and other shared objects as usual."
402 *
403 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
404 *
405 * Note that this is unlikely since static linker avoids generating
406 * relocations for -Bsymbolic linked dynamic executables.
407 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700408 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700409 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700410 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
411 return false;
412 }
413
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700414 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700415 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700416 }
417 }
418
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700419 // 1. Look for it in global_group
420 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700421 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700422 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700423 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700424 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700425 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
426 error = true;
427 return false;
428 }
429
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700430 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700431 *si_found_in = global_si;
432 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700433 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700434
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700435 return true;
436 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700437
438 if (error) {
439 return false;
440 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700441 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700442
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700443 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700444 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700445 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700446 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700447 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700448 // we already did this - skip
449 return true;
450 }
451
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700452 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700453 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700454 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
455 error = true;
456 return false;
457 }
458
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700459 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700460 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700461 return false;
462 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700463
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700464 return true;
465 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700466
467 if (error) {
468 return false;
469 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700470 }
471
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700472 if (s != nullptr) {
473 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
474 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700475 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
476 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700477 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700478 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700479
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700480 *symbol = s;
481 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700482}
483
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700484ProtectedDataGuard::ProtectedDataGuard() {
485 if (ref_count_++ == 0) {
486 protect_data(PROT_READ | PROT_WRITE);
487 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700488
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700489 if (ref_count_ == 0) { // overflow
490 __libc_fatal("Too many nested calls to dlopen()");
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800491 }
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800492}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800493
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800494ProtectedDataGuard::~ProtectedDataGuard() {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700495 if (--ref_count_ == 0) {
496 protect_data(PROT_READ);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800497 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700498}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800499
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700500void ProtectedDataGuard::protect_data(int protection) {
501 g_soinfo_allocator.protect_all(protection);
502 g_soinfo_links_allocator.protect_all(protection);
503 g_namespace_allocator.protect_all(protection);
504 g_namespace_list_allocator.protect_all(protection);
505}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800506
507size_t ProtectedDataGuard::ref_count_ = 0;
508
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700509// Each size has it's own allocator.
510template<size_t size>
511class SizeBasedAllocator {
512 public:
513 static void* alloc() {
514 return allocator_.alloc();
515 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700516
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700517 static void free(void* ptr) {
518 allocator_.free(ptr);
519 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700520
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700521 private:
522 static LinkerBlockAllocator allocator_;
523};
524
525template<size_t size>
526LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
527
528template<typename T>
529class TypeBasedAllocator {
530 public:
531 static T* alloc() {
532 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
533 }
534
535 static void free(T* ptr) {
536 SizeBasedAllocator<sizeof(T)>::free(ptr);
537 }
538};
539
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700540class LoadTask {
541 public:
542 struct deleter_t {
543 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700544 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700545 TypeBasedAllocator<LoadTask>::free(t);
546 }
547 };
548
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700549 static deleter_t deleter;
550
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800551 static LoadTask* create(const char* name,
552 soinfo* needed_by,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700553 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700554 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700555 return new (ptr) LoadTask(name, needed_by, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700556 }
557
558 const char* get_name() const {
559 return name_;
560 }
561
562 soinfo* get_needed_by() const {
563 return needed_by_;
564 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700565
566 soinfo* get_soinfo() const {
567 return si_;
568 }
569
570 void set_soinfo(soinfo* si) {
571 si_ = si;
572 }
573
574 off64_t get_file_offset() const {
575 return file_offset_;
576 }
577
578 void set_file_offset(off64_t offset) {
579 file_offset_ = offset;
580 }
581
582 int get_fd() const {
583 return fd_;
584 }
585
586 void set_fd(int fd, bool assume_ownership) {
587 fd_ = fd;
588 close_fd_ = assume_ownership;
589 }
590
591 const android_dlextinfo* get_extinfo() const {
592 return extinfo_;
593 }
594
595 void set_extinfo(const android_dlextinfo* extinfo) {
596 extinfo_ = extinfo;
597 }
598
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700599 bool is_dt_needed() const {
600 return is_dt_needed_;
601 }
602
603 void set_dt_needed(bool is_dt_needed) {
604 is_dt_needed_ = is_dt_needed;
605 }
606
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700607 const ElfReader& get_elf_reader() const {
608 CHECK(si_ != nullptr);
609 return (*elf_readers_map_)[si_];
610 }
611
612 ElfReader& get_elf_reader() {
613 CHECK(si_ != nullptr);
614 return (*elf_readers_map_)[si_];
615 }
616
617 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
618 return elf_readers_map_;
619 }
620
621 bool read(const char* realpath, off64_t file_size) {
622 ElfReader& elf_reader = get_elf_reader();
623 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
624 }
625
626 bool load() {
627 ElfReader& elf_reader = get_elf_reader();
628 if (!elf_reader.Load(extinfo_)) {
629 return false;
630 }
631
632 si_->base = elf_reader.load_start();
633 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800634 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700635 si_->load_bias = elf_reader.load_bias();
636 si_->phnum = elf_reader.phdr_count();
637 si_->phdr = elf_reader.loaded_phdr();
638
639 return true;
640 }
641
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700642 private:
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800643 LoadTask(const char* name,
644 soinfo* needed_by,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700645 std::unordered_map<const soinfo*, ElfReader>* readers_map)
646 : name_(name), needed_by_(needed_by), si_(nullptr),
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700647 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map),
648 is_dt_needed_(false) {}
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700649
650 ~LoadTask() {
651 if (fd_ != -1 && close_fd_) {
652 close(fd_);
653 }
654 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700655
656 const char* name_;
657 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700658 soinfo* si_;
659 const android_dlextinfo* extinfo_;
660 int fd_;
661 bool close_fd_;
662 off64_t file_offset_;
663 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700664 // TODO(dimitry): needed by workaround for http://b/26394120 (the grey-list)
665 bool is_dt_needed_;
666 // END OF WORKAROUND
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700667
668 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
669};
670
Ningsheng Jiane93be992014-09-16 15:22:10 +0800671LoadTask::deleter_t LoadTask::deleter;
672
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700673template <typename T>
674using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
675
676typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700677typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700678typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700679
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800680enum walk_action_result_t : uint32_t {
681 kWalkStop = 0,
682 kWalkContinue = 1,
683 kWalkSkip = 2
684};
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700685
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700686// This function walks down the tree of soinfo dependencies
687// in breadth-first order and
688// * calls action(soinfo* si) for each node, and
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800689// * terminates walk if action returns kWalkStop
690// * skips children of the node if action
691// return kWalkSkip
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700692//
693// walk_dependencies_tree returns false if walk was terminated
694// by the action and true otherwise.
695template<typename F>
696static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700697 SoinfoLinkedList visit_list;
698 SoinfoLinkedList visited;
699
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700700 for (size_t i = 0; i < root_soinfos_size; ++i) {
701 visit_list.push_back(root_soinfos[i]);
702 }
703
704 soinfo* si;
705 while ((si = visit_list.pop_front()) != nullptr) {
706 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700707 continue;
708 }
709
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800710 walk_action_result_t result = action(si);
711
712 if (result == kWalkStop) {
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700713 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700714 }
715
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700716 visited.push_back(si);
717
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800718 if (result != kWalkSkip) {
719 si->get_children().for_each([&](soinfo* child) {
720 visit_list.push_back(child);
721 });
722 }
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700723 }
724
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700725 return true;
726}
727
728
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800729static const ElfW(Sym)* dlsym_handle_lookup(android_namespace_t* ns,
730 soinfo* root,
731 soinfo* skip_until,
732 soinfo** found,
733 SymbolName& symbol_name,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800734 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700735 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700736 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700737
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700738 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
739 if (skip_lookup) {
740 skip_lookup = current_soinfo != skip_until;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800741 return kWalkContinue;
742 }
743
744 if (!ns->is_accessible(current_soinfo)) {
745 return kWalkSkip;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700746 }
747
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800748 if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700749 result = nullptr;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800750 return kWalkStop;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700751 }
752
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700753 if (result != nullptr) {
754 *found = current_soinfo;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800755 return kWalkStop;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700756 }
757
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800758 return kWalkContinue;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700759 });
760
761 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800762}
763
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800764static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
765 const char* name,
766 const version_info* vi,
767 soinfo** found,
768 soinfo* caller,
769 void* handle);
770
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700771// This is used by dlsym(3). It performs symbol lookup only within the
772// specified soinfo object and its dependencies in breadth first order.
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800773static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si,
774 soinfo** found,
775 const char* name,
776 const version_info* vi) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700777 // According to man dlopen(3) and posix docs in the case when si is handle
778 // of the main executable we need to search not only in the executable and its
779 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
780 //
781 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
782 // libraries and they are loaded in breath-first (correct) order we can just execute
783 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700784 if (si == solist_get_somain()) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800785 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700786 }
787
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700788 SymbolName symbol_name(name);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800789 // note that the namespace is not the namespace associated with caller_addr
790 // we use ns associated with root si intentionally here. Using caller_ns
791 // causes problems when user uses dlopen_ext to open a library in the separate
792 // namespace and then calls dlsym() on the handle.
793 return dlsym_handle_lookup(si->get_primary_namespace(), si, nullptr, found, symbol_name, vi);
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700794}
795
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800796/* This is used by dlsym(3) to performs a global symbol lookup. If the
797 start value is null (for RTLD_DEFAULT), the search starts at the
798 beginning of the global solist. Otherwise the search starts at the
799 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700800 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800801static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
802 const char* name,
803 const version_info* vi,
804 soinfo** found,
805 soinfo* caller,
806 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800807 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800808
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700809 auto& soinfo_list = ns->soinfo_list();
810 auto start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700811
812 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -0700813 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700814 return nullptr;
815 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700816 auto it = soinfo_list.find(caller);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700817 CHECK (it != soinfo_list.end());
818 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700819 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800820 }
821
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700822 const ElfW(Sym)* s = nullptr;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700823 for (auto it = start, end = soinfo_list.end(); it != end; ++it) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700824 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700825 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800826 // if the library is opened by application with target api level < M.
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700827 // See http://b/21565766
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800828 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 &&
829 si->get_target_sdk_version() >= __ANDROID_API_M__) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700830 continue;
831 }
832
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800833 if (!si->find_symbol_by_name(symbol_name, vi, &s)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700834 return nullptr;
835 }
836
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700837 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800838 *found = si;
839 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600840 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800841 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600842
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700843 // If not found - use dlsym_handle_lookup for caller's
844 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700845 // case we already did it.
846 if (s == nullptr && caller != nullptr &&
847 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800848 soinfo* local_group_root = caller->get_local_group_root();
849
850 return dlsym_handle_lookup(local_group_root->get_primary_namespace(),
851 local_group_root,
852 (handle == RTLD_NEXT) ? caller : nullptr,
853 found,
854 symbol_name,
855 vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700856 }
857
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700858 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700859 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
860 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800861 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800862
Elliott Hughescade4c32012-12-20 14:42:14 -0800863 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800864}
865
Kito Chengfa8c05d2013-03-12 14:58:06 +0800866soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800867 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700868 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800869 if (address >= si->base && address - si->base < si->size) {
870 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600871 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800872 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700873 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600874}
875
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700876class ZipArchiveCache {
877 public:
878 ZipArchiveCache() {}
879 ~ZipArchiveCache();
880
881 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
882 private:
883 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
884
885 std::unordered_map<std::string, ZipArchiveHandle> cache_;
886};
887
888bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
889 std::string key(zip_path);
890
891 auto it = cache_.find(key);
892 if (it != cache_.end()) {
893 *handle = it->second;
894 return true;
895 }
896
897 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
898 if (fd == -1) {
899 return false;
900 }
901
902 if (OpenArchiveFd(fd, "", handle) != 0) {
903 // invalid zip-file (?)
Yabin Cui722072d2016-03-21 17:10:12 -0700904 CloseArchive(handle);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700905 close(fd);
906 return false;
907 }
908
909 cache_[key] = *handle;
910 return true;
911}
912
913ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -0700914 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700915 CloseArchive(it.second);
916 }
917}
918
919static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700920 const char* const input_path,
921 off64_t* file_offset, std::string* realpath) {
922 std::string normalized_path;
923 if (!normalize_path(input_path, &normalized_path)) {
924 return -1;
925 }
926
927 const char* const path = normalized_path.c_str();
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700928 TRACE("Trying zip file open from path \"%s\" -> normalized \"%s\"", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +0000929
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700930 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +0000931 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700932 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +0000933 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700934 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +0000935 if (separator == nullptr) {
936 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -0700937 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000938
939 char buf[512];
940 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
941 PRINT("Warning: ignoring very long library path: %s", path);
942 return -1;
943 }
944
945 buf[separator - path] = '\0';
946
947 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700948 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +0000949 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
950 if (fd == -1) {
951 return -1;
952 }
953
954 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700955 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000956 // invalid zip-file (?)
957 close(fd);
958 return -1;
959 }
960
Simon Baldwinaef71952015-01-16 13:22:54 +0000961 ZipEntry entry;
962
Yusuke Sato56f40fb2015-06-25 14:56:07 -0700963 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000964 // Entry was not found.
965 close(fd);
966 return -1;
967 }
968
969 // Check if it is properly stored
970 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
971 close(fd);
972 return -1;
973 }
974
975 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700976
977 if (realpath_fd(fd, realpath)) {
978 *realpath += separator;
979 } else {
980 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
981 normalized_path.c_str());
982 *realpath = normalized_path;
983 }
984
Simon Baldwinaef71952015-01-16 13:22:54 +0000985 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800986}
987
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700988static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
989 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
990 if (n < 0 || n >= static_cast<int>(buf_size)) {
991 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
992 return false;
993 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000994
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700995 return true;
996}
997
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700998static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
999 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001000 const std::vector<std::string>& paths,
1001 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001002 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001003 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001004 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001005 continue;
1006 }
1007
1008 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001009 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001010 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001011 }
1012
1013 if (fd == -1) {
1014 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1015 if (fd != -1) {
1016 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001017 if (!realpath_fd(fd, realpath)) {
1018 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1019 *realpath = buf;
1020 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001021 }
1022 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001023
1024 if (fd != -1) {
1025 return fd;
1026 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001027 }
1028
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001029 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001030}
1031
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001032static int open_library(android_namespace_t* ns,
1033 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001034 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001035 off64_t* file_offset, std::string* realpath) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001036 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001037
Elliott Hughes124fae92012-10-31 14:20:03 -07001038 // 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 -07001039 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001040 int fd = -1;
1041
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001042 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001043 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1044 }
1045
1046 if (fd == -1) {
1047 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001048 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001049 *file_offset = 0;
1050 if (!realpath_fd(fd, realpath)) {
1051 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1052 *realpath = name;
1053 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001054 }
1055 }
1056
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001057 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001058 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001059
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001060 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1061 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 -07001062 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001063 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001064 // Check if the library is accessible
1065 if (fd != -1 && !ns->is_accessible(*realpath)) {
1066 fd = -1;
1067 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001068 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001069
Elliott Hughes124fae92012-10-31 14:20:03 -07001070 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001071 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001072 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001073
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001074 // TODO(dimitry): workaround for http://b/26394120 (the grey-list)
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001075 if (fd == -1 && ns != &g_default_namespace && is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001076 // try searching for it on default_namespace default_library_path
1077 fd = open_library_on_paths(zip_archive_cache, name, file_offset,
1078 g_default_namespace.get_default_library_paths(), realpath);
1079 }
1080 // END OF WORKAROUND
1081
Elliott Hughes124fae92012-10-31 14:20:03 -07001082 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001083}
1084
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001085const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001086#if !defined(__LP64__)
1087 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Elliott Hughes5bc78c82016-11-16 11:35:43 -08001088 if (get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001089 const char* bname = basename(dt_needed);
1090 if (bname != dt_needed) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001091 DL_WARN("library \"%s\" has invalid DT_NEEDED entry \"%s\"", sopath, dt_needed);
1092 add_dlwarning(sopath, "invalid DT_NEEDED entry", dt_needed);
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001093 }
1094
1095 return bname;
1096 }
1097#endif
1098 return dt_needed;
1099}
1100
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001101template<typename F>
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001102static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1103 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1104 if (d->d_tag == DT_NEEDED) {
1105 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1106 }
1107 }
1108}
1109
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001110static bool load_library(android_namespace_t* ns,
1111 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001112 LoadTaskList* load_tasks,
1113 int rtld_flags,
1114 const std::string& realpath) {
1115 off64_t file_offset = task->get_file_offset();
1116 const char* name = task->get_name();
1117 const android_dlextinfo* extinfo = task->get_extinfo();
1118
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001119 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001120 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001121 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001122 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001123 if (file_offset < 0) {
1124 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001125 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001126 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001127
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001128 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001129 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001130 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001131 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001132 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001133 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001134 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1135 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001136 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001137 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001138
1139 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001140 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1141 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001142 auto predicate = [&](soinfo* si) {
1143 return si->get_st_dev() != 0 &&
1144 si->get_st_ino() != 0 &&
1145 si->get_st_dev() == file_stat.st_dev &&
1146 si->get_st_ino() == file_stat.st_ino &&
1147 si->get_file_offset() == file_offset;
1148 };
1149
1150 soinfo* si = ns->soinfo_list().find_if(predicate);
1151
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001152 if (si != nullptr) {
1153 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1154 "will return existing soinfo", name, si->get_realpath());
1155 task->set_soinfo(si);
1156 return true;
1157 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001158 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001159
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001160 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001161 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001162 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001163 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001164
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001165 if (!ns->is_accessible(realpath)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001166 // TODO(dimitry): workaround for http://b/26394120 - the grey-list
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001167
1168 // TODO(dimitry) before O release: add a namespace attribute to have this enabled
1169 // only for classloader-namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001170 const soinfo* needed_by = task->is_dt_needed() ? task->get_needed_by() : nullptr;
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001171 if (is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001172 // print warning only if needed by non-system library
1173 if (needed_by == nullptr || !is_system_library(needed_by->get_realpath())) {
1174 const soinfo* needed_or_dlopened_by = task->get_needed_by();
1175 const char* sopath = needed_or_dlopened_by == nullptr ? "(unknown)" :
1176 needed_or_dlopened_by->get_realpath();
1177 DL_WARN("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the namespace \"%s\""
1178 " - the access is temporarily granted as a workaround for http://b/26394120, note that the access"
1179 " will be removed in future releases of Android.",
1180 name, realpath.c_str(), sopath, ns->get_name());
1181 add_dlwarning(sopath, "unauthorized access to", name);
1182 }
1183 } else {
1184 // do not load libraries if they are not accessible for the specified namespace.
1185 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1186 "(unknown)" :
1187 task->get_needed_by()->get_realpath();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001188
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001189 DL_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
1190 name, needed_or_dlopened_by, ns->get_name());
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001191
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001192 // do not print this if a library is in the list of shared libraries for linked namespaces
1193 if (!maybe_accessible_via_namespace_links(ns, name)) {
1194 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1195 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1196 " permitted_paths=\"%s\"]",
1197 name, realpath.c_str(),
1198 needed_or_dlopened_by,
1199 ns->get_name(),
1200 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1201 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1202 android::base::Join(ns->get_permitted_paths(), ':').c_str());
1203 }
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001204 return false;
1205 }
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001206 }
1207
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001208 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001209 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001210 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001211 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001212
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001213 task->set_soinfo(si);
1214
1215 // Read the ELF header and some of the segments.
1216 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001217 soinfo_free(si);
1218 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001219 return false;
1220 }
1221
1222 // find and set DT_RUNPATH and dt_soname
1223 // Note that these field values are temporary and are
1224 // going to be overwritten on soinfo::prelink_image
1225 // with values from PT_LOAD segments.
1226 const ElfReader& elf_reader = task->get_elf_reader();
1227 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1228 if (d->d_tag == DT_RUNPATH) {
1229 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1230 }
1231 if (d->d_tag == DT_SONAME) {
1232 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1233 }
1234 }
1235
1236 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
1237 load_tasks->push_back(LoadTask::create(name, si, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001238 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001239
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001240 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001241}
1242
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001243static bool load_library(android_namespace_t* ns,
1244 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001245 ZipArchiveCache* zip_archive_cache,
1246 LoadTaskList* load_tasks,
1247 int rtld_flags) {
1248 const char* name = task->get_name();
1249 soinfo* needed_by = task->get_needed_by();
1250 const android_dlextinfo* extinfo = task->get_extinfo();
1251
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001252 off64_t file_offset;
1253 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001254 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001255 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001256 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1257 file_offset = extinfo->library_fd_offset;
1258 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001259
1260 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1261 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1262 "Will use given name.", name);
1263 realpath = name;
1264 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001265
1266 task->set_fd(extinfo->library_fd, false);
1267 task->set_file_offset(file_offset);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001268 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001269 }
1270
1271 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001272 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001273 if (fd == -1) {
1274 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001275 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001276 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001277
1278 task->set_fd(fd, true);
1279 task->set_file_offset(file_offset);
1280
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001281 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001282}
1283
Dimitry Ivanov3bd90612017-02-01 08:54:43 -08001284// Returns true if library was found and false otherwise
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001285static bool find_loaded_library_by_soname(android_namespace_t* ns,
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001286 const char* name, soinfo** candidate) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001287 *candidate = nullptr;
1288
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001289 // Ignore filename with path.
1290 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001291 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001292 }
1293
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001294 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001295 const char* soname = si->get_soname();
1296 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dimitry Ivanov3bd90612017-02-01 08:54:43 -08001297 *candidate = si;
1298 return false;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001299 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001300
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001301 return true;
1302 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001303}
1304
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001305static bool find_library_in_linked_namespace(const android_namespace_link_t& namespace_link,
1306 LoadTask* task,
1307 int rtld_flags) {
1308 android_namespace_t* ns = namespace_link.linked_namespace();
1309
1310 soinfo* candidate;
1311 bool loaded = false;
1312
1313 std::string soname;
1314 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
1315 loaded = true;
1316 soname = candidate->get_soname();
1317 } else {
1318 soname = resolve_soname(task->get_name());
1319 }
1320
1321 if (!namespace_link.is_accessible(soname.c_str())) {
1322 // the library is not accessible via namespace_link
1323 return false;
1324 }
1325
1326 // if library is already loaded - return it
1327 if (loaded) {
1328 task->set_soinfo(candidate);
1329 return true;
1330 }
1331
1332 // try to load the library - once namespace boundary is crossed
1333 // we need to load a library within separate load_group
1334 // to avoid using symbols from foreign namespace while.
1335 //
1336 // All symbols during relocation should be resolved within a
1337 // namespace to preserve library locality to a namespace.
1338 const char* name = task->get_name();
1339 if (find_libraries(ns,
1340 task->get_needed_by(),
1341 &name,
1342 1,
1343 &candidate,
1344 nullptr /* ld_preloads */,
1345 0 /* ld_preload_count*/,
1346 rtld_flags,
1347 nullptr /* extinfo*/,
1348 false /* add_as_children */,
1349 false /* search_linked_namespaces */)) {
1350 task->set_soinfo(candidate);
1351 return true;
1352 }
1353
1354 return false;
1355}
1356
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001357static bool find_library_internal(android_namespace_t* ns,
1358 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001359 ZipArchiveCache* zip_archive_cache,
1360 LoadTaskList* load_tasks,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001361 int rtld_flags,
1362 bool search_linked_namespaces) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001363 soinfo* candidate;
1364
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001365 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001366 task->set_soinfo(candidate);
1367 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001368 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001369
1370 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001371 // of this fact is done by load_library.
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001372 TRACE("[ \"%s\" find_loaded_library_by_soname failed (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001373 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001374
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001375 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags)) {
1376 return true;
1377 }
1378
1379 if (search_linked_namespaces) {
1380 // if a library was not found - look into linked namespaces
1381 for (auto& linked_namespace : ns->linked_namespaces()) {
1382 if (find_library_in_linked_namespace(linked_namespace,
1383 task,
1384 rtld_flags)) {
1385 return true;
1386 }
1387 }
1388 }
1389
1390 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001391}
1392
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001393static void soinfo_unload(soinfo* si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001394static void soinfo_unload(soinfo* soinfos[], size_t count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001395
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001396// TODO: this is slightly unusual way to construct
1397// the global group for relocation. Not every RTLD_GLOBAL
1398// library is included in this group for backwards-compatibility
1399// reasons.
1400//
1401// This group consists of the main executable, LD_PRELOADs
1402// and libraries with the DF_1_GLOBAL flag set.
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001403static soinfo_list_t make_global_group(android_namespace_t* ns) {
1404 soinfo_list_t global_group;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001405 ns->soinfo_list().for_each([&](soinfo* si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001406 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1407 global_group.push_back(si);
1408 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001409 });
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001410
1411 return global_group;
1412}
1413
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001414// This function provides a list of libraries to be shared
1415// by the namespace. For the default namespace this is the global
1416// group (see make_global_group). For all others this is a group
1417// of RTLD_GLOBAL libraries (which includes the global group from
1418// the default namespace).
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001419static soinfo_list_t get_shared_group(android_namespace_t* ns) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001420 if (ns == &g_default_namespace) {
1421 return make_global_group(ns);
1422 }
1423
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001424 soinfo_list_t shared_group;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001425 ns->soinfo_list().for_each([&](soinfo* si) {
1426 if ((si->get_rtld_flags() & RTLD_GLOBAL) != 0) {
1427 shared_group.push_back(si);
1428 }
1429 });
1430
1431 return shared_group;
1432}
1433
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001434static void shuffle(std::vector<LoadTask*>* v) {
1435 for (size_t i = 0, size = v->size(); i < size; ++i) {
1436 size_t n = size - i;
1437 size_t r = arc4random_uniform(n);
1438 std::swap((*v)[n-1], (*v)[r]);
1439 }
1440}
1441
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001442// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1443// not their transitive dependencies) as children of the start_with library.
1444// This is false when find_libraries is called for dlopen(), when newly loaded
1445// libraries must form a disjoint tree.
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001446bool find_libraries(android_namespace_t* ns,
1447 soinfo* start_with,
1448 const char* const library_names[],
1449 size_t library_names_count,
1450 soinfo* soinfos[],
1451 std::vector<soinfo*>* ld_preloads,
1452 size_t ld_preloads_count,
1453 int rtld_flags,
1454 const android_dlextinfo* extinfo,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001455 bool add_as_children,
1456 bool search_linked_namespaces) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001457 // Step 0: prepare.
1458 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001459 std::unordered_map<const soinfo*, ElfReader> readers_map;
1460
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001461 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001462 const char* name = library_names[i];
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001463 load_tasks.push_back(LoadTask::create(name, start_with, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001464 }
1465
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001466 // Construct global_group.
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001467 soinfo_list_t global_group = make_global_group(ns);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001468
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001469 // If soinfos array is null allocate one on stack.
1470 // The array is needed in case of failure; for example
1471 // when library_names[] = {libone.so, libtwo.so} and libone.so
1472 // is loaded correctly but libtwo.so failed for some reason.
1473 // In this case libone.so should be unloaded on return.
1474 // See also implementation of failure_guard below.
1475
1476 if (soinfos == nullptr) {
1477 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1478 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1479 memset(soinfos, 0, soinfos_size);
1480 }
1481
1482 // list of libraries to link - see step 2.
1483 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001484
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001485 auto scope_guard = make_scope_guard([&]() {
1486 for (LoadTask* t : load_tasks) {
1487 LoadTask::deleter(t);
1488 }
1489 });
1490
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001491 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001492 // Housekeeping
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001493 soinfo_unload(soinfos, soinfos_count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001494 });
1495
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001496 ZipArchiveCache zip_archive_cache;
1497
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001498 // Step 1: expand the list of load_tasks to include
1499 // all DT_NEEDED libraries (do not load them just yet)
1500 for (size_t i = 0; i<load_tasks.size(); ++i) {
1501 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07001502 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001503
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001504 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001505 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001506 task->set_dt_needed(is_dt_needed);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001507
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001508 if (!find_library_internal(ns,
1509 task,
1510 &zip_archive_cache,
1511 &load_tasks,
1512 rtld_flags,
1513 search_linked_namespaces || is_dt_needed)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001514 return false;
1515 }
1516
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001517 soinfo* si = task->get_soinfo();
1518
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001519 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001520 needed_by->add_child(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001521
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001522 if (si->is_linked()) {
1523 si->increment_ref_count();
1524 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001525 }
1526
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001527 // When ld_preloads is not null, the first
1528 // ld_preloads_count libs are in fact ld_preloads.
1529 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001530 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001531 }
1532
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001533 if (soinfos_count < library_names_count) {
1534 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001535 }
1536 }
1537
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001538 // Step 2: Load libraries in random order (see b/24047022)
1539 LoadTaskList load_list;
1540 for (auto&& task : load_tasks) {
1541 soinfo* si = task->get_soinfo();
1542 auto pred = [&](const LoadTask* t) {
1543 return t->get_soinfo() == si;
1544 };
1545
1546 if (!si->is_linked() &&
1547 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
1548 load_list.push_back(task);
1549 }
1550 }
1551 shuffle(&load_list);
1552
1553 for (auto&& task : load_list) {
1554 if (!task->load()) {
1555 return false;
1556 }
1557 }
1558
1559 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
1560 for (auto&& task : load_tasks) {
1561 soinfo* si = task->get_soinfo();
1562 if (!si->is_linked() && !si->prelink_image()) {
1563 return false;
1564 }
1565 }
1566
1567 // Step 4: Add LD_PRELOADed libraries to the global group for
1568 // future runs. There is no need to explicitly add them to
1569 // the global group for this run because they are going to
1570 // appear in the local group in the correct order.
1571 if (ld_preloads != nullptr) {
1572 for (auto&& si : *ld_preloads) {
1573 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
1574 }
1575 }
1576
1577
1578 // Step 5: link libraries.
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001579 soinfo_list_t local_group;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001580 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001581 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
1582 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001583 [&] (soinfo* si) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001584 if (ns->is_accessible(si)) {
1585 local_group.push_back(si);
1586 return kWalkContinue;
1587 } else {
1588 return kWalkSkip;
1589 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001590 });
1591
1592 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001593 if (!si->is_linked()) {
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001594 if (!si->link_image(global_group, local_group, extinfo) ||
1595 !get_cfi_shadow()->AfterLoad(si, solist_get_head())) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001596 return false;
1597 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001598 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001599
1600 return true;
1601 });
1602
1603 if (linked) {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001604 local_group.for_each([](soinfo* si) {
1605 if (!si->is_linked()) {
1606 si->set_linked();
1607 }
1608 });
1609
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001610 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001611 }
1612
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001613 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001614}
1615
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001616static soinfo* find_library(android_namespace_t* ns,
1617 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001618 const android_dlextinfo* extinfo,
1619 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001620 soinfo* si;
1621
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001622 if (name == nullptr) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001623 si = solist_get_somain();
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001624 } else if (!find_libraries(ns,
1625 needed_by,
1626 &name,
1627 1,
1628 &si,
1629 nullptr,
1630 0,
1631 rtld_flags,
1632 extinfo,
1633 false /* add_as_children */,
1634 true /* search_linked_namespaces */)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001635 return nullptr;
1636 }
1637
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001638 si->increment_ref_count();
1639
Elliott Hughesd23736e2012-11-01 15:16:56 -07001640 return si;
1641}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001642
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001643static void soinfo_unload(soinfo* root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001644 if (root->is_linked()) {
1645 root = root->get_local_group_root();
1646 }
1647
1648 if (!root->can_unload()) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001649 TRACE("not unloading \"%s\" - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001650 return;
1651 }
1652
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001653 soinfo_unload(&root, 1);
1654}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001655
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001656static void soinfo_unload(soinfo* soinfos[], size_t count) {
1657 // Note that the library can be loaded but not linked;
1658 // in which case there is no root but we still need
1659 // to walk the tree and unload soinfos involved.
1660 //
1661 // This happens on unsuccessful dlopen, when one of
1662 // the DT_NEEDED libraries could not be linked/found.
1663 if (count == 0) {
1664 return;
1665 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001666
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001667 soinfo_list_t unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001668 for (size_t i = 0; i < count; ++i) {
1669 soinfo* si = soinfos[i];
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001670
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001671 if (si->can_unload()) {
1672 size_t ref_count = si->is_linked() ? si->decrement_ref_count() : 0;
1673 if (ref_count == 0) {
1674 unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001675 } else {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001676 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
1677 si->get_realpath(), ref_count);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001678 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001679 } else {
1680 TRACE("not unloading '%s' - the binary is flagged with NODELETE", si->get_realpath());
1681 return;
1682 }
1683 }
1684
1685 // This is used to identify soinfos outside of the load-group
1686 // note that we cannot have > 1 in the array and have any of them
1687 // linked. This is why we can safely use the first one.
1688 soinfo* root = soinfos[0];
1689
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001690 soinfo_list_t local_unload_list;
1691 soinfo_list_t external_unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001692 soinfo* si = nullptr;
1693
1694 while ((si = unload_list.pop_front()) != nullptr) {
1695 if (local_unload_list.contains(si)) {
1696 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001697 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001698
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001699 local_unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001700
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001701 if (si->has_min_version(0)) {
1702 soinfo* child = nullptr;
1703 while ((child = si->get_children().pop_front()) != nullptr) {
1704 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
1705 child->get_realpath(), child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001706
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001707 child->get_parents().remove(si);
1708
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001709 if (local_unload_list.contains(child)) {
1710 continue;
1711 } else if (child->is_linked() && child->get_local_group_root() != root) {
1712 external_unload_list.push_back(child);
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001713 } else if (child->get_parents().empty()) {
1714 unload_list.push_back(child);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001715 }
1716 }
1717 } else {
1718#if !defined(__work_around_b_24465209__)
1719 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
1720#else
1721 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
1722 for_each_dt_needed(si, [&] (const char* library_name) {
1723 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
1724 si->get_realpath(), library_name);
1725
1726 soinfo* needed = find_library(si->get_primary_namespace(),
1727 library_name, RTLD_NOLOAD, nullptr, nullptr);
1728
1729 if (needed != nullptr) {
1730 // Not found: for example if symlink was deleted between dlopen and dlclose
1731 // Since we cannot really handle errors at this point - print and continue.
1732 PRINT("warning: couldn't find %s needed by %s on unload.",
1733 library_name, si->get_realpath());
1734 return;
1735 } else if (local_unload_list.contains(needed)) {
1736 // already visited
1737 return;
1738 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
1739 // external group
1740 external_unload_list.push_back(needed);
1741 } else {
1742 // local group
1743 unload_list.push_front(needed);
1744 }
1745 });
1746#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001747 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001748 }
1749
1750 local_unload_list.for_each([](soinfo* si) {
1751 si->call_destructors();
1752 });
1753
1754 while ((si = local_unload_list.pop_front()) != nullptr) {
1755 notify_gdb_of_unload(si);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001756 get_cfi_shadow()->BeforeUnload(si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001757 soinfo_free(si);
1758 }
1759
1760 while ((si = external_unload_list.pop_front()) != nullptr) {
1761 soinfo_unload(si);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001762 }
1763}
1764
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001765static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
1766 if (sym_ver == nullptr) {
1767 return sym_name;
1768 }
1769
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001770 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001771}
1772
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001773static android_namespace_t* get_caller_namespace(soinfo* caller) {
1774 return caller != nullptr ? caller->get_primary_namespace() : g_anonymous_namespace;
1775}
1776
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001777void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001778 // Use basic string manipulation calls to avoid snprintf.
1779 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1780 // When debug malloc is enabled, this call returns 0. This in turn causes
1781 // snprintf to do nothing, which causes libraries to fail to load.
1782 // See b/17302493 for further details.
1783 // Once the above bug is fixed, this code can be modified to use
1784 // snprintf again.
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001785 const auto& default_ld_paths = g_default_namespace.get_default_library_paths();
1786
1787 size_t required_size = 0;
1788 for (const auto& path : default_ld_paths) {
1789 required_size += path.size() + 1;
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001790 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001791
1792 if (buffer_size < required_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001793 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001794 "buffer len %zu, required len %zu", buffer_size, required_size);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001795 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001796
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001797 char* end = buffer;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001798 for (size_t i = 0; i < default_ld_paths.size(); ++i) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001799 if (i > 0) *end++ = ':';
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001800 end = stpcpy(end, default_ld_paths[i].c_str());
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001801 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001802}
1803
Elliott Hughescade4c32012-12-20 14:42:14 -08001804void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08001805 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08001806}
1807
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001808static std::string android_dlextinfo_to_string(const android_dlextinfo* info) {
1809 if (info == nullptr) {
1810 return "(null)";
1811 }
1812
1813 return android::base::StringPrintf("[flags=0x%" PRIx64 ","
1814 " reserved_addr=%p,"
1815 " reserved_size=0x%zx,"
1816 " relro_fd=%d,"
1817 " library_fd=%d,"
1818 " library_fd_offset=0x%" PRIx64 ","
1819 " library_namespace=%s@%p]",
1820 info->flags,
1821 info->reserved_addr,
1822 info->reserved_size,
1823 info->relro_fd,
1824 info->library_fd,
1825 info->library_fd_offset,
1826 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
1827 (info->library_namespace != nullptr ?
1828 info->library_namespace->get_name() : "(null)") : "(n/a)",
1829 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
1830 info->library_namespace : nullptr);
1831}
1832
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08001833void* do_dlopen(const char* name, int flags,
1834 const android_dlextinfo* extinfo,
1835 const void* caller_addr) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001836 soinfo* const caller = find_containing_library(caller_addr);
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001837 android_namespace_t* ns = get_caller_namespace(caller);
1838
1839 LD_LOG(kLogDlopen,
1840 "dlopen(name=\"%s\", flags=0x%x, extinfo=%s, caller=\"%s\", caller_ns=%s@%p) ...",
1841 name,
1842 flags,
1843 android_dlextinfo_to_string(extinfo).c_str(),
1844 caller == nullptr ? "(null)" : caller->get_realpath(),
1845 ns == nullptr ? "(null)" : ns->get_name(),
1846 ns);
1847
1848 auto failure_guard = make_scope_guard([&]() {
1849 LD_LOG(kLogDlopen, "... dlopen failed: %s", linker_get_error_buffer());
1850 });
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001851
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001852 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001853 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001854 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001855 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001856
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001857 if (extinfo != nullptr) {
1858 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1859 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1860 return nullptr;
1861 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07001862
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001863 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001864 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001865 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
1866 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001867 return nullptr;
1868 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07001869
1870 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
1871 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
1872 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
1873 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
1874 return nullptr;
1875 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001876
1877 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
1878 if (extinfo->library_namespace == nullptr) {
1879 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
1880 return nullptr;
1881 }
1882 ns = extinfo->library_namespace;
1883 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001884 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001885
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001886 std::string asan_name_holder;
1887
1888 const char* translated_name = name;
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001889 if (g_is_asan && translated_name != nullptr && translated_name[0] == '/') {
1890 char translated_path[PATH_MAX];
1891 if (realpath(translated_name, translated_path) != nullptr) {
1892 if (file_is_in_dir(translated_path, kSystemLibDir)) {
1893 asan_name_holder = std::string(kAsanSystemLibDir) + "/" + basename(translated_path);
1894 if (file_exists(asan_name_holder.c_str())) {
1895 translated_name = asan_name_holder.c_str();
1896 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1897 }
1898 } else if (file_is_in_dir(translated_path, kVendorLibDir)) {
1899 asan_name_holder = std::string(kAsanVendorLibDir) + "/" + basename(translated_path);
1900 if (file_exists(asan_name_holder.c_str())) {
1901 translated_name = asan_name_holder.c_str();
1902 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1903 }
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001904 }
1905 }
1906 }
1907
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001908 ProtectedDataGuard guard;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001909 soinfo* si = find_library(ns, translated_name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001910 if (si != nullptr) {
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001911 void* handle = si->to_handle();
1912 LD_LOG(kLogDlopen,
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08001913 "... dlopen calling constructors: realpath=\"%s\", soname=\"%s\", handle=%p",
1914 si->get_realpath(), si->get_soname(), handle);
1915 si->call_constructors();
1916 failure_guard.disable();
1917 LD_LOG(kLogDlopen,
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001918 "... dlopen successful: realpath=\"%s\", soname=\"%s\", handle=%p",
1919 si->get_realpath(), si->get_soname(), handle);
1920 return handle;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001921 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001922
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001923 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001924}
1925
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001926int do_dladdr(const void* addr, Dl_info* info) {
1927 // Determine if this address can be found in any library currently mapped.
1928 soinfo* si = find_containing_library(addr);
1929 if (si == nullptr) {
1930 return 0;
1931 }
1932
1933 memset(info, 0, sizeof(Dl_info));
1934
1935 info->dli_fname = si->get_realpath();
1936 // Address at which the shared object is loaded.
1937 info->dli_fbase = reinterpret_cast<void*>(si->base);
1938
1939 // Determine if any symbol in the library contains the specified address.
1940 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
1941 if (sym != nullptr) {
1942 info->dli_sname = si->get_string(sym->st_name);
1943 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
1944 }
1945
1946 return 1;
1947}
1948
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001949static soinfo* soinfo_from_handle(void* handle) {
1950 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
1951 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
1952 if (it == g_soinfo_handles_map.end()) {
1953 return nullptr;
1954 } else {
1955 return it->second;
1956 }
1957 }
1958
1959 return static_cast<soinfo*>(handle);
1960}
1961
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08001962bool do_dlsym(void* handle,
1963 const char* sym_name,
1964 const char* sym_ver,
1965 const void* caller_addr,
1966 void** symbol) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001967#if !defined(__LP64__)
1968 if (handle == nullptr) {
1969 DL_ERR("dlsym failed: library handle is null");
1970 return false;
1971 }
1972#endif
1973
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001974 soinfo* found = nullptr;
1975 const ElfW(Sym)* sym = nullptr;
1976 soinfo* caller = find_containing_library(caller_addr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001977 android_namespace_t* ns = get_caller_namespace(caller);
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08001978 soinfo* si = nullptr;
1979 if (handle != RTLD_DEFAULT && handle != RTLD_NEXT) {
1980 si = soinfo_from_handle(handle);
1981 }
1982
1983 LD_LOG(kLogDlsym,
1984 "dlsym(handle=%p(\"%s\"), sym_name=\"%s\", sym_ver=\"%s\", caller=\"%s\", caller_ns=%s@%p) ...",
1985 handle,
1986 si != nullptr ? si->get_realpath() : "n/a",
1987 sym_name,
1988 sym_ver,
1989 caller == nullptr ? "(null)" : caller->get_realpath(),
1990 ns == nullptr ? "(null)" : ns->get_name(),
1991 ns);
1992
1993 auto failure_guard = make_scope_guard([&]() {
1994 LD_LOG(kLogDlsym, "... dlsym failed: %s", linker_get_error_buffer());
1995 });
1996
1997 if (sym_name == nullptr) {
1998 DL_ERR("dlsym failed: symbol name is null");
1999 return false;
2000 }
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002001
2002 version_info vi_instance;
2003 version_info* vi = nullptr;
2004
2005 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002006 vi_instance.name = sym_ver;
2007 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002008 vi = &vi_instance;
2009 }
2010
2011 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2012 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2013 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002014 if (si == nullptr) {
2015 DL_ERR("dlsym failed: invalid handle: %p", handle);
2016 return false;
2017 }
2018 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002019 }
2020
2021 if (sym != nullptr) {
2022 uint32_t bind = ELF_ST_BIND(sym->st_info);
2023
2024 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2025 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002026 failure_guard.disable();
2027 LD_LOG(kLogDlsym,
2028 "... dlsym successful: sym_name=\"%s\", sym_ver=\"%s\", found in=\"%s\", address=%p",
2029 sym_name, sym_ver, found->get_soname(), *symbol);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002030 return true;
2031 }
2032
2033 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2034 return false;
2035 }
2036
2037 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2038 return false;
2039}
2040
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002041int do_dlclose(void* handle) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002042 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002043 soinfo* si = soinfo_from_handle(handle);
2044 if (si == nullptr) {
2045 DL_ERR("invalid handle: %p", handle);
2046 return -1;
2047 }
2048
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002049 soinfo_unload(si);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002050 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002051}
2052
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002053bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path) {
2054 if (g_anonymous_namespace_initialized) {
2055 DL_ERR("anonymous namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002056 return false;
2057 }
2058
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002059 ProtectedDataGuard guard;
2060
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002061 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002062 // When the caller is nullptr - create_namespace will take global group
2063 // from the anonymous namespace, which is fine because anonymous namespace
2064 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002065 android_namespace_t* anon_ns =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002066 create_namespace(nullptr,
2067 "(anonymous)",
2068 nullptr,
2069 library_search_path,
2070 // TODO (dimitry): change to isolated eventually.
2071 ANDROID_NAMESPACE_TYPE_REGULAR,
2072 nullptr,
2073 &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002074
2075 if (anon_ns == nullptr) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002076 return false;
2077 }
2078
2079 if (!link_namespaces(anon_ns, &g_default_namespace, shared_lib_sonames)) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002080 return false;
2081 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08002082
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002083 g_anonymous_namespace = anon_ns;
Dimitry Ivanov3e0821d2017-03-07 11:02:10 -08002084 g_anonymous_namespace_initialized = true;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002085
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002086 return true;
2087}
2088
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002089static void add_soinfos_to_namespace(const soinfo_list_t& soinfos, android_namespace_t* ns) {
2090 ns->add_soinfos(soinfos);
2091 for (auto si : soinfos) {
2092 si->add_secondary_namespace(ns);
2093 }
2094}
2095
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002096android_namespace_t* create_namespace(const void* caller_addr,
2097 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002098 const char* ld_library_path,
2099 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002100 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002101 const char* permitted_when_isolated_path,
2102 android_namespace_t* parent_namespace) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002103 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002104 // if parent_namespace is nullptr -> set it to the caller namespace
2105 soinfo* caller_soinfo = find_containing_library(caller_addr);
2106
2107 parent_namespace = caller_soinfo != nullptr ?
2108 caller_soinfo->get_primary_namespace() :
2109 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002110 }
2111
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002112 ProtectedDataGuard guard;
2113 std::vector<std::string> ld_library_paths;
2114 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002115 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002116
2117 parse_path(ld_library_path, ":", &ld_library_paths);
2118 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002119 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002120
2121 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2122 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002123 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002124 ns->set_ld_library_paths(std::move(ld_library_paths));
2125 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002126 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002127
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002128 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002129 // If shared - clone the parent namespace
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002130 add_soinfos_to_namespace(parent_namespace->soinfo_list(), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002131 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002132 // If not shared - copy only the shared group
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002133 add_soinfos_to_namespace(get_shared_group(parent_namespace), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002134 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002135
2136 return ns;
2137}
2138
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002139bool link_namespaces(android_namespace_t* namespace_from,
2140 android_namespace_t* namespace_to,
2141 const char* shared_lib_sonames) {
2142 if (namespace_to == nullptr) {
2143 namespace_to = &g_default_namespace;
2144 }
2145
2146 if (namespace_from == nullptr) {
2147 DL_ERR("error linking namespaces: namespace_from is null.");
2148 return false;
2149 }
2150
2151 if (shared_lib_sonames == nullptr || shared_lib_sonames[0] == '\0') {
2152 DL_ERR("error linking namespaces \"%s\"->\"%s\": the list of shared libraries is empty.",
2153 namespace_from->get_name(), namespace_to->get_name());
2154 return false;
2155 }
2156
2157 auto sonames = android::base::Split(shared_lib_sonames, ":");
2158 std::unordered_set<std::string> sonames_set(sonames.begin(), sonames.end());
2159
2160 ProtectedDataGuard guard;
2161 namespace_from->add_linked_namespace(namespace_to, sonames_set);
2162
2163 return true;
2164}
2165
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002166ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002167 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2168 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2169 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002170 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2171 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002172
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002173 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002174}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002175
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002176const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2177 if (source_symver < 2 ||
2178 source_symver >= version_infos.size() ||
2179 version_infos[source_symver].name == nullptr) {
2180 return nullptr;
2181 }
2182
2183 return &version_infos[source_symver];
2184}
2185
2186void VersionTracker::add_version_info(size_t source_index,
2187 ElfW(Word) elf_hash,
2188 const char* ver_name,
2189 const soinfo* target_si) {
2190 if (source_index >= version_infos.size()) {
2191 version_infos.resize(source_index+1);
2192 }
2193
2194 version_infos[source_index].elf_hash = elf_hash;
2195 version_infos[source_index].name = ver_name;
2196 version_infos[source_index].target_si = target_si;
2197}
2198
2199bool VersionTracker::init_verneed(const soinfo* si_from) {
2200 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2201
2202 if (verneed_ptr == 0) {
2203 return true;
2204 }
2205
2206 size_t verneed_cnt = si_from->get_verneed_cnt();
2207
2208 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2209 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2210 size_t vernaux_offset = offset + verneed->vn_aux;
2211 offset += verneed->vn_next;
2212
2213 if (verneed->vn_version != 1) {
2214 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2215 return false;
2216 }
2217
2218 const char* target_soname = si_from->get_string(verneed->vn_file);
2219 // find it in dependencies
2220 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002221 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002222 });
2223
2224 if (target_si == nullptr) {
2225 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002226 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002227 return false;
2228 }
2229
2230 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2231 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2232 vernaux_offset += vernaux->vna_next;
2233
2234 const ElfW(Word) elf_hash = vernaux->vna_hash;
2235 const char* ver_name = si_from->get_string(vernaux->vna_name);
2236 ElfW(Half) source_index = vernaux->vna_other;
2237
2238 add_version_info(source_index, elf_hash, ver_name, target_si);
2239 }
2240 }
2241
2242 return true;
2243}
2244
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002245template <typename F>
2246static bool for_each_verdef(const soinfo* si, F functor) {
2247 if (!si->has_min_version(2)) {
2248 return true;
2249 }
2250
2251 uintptr_t verdef_ptr = si->get_verdef_ptr();
2252 if (verdef_ptr == 0) {
2253 return true;
2254 }
2255
2256 size_t offset = 0;
2257
2258 size_t verdef_cnt = si->get_verdef_cnt();
2259 for (size_t i = 0; i<verdef_cnt; ++i) {
2260 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
2261 size_t verdaux_offset = offset + verdef->vd_aux;
2262 offset += verdef->vd_next;
2263
2264 if (verdef->vd_version != 1) {
2265 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
2266 i, verdef->vd_version, si->get_realpath());
2267 return false;
2268 }
2269
2270 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
2271 // "this is the version of the file itself. It must not be used for
2272 // matching a symbol. It can be used to match references."
2273 //
2274 // http://www.akkadia.org/drepper/symbol-versioning
2275 continue;
2276 }
2277
2278 if (verdef->vd_cnt == 0) {
2279 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
2280 return false;
2281 }
2282
2283 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
2284
2285 if (functor(i, verdef, verdaux) == true) {
2286 break;
2287 }
2288 }
2289
2290 return true;
2291}
2292
2293bool find_verdef_version_index(const soinfo* si, const version_info* vi, ElfW(Versym)* versym) {
2294 if (vi == nullptr) {
2295 *versym = kVersymNotNeeded;
2296 return true;
2297 }
2298
2299 *versym = kVersymGlobal;
2300
2301 return for_each_verdef(si,
2302 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2303 if (verdef->vd_hash == vi->elf_hash &&
2304 strcmp(vi->name, si->get_string(verdaux->vda_name)) == 0) {
2305 *versym = verdef->vd_ndx;
2306 return true;
2307 }
2308
2309 return false;
2310 }
2311 );
2312}
2313
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002314bool VersionTracker::init_verdef(const soinfo* si_from) {
2315 return for_each_verdef(si_from,
2316 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2317 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2318 si_from->get_string(verdaux->vda_name), si_from);
2319 return false;
2320 }
2321 );
2322}
2323
2324bool VersionTracker::init(const soinfo* si_from) {
2325 if (!si_from->has_min_version(2)) {
2326 return true;
2327 }
2328
2329 return init_verneed(si_from) && init_verdef(si_from);
2330}
2331
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002332// TODO (dimitry): Methods below need to be moved out of soinfo
2333// and in more isolated file in order minimize dependencies on
2334// unnecessary object in the linker binary. Consider making them
2335// independent from soinfo (?).
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002336bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2337 const char* sym_name, const version_info** vi) {
2338 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2339 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2340
2341 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2342 *vi = version_tracker.get_version_info(sym_ver);
2343
2344 if (*vi == nullptr) {
2345 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002346 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002347 return false;
2348 }
2349 } else {
2350 // there is no version info
2351 *vi = nullptr;
2352 }
2353
2354 return true;
2355}
2356
Dimitry Ivanov576a3752016-08-09 06:58:55 -07002357#if !defined(__mips__)
2358#if defined(USE_RELA)
2359static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2360 return rela->r_addend;
2361}
2362#else
2363static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
2364 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2365 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
2366 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2367 }
2368 return 0;
2369}
2370#endif
2371
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002372template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002373bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2374 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002375 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2376 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002377 if (rel == nullptr) {
2378 return false;
2379 }
2380
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002381 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2382 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2383
2384 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002385 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002386 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002387 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002388
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002389 DEBUG("Processing \"%s\" relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002390 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002391 continue;
2392 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002393
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002394 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002395 soinfo* lsi = nullptr;
2396
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002397 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002398 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002399 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002400
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002401 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2402 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002403 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002404
2405 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2406 return false;
2407 }
2408
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002409 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002410 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002411 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002412 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002413 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002414 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002415 }
2416
2417 /* IHI0044C AAELF 4.5.1.1:
2418
2419 Libraries are not searched to resolve weak references.
2420 It is not an error for a weak reference to remain unsatisfied.
2421
2422 During linking, the value of an undefined weak reference is:
2423 - Zero if the relocation type is absolute
2424 - The address of the place if the relocation is pc-relative
2425 - The address of nominal base address if the relocation
2426 type is base-relative.
2427 */
2428
2429 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002430 case R_GENERIC_JUMP_SLOT:
2431 case R_GENERIC_GLOB_DAT:
2432 case R_GENERIC_RELATIVE:
2433 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002434#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002435 case R_AARCH64_ABS64:
2436 case R_AARCH64_ABS32:
2437 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002438#elif defined(__x86_64__)
2439 case R_X86_64_32:
2440 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002441#elif defined(__arm__)
2442 case R_ARM_ABS32:
2443#elif defined(__i386__)
2444 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002445#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002446 /*
2447 * The sym_addr was initialized to be zero above, or the relocation
2448 * code below does not care about value of sym_addr.
2449 * No need to do anything.
2450 */
2451 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002452#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002453 case R_X86_64_PC32:
2454 sym_addr = reloc;
2455 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002456#elif defined(__i386__)
2457 case R_386_PC32:
2458 sym_addr = reloc;
2459 break;
2460#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002461 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002462 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002463 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002464 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002465 } else { // We got a definition.
2466#if !defined(__LP64__)
2467 // When relocating dso with text_relocation .text segment is
2468 // not executable. We need to restore elf flags before resolving
2469 // STT_GNU_IFUNC symbol.
2470 bool protect_segments = has_text_relocations &&
2471 lsi == this &&
2472 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2473 if (protect_segments) {
2474 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2475 DL_ERR("can't protect segments for \"%s\": %s",
2476 get_realpath(), strerror(errno));
2477 return false;
2478 }
2479 }
2480#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002481 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002482#if !defined(__LP64__)
2483 if (protect_segments) {
2484 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2485 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2486 get_realpath(), strerror(errno));
2487 return false;
2488 }
2489 }
2490#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002491 }
2492 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002493 }
2494
2495 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002496 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002497 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002498 MARK(rel->r_offset);
2499 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2500 reinterpret_cast<void*>(reloc),
2501 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2502
2503 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002504 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002505 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002506 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002507 MARK(rel->r_offset);
2508 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2509 reinterpret_cast<void*>(reloc),
2510 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2511 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002512 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002513 case R_GENERIC_RELATIVE:
2514 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002515 MARK(rel->r_offset);
2516 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2517 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002518 reinterpret_cast<void*>(load_bias + addend));
2519 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002520 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002521 case R_GENERIC_IRELATIVE:
2522 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002523 MARK(rel->r_offset);
2524 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2525 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002526 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002527 {
2528#if !defined(__LP64__)
2529 // When relocating dso with text_relocation .text segment is
2530 // not executable. We need to restore elf flags for this
2531 // particular call.
2532 if (has_text_relocations) {
2533 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2534 DL_ERR("can't protect segments for \"%s\": %s",
2535 get_realpath(), strerror(errno));
2536 return false;
2537 }
2538 }
2539#endif
2540 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2541#if !defined(__LP64__)
2542 // Unprotect it afterwards...
2543 if (has_text_relocations) {
2544 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2545 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2546 get_realpath(), strerror(errno));
2547 return false;
2548 }
2549 }
2550#endif
2551 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2552 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002553 break;
2554
2555#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002556 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002557 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002558 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002559 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002560 reloc, sym_addr + addend, sym_name);
2561 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002562 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002563 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002564 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002565 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002566 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002567 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002568 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002569 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2570 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002571 if ((min_value <= (sym_addr + addend)) &&
2572 ((sym_addr + addend) <= max_value)) {
2573 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002574 } else {
2575 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002576 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002577 return false;
2578 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002579 }
2580 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002581 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002582 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002583 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002584 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002585 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002586 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002587 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2588 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002589 if ((min_value <= (sym_addr + addend)) &&
2590 ((sym_addr + addend) <= max_value)) {
2591 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002592 } else {
2593 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002594 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002595 return false;
2596 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002597 }
2598 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002599 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002600 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002601 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002602 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002603 reloc, sym_addr + addend, rel->r_offset, sym_name);
2604 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002605 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002606 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002607 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002608 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002609 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002610 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002611 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002612 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2613 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002614 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2615 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2616 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002617 } else {
2618 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002619 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002620 return false;
2621 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002622 }
2623 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002624 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002625 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002626 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002627 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002628 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002629 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002630 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2631 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002632 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2633 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2634 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002635 } else {
2636 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002637 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002638 return false;
2639 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002640 }
2641 break;
2642
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002643 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002644 /*
2645 * ET_EXEC is not supported so this should not happen.
2646 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002647 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002648 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002649 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002650 * R_AARCH64_COPY may only appear in executable objects where e_type is
2651 * set to ET_EXEC.
2652 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002653 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002654 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002655 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002656 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002657 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002658 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002659 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002660 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002661 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002662 break;
2663#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002664 case R_X86_64_32:
2665 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002666 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002667 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2668 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002669 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002670 break;
2671 case R_X86_64_64:
2672 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002673 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002674 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2675 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002676 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002677 break;
2678 case R_X86_64_PC32:
2679 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002680 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002681 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2682 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2683 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002684 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002685 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002686#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002687 case R_ARM_ABS32:
2688 count_relocation(kRelocAbsolute);
2689 MARK(rel->r_offset);
2690 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2691 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2692 break;
2693 case R_ARM_REL32:
2694 count_relocation(kRelocRelative);
2695 MARK(rel->r_offset);
2696 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2697 reloc, sym_addr, rel->r_offset, sym_name);
2698 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2699 break;
2700 case R_ARM_COPY:
2701 /*
2702 * ET_EXEC is not supported so this should not happen.
2703 *
2704 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2705 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002706 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002707 * R_ARM_COPY may only appear in executable objects where e_type is
2708 * set to ET_EXEC.
2709 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002710 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002711 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002712#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002713 case R_386_32:
2714 count_relocation(kRelocRelative);
2715 MARK(rel->r_offset);
2716 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2717 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2718 break;
2719 case R_386_PC32:
2720 count_relocation(kRelocRelative);
2721 MARK(rel->r_offset);
2722 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2723 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2724 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2725 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002726#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002727 default:
2728 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002729 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002730 }
2731 }
2732 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002733}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002734#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002735
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002736// An empty list of soinfos
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002737static soinfo_list_t g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002738
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002739bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002740 /* Extract dynamic section */
2741 ElfW(Word) dynamic_flags = 0;
2742 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002743
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002744 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002745 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002746 if (!relocating_linker) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002747 INFO("[ Linking \"%s\" ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002748 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002749 }
2750
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002751 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002752 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002753 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002754 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002755 return false;
2756 } else {
2757 if (!relocating_linker) {
2758 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002759 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002760 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002761
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002762#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002763 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2764 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002765#endif
2766
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002767 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002768 // Note that: "Except for the DT_NULL element at the end of the array,
2769 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2770 //
2771 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002772 uint32_t needed_count = 0;
2773 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2774 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2775 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2776 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002777 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002778 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002779 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002780
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002781 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002782 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2783 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2784 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2785 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002786 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002787
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002788 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002789 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002790 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002791 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2792 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002793
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002794 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002795 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002796 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002797 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
2798 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002799
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002800 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002801 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002802 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002803 return false;
2804 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002805 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002806
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002807 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002808 break;
2809
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002810 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002811 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002812 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002813
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002814 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002815 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002816 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002817
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002818 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002819 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002820 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002821
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002822 case DT_SYMENT:
2823 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002824 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
2825 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002826 return false;
2827 }
2828 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002829
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002830 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002831#if defined(USE_RELA)
2832 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002833 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002834 return false;
2835 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002836#else
2837 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002838 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002839 return false;
2840 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002841#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002842 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002843
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002844 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002845#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002846 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002847#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002848 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002849#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002850 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002851
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002852 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002853#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002854 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002855#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002856 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002857#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002858 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002859
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002860 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002861#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002862 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002863 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002864#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002865 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2866 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002867
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002868 case DT_DEBUG:
2869 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2870 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002871// FIXME: not working currently for N64
2872// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002873// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002874// read-only, but the DYNAMIC header claims it is writable.
2875#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002876 if ((dynamic_flags & PF_W) != 0) {
2877 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2878 }
Chris Dearman99186652014-02-06 20:36:51 -08002879#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002880 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002881#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002882 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002883 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002884 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002885
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002886 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002887 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002888 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002889
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002890 case DT_ANDROID_RELA:
2891 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2892 break;
2893
2894 case DT_ANDROID_RELASZ:
2895 android_relocs_size_ = d->d_un.d_val;
2896 break;
2897
2898 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002899 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002900 return false;
2901
2902 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002903 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002904 return false;
2905
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002906 case DT_RELAENT:
2907 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002908 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002909 return false;
2910 }
2911 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002912
2913 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002914 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002915 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002916
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002917 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002918 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002919 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002920
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002921 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002922 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002923 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002924
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002925#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002926 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002927 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002928 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002929
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002930 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002931 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002932 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002933
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002934 case DT_RELENT:
2935 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002936 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002937 return false;
2938 }
2939 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002940
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002941 case DT_ANDROID_REL:
2942 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2943 break;
2944
2945 case DT_ANDROID_RELSZ:
2946 android_relocs_size_ = d->d_un.d_val;
2947 break;
2948
2949 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002950 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002951 return false;
2952
2953 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002954 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002955 return false;
2956
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002957 // "Indicates that all RELATIVE relocations have been concatenated together,
2958 // and specifies the RELATIVE relocation count."
2959 //
2960 // TODO: Spec also mentions that this can be used to optimize relocation process;
2961 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002962 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002963 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002964
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002965 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002966 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002967 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002968
2969 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002970 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002971 return false;
2972
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002973#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002974 case DT_INIT:
Dimitry Ivanov55437462016-07-20 15:33:07 -07002975 init_func_ = reinterpret_cast<linker_ctor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002976 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002977 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002978
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002979 case DT_FINI:
Dimitry Ivanov55437462016-07-20 15:33:07 -07002980 fini_func_ = reinterpret_cast<linker_dtor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002981 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002982 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002983
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002984 case DT_INIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07002985 init_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002986 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002987 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002988
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002989 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002990 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002991 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002992
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002993 case DT_FINI_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07002994 fini_array_ = reinterpret_cast<linker_dtor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002995 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002996 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002997
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002998 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002999 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003000 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003001
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003002 case DT_PREINIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003003 preinit_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003004 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003005 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003006
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003007 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003008 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003009 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003010
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003011 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003012#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003013 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003014 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003015#else
3016 has_text_relocations = true;
3017 break;
3018#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003019
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003020 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003021 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003022 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003023
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003024 case DT_NEEDED:
3025 ++needed_count;
3026 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003027
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003028 case DT_FLAGS:
3029 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003030#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003031 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003032 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003033#else
3034 has_text_relocations = true;
3035#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003036 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003037 if (d->d_un.d_val & DF_SYMBOLIC) {
3038 has_DT_SYMBOLIC = true;
3039 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003040 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003041
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003042 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003043 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003044
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003045 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003046 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 -07003047 }
3048 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003049#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003050 case DT_MIPS_RLD_MAP:
3051 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3052 {
3053 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3054 *dp = &_r_debug;
3055 }
3056 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003057 case DT_MIPS_RLD_MAP_REL:
3058 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003059 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003060 r_debug** dp = reinterpret_cast<r_debug**>(
3061 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003062 *dp = &_r_debug;
3063 }
3064 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003065
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003066 case DT_MIPS_RLD_VERSION:
3067 case DT_MIPS_FLAGS:
3068 case DT_MIPS_BASE_ADDRESS:
3069 case DT_MIPS_UNREFEXTNO:
3070 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003071
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003072 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003073 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003074 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003075
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003076 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003077 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003078 break;
3079
3080 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003081 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003082 break;
3083#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003084 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3085 case DT_BIND_NOW:
3086 break;
3087
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003088 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003089 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3090 break;
3091
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003092 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003093 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3094 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003095 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003096 verdef_cnt_ = d->d_un.d_val;
3097 break;
3098
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003099 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003100 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3101 break;
3102
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003103 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003104 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003105 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003106
Evgenii Stepanov68650822015-06-10 13:38:39 -07003107 case DT_RUNPATH:
3108 // this is parsed after we have strtab initialized (see below).
3109 break;
3110
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003111 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003112 if (!relocating_linker) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003113 DL_WARN("\"%s\" unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003114 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3115 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003116 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003117 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003118 }
3119
Duane Sandbc425c72015-06-01 16:29:14 -07003120#if defined(__mips__) && !defined(__LP64__)
3121 if (!mips_check_and_adjust_fp_modes()) {
3122 return false;
3123 }
3124#endif
3125
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003126 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003127 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003128
3129 // Sanity checks.
3130 if (relocating_linker && needed_count != 0) {
3131 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3132 return false;
3133 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003134 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003135 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003136 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003137 return false;
3138 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003139 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003140 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003141 return false;
3142 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003143 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003144 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003145 return false;
3146 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003147
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003148 // second pass - parse entries relying on strtab
3149 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003150 switch (d->d_tag) {
3151 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003152 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003153 break;
3154 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003155 set_dt_runpath(get_string(d->d_un.d_val));
3156 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003157 }
3158 }
3159
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003160 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003161 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003162 // because they can't find dt_needed library by soname.
3163 // This workaround should keep them working. (applies only
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003164 // for apps targeting sdk version < M). Make an exception for
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003165 // the main executable and linker; they do not need to have dt_soname
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003166 if (soname_ == nullptr &&
3167 this != solist_get_somain() &&
3168 (flags_ & FLAG_LINKER) == 0 &&
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003169 get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003170 soname_ = basename(realpath_.c_str());
3171 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3172 get_realpath(), soname_);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003173 // 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 -07003174 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003175 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003176}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003177
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003178bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3179 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003180
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003181 local_group_root_ = local_group.front();
3182 if (local_group_root_ == nullptr) {
3183 local_group_root_ = this;
3184 }
3185
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003186 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3187 target_sdk_version_ = get_application_target_sdk_version();
3188 }
3189
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003190 VersionTracker version_tracker;
3191
3192 if (!version_tracker.init(this)) {
3193 return false;
3194 }
3195
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003196#if !defined(__LP64__)
3197 if (has_text_relocations) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003198 // Fail if app is targeting M or above.
3199 if (get_application_target_sdk_version() >= __ANDROID_API_M__) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003200 DL_ERR_AND_LOG("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003201 return false;
3202 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003203 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003204 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003205 DL_WARN("\"%s\" has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003206 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003207 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003208 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3209 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003210 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003211 return false;
3212 }
3213 }
3214#endif
3215
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003216 if (android_relocs_ != nullptr) {
3217 // check signature
3218 if (android_relocs_size_ > 3 &&
3219 android_relocs_[0] == 'A' &&
3220 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003221 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003222 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003223 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003224
3225 bool relocated = false;
3226 const uint8_t* packed_relocs = android_relocs_ + 4;
3227 const size_t packed_relocs_size = android_relocs_size_ - 4;
3228
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003229 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003230 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003231 packed_reloc_iterator<sleb128_decoder>(
3232 sleb128_decoder(packed_relocs, packed_relocs_size)),
3233 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003234
3235 if (!relocated) {
3236 return false;
3237 }
3238 } else {
3239 DL_ERR("bad android relocation header.");
3240 return false;
3241 }
3242 }
3243
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003244#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003245 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003246 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003247 if (!relocate(version_tracker,
3248 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003249 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003250 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003251 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003252 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003253 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003254 if (!relocate(version_tracker,
3255 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003256 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003257 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003258 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003259#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003260 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003261 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003262 if (!relocate(version_tracker,
3263 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003264 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003265 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003266 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003267 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003268 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003269 if (!relocate(version_tracker,
3270 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003271 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003272 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003273 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003274#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003275
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003276#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003277 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003278 return false;
3279 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003280#endif
3281
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003282 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003283
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003284#if !defined(__LP64__)
3285 if (has_text_relocations) {
3286 // All relocations are done, we can protect our segments back to read-only.
3287 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3288 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003289 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003290 return false;
3291 }
3292 }
3293#endif
3294
Mingwei Shibe910522015-11-12 07:02:14 +00003295 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3296 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3297 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003298 return false;
3299 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003300
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003301 /* Handle serializing/sharing the RELRO segment */
3302 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3303 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3304 extinfo->relro_fd) < 0) {
3305 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003306 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003307 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003308 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003309 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3310 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3311 extinfo->relro_fd) < 0) {
3312 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003313 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003314 return false;
3315 }
3316 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003317
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003318 notify_gdb_of_load(this);
3319 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003320}
3321
Mingwei Shibe910522015-11-12 07:02:14 +00003322bool soinfo::protect_relro() {
3323 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3324 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3325 get_realpath(), strerror(errno));
3326 return false;
3327 }
3328 return true;
3329}
3330
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003331void init_default_namespace() {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003332 g_default_namespace.set_name("(default)");
3333 g_default_namespace.set_isolated(false);
3334
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003335 soinfo* somain = solist_get_somain();
3336
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003337 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3338 somain->load_bias);
3339 const char* bname = basename(interp);
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003340
3341 bool is_asan = bname != nullptr &&
3342 (strcmp(bname, "linker_asan") == 0 ||
3343 strcmp(bname, "linker_asan64") == 0);
3344 auto default_ld_paths = is_asan ? kAsanDefaultLdPaths : kDefaultLdPaths;
3345 g_is_asan = is_asan;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003346
neo.chae2589f9d2016-10-04 11:00:27 +09003347 char real_path[PATH_MAX];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003348 std::vector<std::string> ld_default_paths;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003349 for (size_t i = 0; default_ld_paths[i] != nullptr; ++i) {
3350 if (realpath(default_ld_paths[i], real_path) != nullptr) {
neo.chae2589f9d2016-10-04 11:00:27 +09003351 ld_default_paths.push_back(real_path);
3352 } else {
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003353 ld_default_paths.push_back(default_ld_paths[i]);
neo.chae2589f9d2016-10-04 11:00:27 +09003354 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003355 }
3356
3357 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003358};