blob: 846cb88f7ba02ae85b263b606d6424c2261b83e1 [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) {
Evgenii Stepanov5b715002016-10-03 15:09:28 -07001892 if (file_is_under_dir(translated_path, kSystemLibDir)) {
1893 asan_name_holder = std::string(kAsanSystemLibDir) + "/" +
1894 (translated_path + strlen(kSystemLibDir) + 1);
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001895 if (file_exists(asan_name_holder.c_str())) {
1896 translated_name = asan_name_holder.c_str();
1897 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1898 }
Evgenii Stepanov5b715002016-10-03 15:09:28 -07001899 } else if (file_is_under_dir(translated_path, kVendorLibDir)) {
1900 asan_name_holder = std::string(kAsanVendorLibDir) + "/" +
1901 (translated_path + strlen(kVendorLibDir) + 1);
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001902 if (file_exists(asan_name_holder.c_str())) {
1903 translated_name = asan_name_holder.c_str();
1904 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1905 }
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001906 }
1907 }
1908 }
1909
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001910 ProtectedDataGuard guard;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001911 soinfo* si = find_library(ns, translated_name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001912 if (si != nullptr) {
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001913 void* handle = si->to_handle();
1914 LD_LOG(kLogDlopen,
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08001915 "... dlopen calling constructors: realpath=\"%s\", soname=\"%s\", handle=%p",
1916 si->get_realpath(), si->get_soname(), handle);
1917 si->call_constructors();
1918 failure_guard.disable();
1919 LD_LOG(kLogDlopen,
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001920 "... dlopen successful: realpath=\"%s\", soname=\"%s\", handle=%p",
1921 si->get_realpath(), si->get_soname(), handle);
1922 return handle;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001923 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001924
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001925 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001926}
1927
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001928int do_dladdr(const void* addr, Dl_info* info) {
1929 // Determine if this address can be found in any library currently mapped.
1930 soinfo* si = find_containing_library(addr);
1931 if (si == nullptr) {
1932 return 0;
1933 }
1934
1935 memset(info, 0, sizeof(Dl_info));
1936
1937 info->dli_fname = si->get_realpath();
1938 // Address at which the shared object is loaded.
1939 info->dli_fbase = reinterpret_cast<void*>(si->base);
1940
1941 // Determine if any symbol in the library contains the specified address.
1942 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
1943 if (sym != nullptr) {
1944 info->dli_sname = si->get_string(sym->st_name);
1945 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
1946 }
1947
1948 return 1;
1949}
1950
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001951static soinfo* soinfo_from_handle(void* handle) {
1952 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
1953 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
1954 if (it == g_soinfo_handles_map.end()) {
1955 return nullptr;
1956 } else {
1957 return it->second;
1958 }
1959 }
1960
1961 return static_cast<soinfo*>(handle);
1962}
1963
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08001964bool do_dlsym(void* handle,
1965 const char* sym_name,
1966 const char* sym_ver,
1967 const void* caller_addr,
1968 void** symbol) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001969#if !defined(__LP64__)
1970 if (handle == nullptr) {
1971 DL_ERR("dlsym failed: library handle is null");
1972 return false;
1973 }
1974#endif
1975
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001976 soinfo* found = nullptr;
1977 const ElfW(Sym)* sym = nullptr;
1978 soinfo* caller = find_containing_library(caller_addr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001979 android_namespace_t* ns = get_caller_namespace(caller);
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08001980 soinfo* si = nullptr;
1981 if (handle != RTLD_DEFAULT && handle != RTLD_NEXT) {
1982 si = soinfo_from_handle(handle);
1983 }
1984
1985 LD_LOG(kLogDlsym,
1986 "dlsym(handle=%p(\"%s\"), sym_name=\"%s\", sym_ver=\"%s\", caller=\"%s\", caller_ns=%s@%p) ...",
1987 handle,
1988 si != nullptr ? si->get_realpath() : "n/a",
1989 sym_name,
1990 sym_ver,
1991 caller == nullptr ? "(null)" : caller->get_realpath(),
1992 ns == nullptr ? "(null)" : ns->get_name(),
1993 ns);
1994
1995 auto failure_guard = make_scope_guard([&]() {
1996 LD_LOG(kLogDlsym, "... dlsym failed: %s", linker_get_error_buffer());
1997 });
1998
1999 if (sym_name == nullptr) {
2000 DL_ERR("dlsym failed: symbol name is null");
2001 return false;
2002 }
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002003
2004 version_info vi_instance;
2005 version_info* vi = nullptr;
2006
2007 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002008 vi_instance.name = sym_ver;
2009 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002010 vi = &vi_instance;
2011 }
2012
2013 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2014 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2015 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002016 if (si == nullptr) {
2017 DL_ERR("dlsym failed: invalid handle: %p", handle);
2018 return false;
2019 }
2020 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002021 }
2022
2023 if (sym != nullptr) {
2024 uint32_t bind = ELF_ST_BIND(sym->st_info);
2025
2026 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2027 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002028 failure_guard.disable();
2029 LD_LOG(kLogDlsym,
2030 "... dlsym successful: sym_name=\"%s\", sym_ver=\"%s\", found in=\"%s\", address=%p",
2031 sym_name, sym_ver, found->get_soname(), *symbol);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002032 return true;
2033 }
2034
2035 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2036 return false;
2037 }
2038
2039 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2040 return false;
2041}
2042
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002043int do_dlclose(void* handle) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002044 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002045 soinfo* si = soinfo_from_handle(handle);
2046 if (si == nullptr) {
2047 DL_ERR("invalid handle: %p", handle);
2048 return -1;
2049 }
2050
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002051 soinfo_unload(si);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002052 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002053}
2054
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002055bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path) {
2056 if (g_anonymous_namespace_initialized) {
2057 DL_ERR("anonymous namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002058 return false;
2059 }
2060
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002061 ProtectedDataGuard guard;
2062
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002063 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002064 // When the caller is nullptr - create_namespace will take global group
2065 // from the anonymous namespace, which is fine because anonymous namespace
2066 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002067 android_namespace_t* anon_ns =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002068 create_namespace(nullptr,
2069 "(anonymous)",
2070 nullptr,
2071 library_search_path,
2072 // TODO (dimitry): change to isolated eventually.
2073 ANDROID_NAMESPACE_TYPE_REGULAR,
2074 nullptr,
2075 &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002076
2077 if (anon_ns == nullptr) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002078 return false;
2079 }
2080
2081 if (!link_namespaces(anon_ns, &g_default_namespace, shared_lib_sonames)) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002082 return false;
2083 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08002084
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002085 g_anonymous_namespace = anon_ns;
Dimitry Ivanov3e0821d2017-03-07 11:02:10 -08002086 g_anonymous_namespace_initialized = true;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002087
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002088 return true;
2089}
2090
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002091static void add_soinfos_to_namespace(const soinfo_list_t& soinfos, android_namespace_t* ns) {
2092 ns->add_soinfos(soinfos);
2093 for (auto si : soinfos) {
2094 si->add_secondary_namespace(ns);
2095 }
2096}
2097
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002098android_namespace_t* create_namespace(const void* caller_addr,
2099 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002100 const char* ld_library_path,
2101 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002102 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002103 const char* permitted_when_isolated_path,
2104 android_namespace_t* parent_namespace) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002105 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002106 // if parent_namespace is nullptr -> set it to the caller namespace
2107 soinfo* caller_soinfo = find_containing_library(caller_addr);
2108
2109 parent_namespace = caller_soinfo != nullptr ?
2110 caller_soinfo->get_primary_namespace() :
2111 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002112 }
2113
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002114 ProtectedDataGuard guard;
2115 std::vector<std::string> ld_library_paths;
2116 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002117 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002118
2119 parse_path(ld_library_path, ":", &ld_library_paths);
2120 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002121 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002122
2123 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2124 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002125 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002126 ns->set_ld_library_paths(std::move(ld_library_paths));
2127 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002128 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002129
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002130 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002131 // If shared - clone the parent namespace
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002132 add_soinfos_to_namespace(parent_namespace->soinfo_list(), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002133 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002134 // If not shared - copy only the shared group
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002135 add_soinfos_to_namespace(get_shared_group(parent_namespace), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002136 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002137
2138 return ns;
2139}
2140
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002141bool link_namespaces(android_namespace_t* namespace_from,
2142 android_namespace_t* namespace_to,
2143 const char* shared_lib_sonames) {
2144 if (namespace_to == nullptr) {
2145 namespace_to = &g_default_namespace;
2146 }
2147
2148 if (namespace_from == nullptr) {
2149 DL_ERR("error linking namespaces: namespace_from is null.");
2150 return false;
2151 }
2152
2153 if (shared_lib_sonames == nullptr || shared_lib_sonames[0] == '\0') {
2154 DL_ERR("error linking namespaces \"%s\"->\"%s\": the list of shared libraries is empty.",
2155 namespace_from->get_name(), namespace_to->get_name());
2156 return false;
2157 }
2158
2159 auto sonames = android::base::Split(shared_lib_sonames, ":");
2160 std::unordered_set<std::string> sonames_set(sonames.begin(), sonames.end());
2161
2162 ProtectedDataGuard guard;
2163 namespace_from->add_linked_namespace(namespace_to, sonames_set);
2164
2165 return true;
2166}
2167
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002168ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002169 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2170 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2171 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002172 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2173 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002174
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002175 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002176}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002177
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002178const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2179 if (source_symver < 2 ||
2180 source_symver >= version_infos.size() ||
2181 version_infos[source_symver].name == nullptr) {
2182 return nullptr;
2183 }
2184
2185 return &version_infos[source_symver];
2186}
2187
2188void VersionTracker::add_version_info(size_t source_index,
2189 ElfW(Word) elf_hash,
2190 const char* ver_name,
2191 const soinfo* target_si) {
2192 if (source_index >= version_infos.size()) {
2193 version_infos.resize(source_index+1);
2194 }
2195
2196 version_infos[source_index].elf_hash = elf_hash;
2197 version_infos[source_index].name = ver_name;
2198 version_infos[source_index].target_si = target_si;
2199}
2200
2201bool VersionTracker::init_verneed(const soinfo* si_from) {
2202 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2203
2204 if (verneed_ptr == 0) {
2205 return true;
2206 }
2207
2208 size_t verneed_cnt = si_from->get_verneed_cnt();
2209
2210 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2211 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2212 size_t vernaux_offset = offset + verneed->vn_aux;
2213 offset += verneed->vn_next;
2214
2215 if (verneed->vn_version != 1) {
2216 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2217 return false;
2218 }
2219
2220 const char* target_soname = si_from->get_string(verneed->vn_file);
2221 // find it in dependencies
2222 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002223 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002224 });
2225
2226 if (target_si == nullptr) {
2227 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002228 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002229 return false;
2230 }
2231
2232 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2233 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2234 vernaux_offset += vernaux->vna_next;
2235
2236 const ElfW(Word) elf_hash = vernaux->vna_hash;
2237 const char* ver_name = si_from->get_string(vernaux->vna_name);
2238 ElfW(Half) source_index = vernaux->vna_other;
2239
2240 add_version_info(source_index, elf_hash, ver_name, target_si);
2241 }
2242 }
2243
2244 return true;
2245}
2246
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002247template <typename F>
2248static bool for_each_verdef(const soinfo* si, F functor) {
2249 if (!si->has_min_version(2)) {
2250 return true;
2251 }
2252
2253 uintptr_t verdef_ptr = si->get_verdef_ptr();
2254 if (verdef_ptr == 0) {
2255 return true;
2256 }
2257
2258 size_t offset = 0;
2259
2260 size_t verdef_cnt = si->get_verdef_cnt();
2261 for (size_t i = 0; i<verdef_cnt; ++i) {
2262 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
2263 size_t verdaux_offset = offset + verdef->vd_aux;
2264 offset += verdef->vd_next;
2265
2266 if (verdef->vd_version != 1) {
2267 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
2268 i, verdef->vd_version, si->get_realpath());
2269 return false;
2270 }
2271
2272 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
2273 // "this is the version of the file itself. It must not be used for
2274 // matching a symbol. It can be used to match references."
2275 //
2276 // http://www.akkadia.org/drepper/symbol-versioning
2277 continue;
2278 }
2279
2280 if (verdef->vd_cnt == 0) {
2281 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
2282 return false;
2283 }
2284
2285 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
2286
2287 if (functor(i, verdef, verdaux) == true) {
2288 break;
2289 }
2290 }
2291
2292 return true;
2293}
2294
2295bool find_verdef_version_index(const soinfo* si, const version_info* vi, ElfW(Versym)* versym) {
2296 if (vi == nullptr) {
2297 *versym = kVersymNotNeeded;
2298 return true;
2299 }
2300
2301 *versym = kVersymGlobal;
2302
2303 return for_each_verdef(si,
2304 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2305 if (verdef->vd_hash == vi->elf_hash &&
2306 strcmp(vi->name, si->get_string(verdaux->vda_name)) == 0) {
2307 *versym = verdef->vd_ndx;
2308 return true;
2309 }
2310
2311 return false;
2312 }
2313 );
2314}
2315
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002316bool VersionTracker::init_verdef(const soinfo* si_from) {
2317 return for_each_verdef(si_from,
2318 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2319 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2320 si_from->get_string(verdaux->vda_name), si_from);
2321 return false;
2322 }
2323 );
2324}
2325
2326bool VersionTracker::init(const soinfo* si_from) {
2327 if (!si_from->has_min_version(2)) {
2328 return true;
2329 }
2330
2331 return init_verneed(si_from) && init_verdef(si_from);
2332}
2333
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002334// TODO (dimitry): Methods below need to be moved out of soinfo
2335// and in more isolated file in order minimize dependencies on
2336// unnecessary object in the linker binary. Consider making them
2337// independent from soinfo (?).
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002338bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2339 const char* sym_name, const version_info** vi) {
2340 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2341 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2342
2343 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2344 *vi = version_tracker.get_version_info(sym_ver);
2345
2346 if (*vi == nullptr) {
2347 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002348 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002349 return false;
2350 }
2351 } else {
2352 // there is no version info
2353 *vi = nullptr;
2354 }
2355
2356 return true;
2357}
2358
Dimitry Ivanov576a3752016-08-09 06:58:55 -07002359#if !defined(__mips__)
2360#if defined(USE_RELA)
2361static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2362 return rela->r_addend;
2363}
2364#else
2365static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
2366 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2367 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
2368 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2369 }
2370 return 0;
2371}
2372#endif
2373
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002374template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002375bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2376 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002377 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2378 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002379 if (rel == nullptr) {
2380 return false;
2381 }
2382
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002383 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2384 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2385
2386 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002387 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002388 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002389 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002390
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002391 DEBUG("Processing \"%s\" relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002392 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002393 continue;
2394 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002395
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002396 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002397 soinfo* lsi = nullptr;
2398
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002399 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002400 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002401 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002402
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002403 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2404 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002405 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002406
2407 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2408 return false;
2409 }
2410
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002411 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002412 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002413 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002414 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002415 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002416 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002417 }
2418
2419 /* IHI0044C AAELF 4.5.1.1:
2420
2421 Libraries are not searched to resolve weak references.
2422 It is not an error for a weak reference to remain unsatisfied.
2423
2424 During linking, the value of an undefined weak reference is:
2425 - Zero if the relocation type is absolute
2426 - The address of the place if the relocation is pc-relative
2427 - The address of nominal base address if the relocation
2428 type is base-relative.
2429 */
2430
2431 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002432 case R_GENERIC_JUMP_SLOT:
2433 case R_GENERIC_GLOB_DAT:
2434 case R_GENERIC_RELATIVE:
2435 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002436#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002437 case R_AARCH64_ABS64:
2438 case R_AARCH64_ABS32:
2439 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002440#elif defined(__x86_64__)
2441 case R_X86_64_32:
2442 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002443#elif defined(__arm__)
2444 case R_ARM_ABS32:
2445#elif defined(__i386__)
2446 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002447#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002448 /*
2449 * The sym_addr was initialized to be zero above, or the relocation
2450 * code below does not care about value of sym_addr.
2451 * No need to do anything.
2452 */
2453 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002454#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002455 case R_X86_64_PC32:
2456 sym_addr = reloc;
2457 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002458#elif defined(__i386__)
2459 case R_386_PC32:
2460 sym_addr = reloc;
2461 break;
2462#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002463 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002464 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002465 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002466 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002467 } else { // We got a definition.
2468#if !defined(__LP64__)
2469 // When relocating dso with text_relocation .text segment is
2470 // not executable. We need to restore elf flags before resolving
2471 // STT_GNU_IFUNC symbol.
2472 bool protect_segments = has_text_relocations &&
2473 lsi == this &&
2474 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2475 if (protect_segments) {
2476 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2477 DL_ERR("can't protect segments for \"%s\": %s",
2478 get_realpath(), strerror(errno));
2479 return false;
2480 }
2481 }
2482#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002483 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002484#if !defined(__LP64__)
2485 if (protect_segments) {
2486 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2487 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2488 get_realpath(), strerror(errno));
2489 return false;
2490 }
2491 }
2492#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002493 }
2494 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002495 }
2496
2497 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002498 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002499 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002500 MARK(rel->r_offset);
2501 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2502 reinterpret_cast<void*>(reloc),
2503 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2504
2505 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002506 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002507 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002508 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002509 MARK(rel->r_offset);
2510 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2511 reinterpret_cast<void*>(reloc),
2512 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2513 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002514 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002515 case R_GENERIC_RELATIVE:
2516 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002517 MARK(rel->r_offset);
2518 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2519 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002520 reinterpret_cast<void*>(load_bias + addend));
2521 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002522 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002523 case R_GENERIC_IRELATIVE:
2524 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002525 MARK(rel->r_offset);
2526 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2527 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002528 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002529 {
2530#if !defined(__LP64__)
2531 // When relocating dso with text_relocation .text segment is
2532 // not executable. We need to restore elf flags for this
2533 // particular call.
2534 if (has_text_relocations) {
2535 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2536 DL_ERR("can't protect segments for \"%s\": %s",
2537 get_realpath(), strerror(errno));
2538 return false;
2539 }
2540 }
2541#endif
2542 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2543#if !defined(__LP64__)
2544 // Unprotect it afterwards...
2545 if (has_text_relocations) {
2546 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2547 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2548 get_realpath(), strerror(errno));
2549 return false;
2550 }
2551 }
2552#endif
2553 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2554 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002555 break;
2556
2557#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002558 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002559 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002560 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002561 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002562 reloc, sym_addr + addend, sym_name);
2563 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002564 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002565 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002566 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002567 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002568 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002569 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002570 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002571 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2572 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002573 if ((min_value <= (sym_addr + addend)) &&
2574 ((sym_addr + addend) <= max_value)) {
2575 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002576 } else {
2577 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002578 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002579 return false;
2580 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002581 }
2582 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002583 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002584 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002585 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002586 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002587 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002588 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002589 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2590 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002591 if ((min_value <= (sym_addr + addend)) &&
2592 ((sym_addr + addend) <= max_value)) {
2593 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002594 } else {
2595 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002596 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002597 return false;
2598 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002599 }
2600 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002601 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002602 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002603 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002604 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002605 reloc, sym_addr + addend, rel->r_offset, sym_name);
2606 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002607 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002608 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002609 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002610 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002611 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002612 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002613 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002614 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2615 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002616 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2617 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2618 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002619 } else {
2620 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002621 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002622 return false;
2623 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002624 }
2625 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002626 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002627 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002628 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002629 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002630 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002631 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002632 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2633 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002634 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2635 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2636 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002637 } else {
2638 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002639 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002640 return false;
2641 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002642 }
2643 break;
2644
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002645 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002646 /*
2647 * ET_EXEC is not supported so this should not happen.
2648 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002649 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002650 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002651 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002652 * R_AARCH64_COPY may only appear in executable objects where e_type is
2653 * set to ET_EXEC.
2654 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002655 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002656 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002657 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002658 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002659 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002660 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002661 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002662 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002663 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002664 break;
2665#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002666 case R_X86_64_32:
2667 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002668 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002669 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2670 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002671 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002672 break;
2673 case R_X86_64_64:
2674 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002675 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002676 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2677 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002678 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002679 break;
2680 case R_X86_64_PC32:
2681 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002682 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002683 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2684 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2685 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002686 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002687 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002688#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002689 case R_ARM_ABS32:
2690 count_relocation(kRelocAbsolute);
2691 MARK(rel->r_offset);
2692 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2693 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2694 break;
2695 case R_ARM_REL32:
2696 count_relocation(kRelocRelative);
2697 MARK(rel->r_offset);
2698 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2699 reloc, sym_addr, rel->r_offset, sym_name);
2700 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2701 break;
2702 case R_ARM_COPY:
2703 /*
2704 * ET_EXEC is not supported so this should not happen.
2705 *
2706 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2707 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002708 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002709 * R_ARM_COPY may only appear in executable objects where e_type is
2710 * set to ET_EXEC.
2711 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002712 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002713 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002714#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002715 case R_386_32:
2716 count_relocation(kRelocRelative);
2717 MARK(rel->r_offset);
2718 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2719 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2720 break;
2721 case R_386_PC32:
2722 count_relocation(kRelocRelative);
2723 MARK(rel->r_offset);
2724 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2725 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2726 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2727 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002728#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002729 default:
2730 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002731 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002732 }
2733 }
2734 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002735}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002736#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002737
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002738// An empty list of soinfos
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002739static soinfo_list_t g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002740
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002741bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002742 /* Extract dynamic section */
2743 ElfW(Word) dynamic_flags = 0;
2744 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002745
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002746 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002747 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002748 if (!relocating_linker) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002749 INFO("[ Linking \"%s\" ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002750 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002751 }
2752
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002753 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002754 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002755 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002756 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002757 return false;
2758 } else {
2759 if (!relocating_linker) {
2760 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002761 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002762 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002763
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002764#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002765 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2766 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002767#endif
2768
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002769 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002770 // Note that: "Except for the DT_NULL element at the end of the array,
2771 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2772 //
2773 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002774 uint32_t needed_count = 0;
2775 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2776 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2777 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2778 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002779 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002780 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002781 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002782
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002783 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002784 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2785 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2786 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2787 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002788 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002789
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002790 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002791 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002792 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002793 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2794 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002795
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002796 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002797 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002798 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002799 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
2800 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002801
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002802 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002803 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002804 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002805 return false;
2806 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002807 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002808
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002809 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002810 break;
2811
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002812 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002813 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002814 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002815
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002816 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002817 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002818 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002819
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002820 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002821 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002822 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002823
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002824 case DT_SYMENT:
2825 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002826 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
2827 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002828 return false;
2829 }
2830 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002831
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002832 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002833#if defined(USE_RELA)
2834 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002835 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002836 return false;
2837 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002838#else
2839 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002840 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002841 return false;
2842 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002843#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002844 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002845
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002846 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002847#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002848 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002849#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002850 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002851#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002852 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002853
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002854 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002855#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002856 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002857#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002858 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002859#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002860 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002861
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002862 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002863#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002864 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002865 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002866#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002867 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2868 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002869
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002870 case DT_DEBUG:
2871 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2872 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002873// FIXME: not working currently for N64
2874// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002875// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002876// read-only, but the DYNAMIC header claims it is writable.
2877#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002878 if ((dynamic_flags & PF_W) != 0) {
2879 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2880 }
Chris Dearman99186652014-02-06 20:36:51 -08002881#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002882 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002883#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002884 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002885 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002886 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002887
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002888 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002889 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002890 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002891
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002892 case DT_ANDROID_RELA:
2893 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2894 break;
2895
2896 case DT_ANDROID_RELASZ:
2897 android_relocs_size_ = d->d_un.d_val;
2898 break;
2899
2900 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002901 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002902 return false;
2903
2904 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002905 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002906 return false;
2907
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002908 case DT_RELAENT:
2909 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002910 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002911 return false;
2912 }
2913 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002914
2915 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002916 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002917 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002918
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002919 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002920 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002921 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002922
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002923 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002924 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002925 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002926
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002927#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002928 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002929 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002930 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002931
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002932 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002933 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002934 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002935
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002936 case DT_RELENT:
2937 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002938 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002939 return false;
2940 }
2941 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002942
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002943 case DT_ANDROID_REL:
2944 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2945 break;
2946
2947 case DT_ANDROID_RELSZ:
2948 android_relocs_size_ = d->d_un.d_val;
2949 break;
2950
2951 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002952 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002953 return false;
2954
2955 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002956 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002957 return false;
2958
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002959 // "Indicates that all RELATIVE relocations have been concatenated together,
2960 // and specifies the RELATIVE relocation count."
2961 //
2962 // TODO: Spec also mentions that this can be used to optimize relocation process;
2963 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002964 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002965 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002966
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002967 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002968 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002969 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002970
2971 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002972 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002973 return false;
2974
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002975#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002976 case DT_INIT:
Dimitry Ivanov55437462016-07-20 15:33:07 -07002977 init_func_ = reinterpret_cast<linker_ctor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002978 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002979 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002980
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002981 case DT_FINI:
Dimitry Ivanov55437462016-07-20 15:33:07 -07002982 fini_func_ = reinterpret_cast<linker_dtor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002983 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002984 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002985
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002986 case DT_INIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07002987 init_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002988 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002989 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002990
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002991 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08002992 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002993 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002994
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002995 case DT_FINI_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07002996 fini_array_ = reinterpret_cast<linker_dtor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002997 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002998 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002999
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003000 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003001 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003002 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003003
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003004 case DT_PREINIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003005 preinit_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003006 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003007 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003008
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003009 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003010 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003011 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003012
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003013 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003014#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003015 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003016 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003017#else
3018 has_text_relocations = true;
3019 break;
3020#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003021
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003022 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003023 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003024 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003025
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003026 case DT_NEEDED:
3027 ++needed_count;
3028 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003029
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003030 case DT_FLAGS:
3031 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003032#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003033 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003034 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003035#else
3036 has_text_relocations = true;
3037#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003038 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003039 if (d->d_un.d_val & DF_SYMBOLIC) {
3040 has_DT_SYMBOLIC = true;
3041 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003042 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003043
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003044 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003045 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003046
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003047 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003048 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 -07003049 }
3050 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003051#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003052 case DT_MIPS_RLD_MAP:
3053 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3054 {
3055 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3056 *dp = &_r_debug;
3057 }
3058 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003059 case DT_MIPS_RLD_MAP_REL:
3060 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003061 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003062 r_debug** dp = reinterpret_cast<r_debug**>(
3063 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003064 *dp = &_r_debug;
3065 }
3066 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003067
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003068 case DT_MIPS_RLD_VERSION:
3069 case DT_MIPS_FLAGS:
3070 case DT_MIPS_BASE_ADDRESS:
3071 case DT_MIPS_UNREFEXTNO:
3072 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003073
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003074 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003075 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003076 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003077
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003078 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003079 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003080 break;
3081
3082 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003083 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003084 break;
3085#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003086 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3087 case DT_BIND_NOW:
3088 break;
3089
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003090 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003091 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3092 break;
3093
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003094 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003095 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3096 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003097 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003098 verdef_cnt_ = d->d_un.d_val;
3099 break;
3100
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003101 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003102 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3103 break;
3104
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003105 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003106 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003107 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003108
Evgenii Stepanov68650822015-06-10 13:38:39 -07003109 case DT_RUNPATH:
3110 // this is parsed after we have strtab initialized (see below).
3111 break;
3112
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003113 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003114 if (!relocating_linker) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003115 DL_WARN("\"%s\" unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003116 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3117 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003118 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003119 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003120 }
3121
Duane Sandbc425c72015-06-01 16:29:14 -07003122#if defined(__mips__) && !defined(__LP64__)
3123 if (!mips_check_and_adjust_fp_modes()) {
3124 return false;
3125 }
3126#endif
3127
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003128 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003129 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003130
3131 // Sanity checks.
3132 if (relocating_linker && needed_count != 0) {
3133 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3134 return false;
3135 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003136 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003137 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003138 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003139 return false;
3140 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003141 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003142 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003143 return false;
3144 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003145 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003146 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003147 return false;
3148 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003149
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003150 // second pass - parse entries relying on strtab
3151 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003152 switch (d->d_tag) {
3153 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003154 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003155 break;
3156 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003157 set_dt_runpath(get_string(d->d_un.d_val));
3158 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003159 }
3160 }
3161
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003162 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003163 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003164 // because they can't find dt_needed library by soname.
3165 // This workaround should keep them working. (applies only
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003166 // for apps targeting sdk version < M). Make an exception for
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003167 // the main executable and linker; they do not need to have dt_soname
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003168 if (soname_ == nullptr &&
3169 this != solist_get_somain() &&
3170 (flags_ & FLAG_LINKER) == 0 &&
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003171 get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003172 soname_ = basename(realpath_.c_str());
3173 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3174 get_realpath(), soname_);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003175 // 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 -07003176 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003177 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003178}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003179
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003180bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3181 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003182
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003183 local_group_root_ = local_group.front();
3184 if (local_group_root_ == nullptr) {
3185 local_group_root_ = this;
3186 }
3187
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003188 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3189 target_sdk_version_ = get_application_target_sdk_version();
3190 }
3191
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003192 VersionTracker version_tracker;
3193
3194 if (!version_tracker.init(this)) {
3195 return false;
3196 }
3197
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003198#if !defined(__LP64__)
3199 if (has_text_relocations) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003200 // Fail if app is targeting M or above.
3201 if (get_application_target_sdk_version() >= __ANDROID_API_M__) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003202 DL_ERR_AND_LOG("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003203 return false;
3204 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003205 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003206 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003207 DL_WARN("\"%s\" has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003208 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003209 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003210 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3211 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003212 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003213 return false;
3214 }
3215 }
3216#endif
3217
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003218 if (android_relocs_ != nullptr) {
3219 // check signature
3220 if (android_relocs_size_ > 3 &&
3221 android_relocs_[0] == 'A' &&
3222 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003223 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003224 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003225 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003226
3227 bool relocated = false;
3228 const uint8_t* packed_relocs = android_relocs_ + 4;
3229 const size_t packed_relocs_size = android_relocs_size_ - 4;
3230
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003231 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003232 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003233 packed_reloc_iterator<sleb128_decoder>(
3234 sleb128_decoder(packed_relocs, packed_relocs_size)),
3235 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003236
3237 if (!relocated) {
3238 return false;
3239 }
3240 } else {
3241 DL_ERR("bad android relocation header.");
3242 return false;
3243 }
3244 }
3245
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003246#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003247 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003248 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003249 if (!relocate(version_tracker,
3250 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003251 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003252 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003253 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003254 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003255 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003256 if (!relocate(version_tracker,
3257 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003258 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003259 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003260 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003261#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003262 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003263 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003264 if (!relocate(version_tracker,
3265 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003266 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003267 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003268 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003269 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003270 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003271 if (!relocate(version_tracker,
3272 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003273 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003274 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003275 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003276#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003277
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003278#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003279 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003280 return false;
3281 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003282#endif
3283
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003284 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003285
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003286#if !defined(__LP64__)
3287 if (has_text_relocations) {
3288 // All relocations are done, we can protect our segments back to read-only.
3289 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3290 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003291 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003292 return false;
3293 }
3294 }
3295#endif
3296
Mingwei Shibe910522015-11-12 07:02:14 +00003297 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3298 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3299 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003300 return false;
3301 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003302
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003303 /* Handle serializing/sharing the RELRO segment */
3304 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3305 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3306 extinfo->relro_fd) < 0) {
3307 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003308 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003309 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003310 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003311 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3312 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3313 extinfo->relro_fd) < 0) {
3314 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003315 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003316 return false;
3317 }
3318 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003319
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003320 notify_gdb_of_load(this);
3321 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003322}
3323
Mingwei Shibe910522015-11-12 07:02:14 +00003324bool soinfo::protect_relro() {
3325 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3326 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3327 get_realpath(), strerror(errno));
3328 return false;
3329 }
3330 return true;
3331}
3332
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003333void init_default_namespace() {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003334 g_default_namespace.set_name("(default)");
3335 g_default_namespace.set_isolated(false);
3336
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003337 soinfo* somain = solist_get_somain();
3338
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003339 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3340 somain->load_bias);
3341 const char* bname = basename(interp);
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003342
3343 bool is_asan = bname != nullptr &&
3344 (strcmp(bname, "linker_asan") == 0 ||
3345 strcmp(bname, "linker_asan64") == 0);
3346 auto default_ld_paths = is_asan ? kAsanDefaultLdPaths : kDefaultLdPaths;
3347 g_is_asan = is_asan;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003348
neo.chae2589f9d2016-10-04 11:00:27 +09003349 char real_path[PATH_MAX];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003350 std::vector<std::string> ld_default_paths;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003351 for (size_t i = 0; default_ld_paths[i] != nullptr; ++i) {
3352 if (realpath(default_ld_paths[i], real_path) != nullptr) {
neo.chae2589f9d2016-10-04 11:00:27 +09003353 ld_default_paths.push_back(real_path);
3354 } else {
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003355 ld_default_paths.push_back(default_ld_paths[i]);
neo.chae2589f9d2016-10-04 11:00:27 +09003356 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003357 }
3358
3359 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003360};