blob: 4b00f801a916c8e9a738e4d320abb0e826f525ab [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002 * Copyright (C) 2008 The Android Open Source Project
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003 * All rights reserved.
4 *
5 * Redistribution and use in source and binary forms, with or without
6 * modification, are permitted provided that the following conditions
7 * are met:
8 * * Redistributions of source code must retain the above copyright
9 * notice, this list of conditions and the following disclaimer.
10 * * Redistributions in binary form must reproduce the above copyright
11 * notice, this list of conditions and the following disclaimer in
12 * the documentation and/or other materials provided with the
13 * distribution.
14 *
15 * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
16 * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
17 * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
18 * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
19 * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
20 * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
21 * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
22 * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
23 * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
24 * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
25 * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
26 * SUCH DAMAGE.
27 */
28
Dmitriy Ivanov19133522015-06-02 17:36:54 -070029#include <android/api-level.h>
Elliott Hughes46882792012-08-03 16:49:39 -070030#include <errno.h>
31#include <fcntl.h>
Elliott Hughes0266ae52014-02-10 17:46:57 -080032#include <inttypes.h>
Elliott Hughes46882792012-08-03 16:49:39 -070033#include <pthread.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080034#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
Elliott Hughes46882792012-08-03 16:49:39 -070037#include <sys/mman.h>
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -080038#include <sys/param.h>
Elliott Hughes46882792012-08-03 16:49:39 -070039#include <unistd.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080040
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070041#include <new>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070042#include <string>
Dmitriy Ivanovb4827502015-09-28 16:38:31 -070043#include <unordered_map>
Dmitriy Ivanovd165f562015-03-23 18:43:02 -070044#include <vector>
Dmitriy Ivanov0d150942014-08-22 12:25:04 -070045
Elliott Hughes46882792012-08-03 16:49:39 -070046// Private C library headers.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070047#include "private/ScopeGuard.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080048
49#include "linker.h"
Dmitriy Ivanovc9ce70d2015-03-10 15:30:26 -070050#include "linker_block_allocator.h"
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070051#include "linker_cfi.h"
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -080052#include "linker_gdb_support.h"
Dimitry Ivanov48ec2882016-08-04 11:50:36 -070053#include "linker_globals.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080054#include "linker_debug.h"
Dimitry Ivanov769b33f2016-07-21 11:33:40 -070055#include "linker_dlwarning.h"
Dimitry Ivanov3f660572016-09-09 10:00:39 -070056#include "linker_main.h"
Dimitry Ivanovb943f302016-08-03 16:00:10 -070057#include "linker_namespaces.h"
Dmitriy Ivanov18870d32015-04-22 13:10:04 -070058#include "linker_sleb128.h"
David 'Digit' Turner23363ed2012-06-18 18:13:49 +020059#include "linker_phdr.h"
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -080060#include "linker_relocs.h"
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -080061#include "linker_reloc_iterators.h"
Dmitriy Ivanova1feb112015-10-01 18:41:57 -070062#include "linker_utils.h"
tony.ys_liub4474402015-07-29 18:00:22 +080063
Elliott Hughes939a7e02015-12-04 15:27:46 -080064#include "android-base/strings.h"
Dimitry Ivanovb996d602016-07-11 18:11:39 -070065#include "android-base/stringprintf.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000066#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080067
Elliott Hughes1801db32015-06-08 18:04:00 -070068// Override macros to use C++ style casts.
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080069#undef ELF_ST_TYPE
70#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
71
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -070072static android_namespace_t* g_anonymous_namespace = &g_default_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070073
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -070074static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
75static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +020076
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070077static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -070078static LinkerTypeAllocator<LinkedListEntry<android_namespace_t>> g_namespace_list_allocator;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070079
Elliott Hughes4eeb1f12013-10-25 17:38:02 -070080#if defined(__LP64__)
Jiyong Park1daa18e2017-01-31 17:32:46 +090081static const char* const kSystemLibDir = "/system/lib64";
82static const char* const kSystemNdkLibDir = "/system/lib64/ndk";
83static const char* const kSystemVndkLibDir = "/system/lib64/vndk";
84static const char* const kSystemVndkExtLibDir = "/system/lib64/vndk-ext";
85static const char* const kVendorSpHalLibDir = "/vendor/lib64/sameprocess";
86static const char* const kVendorLibDir = "/vendor/lib64";
87static const char* const kAsanSystemLibDir = "/data/lib64";
88static const char* const kAsanSystemNdkLibDir = "/data/lib64/ndk";
89static const char* const kAsanSystemVndkLibDir = "/data/lib64/vndk";
90static const char* const kAsanSystemVndkExtLibDir = "/data/lib64/vndk-ext";
91static const char* const kAsanVendorSpHalLibDir = "/data/vendor/lib64/sameprocess";
92static const char* const kAsanVendorLibDir = "/data/vendor/lib64";
Elliott Hughes011bc0b2013-10-08 14:27:10 -070093#else
Jiyong Park1daa18e2017-01-31 17:32:46 +090094static const char* const kSystemLibDir = "/system/lib";
95static const char* const kSystemNdkLibDir = "/system/lib/ndk";
96static const char* const kSystemVndkLibDir = "/system/lib/vndk";
97static const char* const kSystemVndkExtLibDir = "/system/lib/vndk-ext";
98static const char* const kVendorSpHalLibDir = "/vendor/lib/sameprocess";
99static const char* const kVendorLibDir = "/vendor/lib";
100static const char* const kAsanSystemLibDir = "/data/lib";
101static const char* const kAsanSystemNdkLibDir = "/data/lib/ndk";
102static const char* const kAsanSystemVndkLibDir = "/data/lib/vndk";
103static const char* const kAsanSystemVndkExtLibDir = "/data/lib/vndk-ext";
104static const char* const kAsanVendorSpHalLibDir = "/data/vendor/lib/sameprocess";
105static const char* const kAsanVendorLibDir = "/data/vendor/lib";
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700106#endif
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700107
108static const char* const kDefaultLdPaths[] = {
109 kSystemLibDir,
Jiyong Park1daa18e2017-01-31 17:32:46 +0900110 kSystemNdkLibDir,
111 kSystemVndkExtLibDir,
112 kSystemVndkLibDir,
113 kVendorSpHalLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700114 kVendorLibDir,
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700115 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700116};
David Bartleybc3a5c22009-06-02 18:27:28 -0700117
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700118static const char* const kAsanDefaultLdPaths[] = {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700119 kAsanSystemLibDir,
120 kSystemLibDir,
Jiyong Park1daa18e2017-01-31 17:32:46 +0900121 kAsanSystemNdkLibDir,
122 kSystemNdkLibDir,
123 kAsanSystemVndkExtLibDir,
124 kSystemVndkExtLibDir,
125 kAsanSystemVndkLibDir,
126 kSystemVndkLibDir,
127 kAsanVendorSpHalLibDir,
128 kVendorSpHalLibDir,
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700129 kAsanVendorLibDir,
130 kVendorLibDir,
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700131 nullptr
132};
133
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700134// Is ASAN enabled?
135static bool g_is_asan = false;
136
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -0700137static CFIShadowWriter g_cfi_shadow;
138
139CFIShadowWriter* get_cfi_shadow() {
140 return &g_cfi_shadow;
141}
142
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700143static bool is_system_library(const std::string& realpath) {
144 for (const auto& dir : g_default_namespace.get_default_library_paths()) {
145 if (file_is_in_dir(realpath, dir)) {
146 return true;
147 }
148 }
149 return false;
150}
151
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700152// Checks if the file exists and not a directory.
153static bool file_exists(const char* path) {
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700154 struct stat s;
155
156 if (stat(path, &s) != 0) {
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700157 return false;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700158 }
Dimitry Ivanov4cf70242016-08-11 11:11:52 -0700159
160 return S_ISREG(s.st_mode);
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -0700161}
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700162
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800163static std::string resolve_soname(const std::string& name) {
164 // We assume that soname equals to basename here
165
166 // TODO(dimitry): consider having honest absolute-path -> soname resolution
167 // note that since we might end up refusing to load this library because
168 // it is not in shared libs list we need to get the soname without actually loading
169 // the library.
170 //
171 // On the other hand there are several places where we already assume that
172 // soname == basename in particular for any not-loaded library mentioned
173 // in DT_NEEDED list.
174 return basename(name.c_str());
175}
176
177static bool maybe_accessible_via_namespace_links(android_namespace_t* ns, const char* name) {
178 std::string soname = resolve_soname(name);
179 for (auto& ns_link : ns->linked_namespaces()) {
180 if (ns_link.is_accessible(soname.c_str())) {
181 return true;
182 }
183 }
184
185 return false;
186}
187
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700188// TODO(dimitry): The grey-list is a workaround for http://b/26394120 ---
189// gradually remove libraries from this list until it is gone.
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800190static bool is_greylisted(android_namespace_t* ns, const char* name, const soinfo* needed_by) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700191 static const char* const kLibraryGreyList[] = {
192 "libandroid_runtime.so",
193 "libbinder.so",
194 "libcrypto.so",
195 "libcutils.so",
196 "libexpat.so",
197 "libgui.so",
198 "libmedia.so",
199 "libnativehelper.so",
200 "libskia.so",
201 "libssl.so",
202 "libstagefright.so",
203 "libsqlite.so",
204 "libui.so",
205 "libutils.so",
206 "libvorbisidec.so",
207 nullptr
208 };
209
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800210 // If you're targeting N, you don't get the greylist.
211 if (get_application_target_sdk_version() >= __ANDROID_API_N__) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700212 return false;
213 }
214
215 // if the library needed by a system library - implicitly assume it
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800216 // is greylisted unless it is in the list of shared libraries for one or
217 // more linked namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700218 if (needed_by != nullptr && is_system_library(needed_by->get_realpath())) {
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -0800219 return !maybe_accessible_via_namespace_links(ns, name);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700220 }
221
222 // if this is an absolute path - make sure it points to /system/lib(64)
223 if (name[0] == '/' && dirname(name) == kSystemLibDir) {
224 // and reduce the path to basename
225 name = basename(name);
226 }
227
228 for (size_t i = 0; kLibraryGreyList[i] != nullptr; ++i) {
229 if (strcmp(name, kLibraryGreyList[i]) == 0) {
230 return true;
231 }
232 }
233
234 return false;
235}
236// END OF WORKAROUND
237
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700238static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800239
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800240static bool g_anonymous_namespace_initialized;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700241
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800242#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700243struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700244 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700245};
246
247static linker_stats_t linker_stats;
248
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800249void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700250 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700251}
252#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800253void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700254}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800255#endif
256
257#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800258uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800259#endif
260
Elliott Hughesbedfe382012-08-14 14:07:59 -0700261static void notify_gdb_of_load(soinfo* info) {
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800262 if (info->is_linker() || info->is_main_executable()) {
263 // gdb already knows about the linker and the main executable.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700264 return;
265 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800266
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800267 link_map* map = &(info->link_map_head);
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000268
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800269 map->l_addr = info->load_bias;
270 // link_map l_name field is not const.
271 map->l_name = const_cast<char*>(info->get_realpath());
272 map->l_ld = info->dynamic;
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000273
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800274 CHECK(map->l_name != nullptr);
275 CHECK(map->l_name[0] != '\0');
276
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800277 notify_gdb_of_load(map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700278}
279
Elliott Hughesbedfe382012-08-14 14:07:59 -0700280static void notify_gdb_of_unload(soinfo* info) {
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800281 notify_gdb_of_unload(&(info->link_map_head));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800282}
283
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700284LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
285 return g_soinfo_links_allocator.alloc();
286}
287
288void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
289 g_soinfo_links_allocator.free(entry);
290}
291
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700292LinkedListEntry<android_namespace_t>* NamespaceListAllocator::alloc() {
293 return g_namespace_list_allocator.alloc();
294}
295
296void NamespaceListAllocator::free(LinkedListEntry<android_namespace_t>* entry) {
297 g_namespace_list_allocator.free(entry);
298}
299
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700300soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
301 struct stat* file_stat, off64_t file_offset,
302 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700303 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200304 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700305 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200306 }
307
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700308 TRACE("name %s: allocating soinfo for ns=%p", name, ns);
309
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700310 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
311 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700312
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700313 solist_add_soinfo(si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200314
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700315 si->generate_handle();
316 ns->add_soinfo(si);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700317
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700318 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200319 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800320}
321
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800322static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700323 if (si == nullptr) {
324 return;
325 }
326
327 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800328 if (!si->is_mapped_by_caller()) {
329 munmap(reinterpret_cast<void*>(si->base), si->size);
330 } else {
331 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
332 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
333 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
334 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700335 }
336
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700337 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700338
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700339 if (!solist_remove_soinfo(si)) {
340 // TODO (dimitry): revisit this - for now preserving the logic
341 // but it does not look right, abort if soinfo is not in the list instead?
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700342 return;
343 }
Elliott Hughes46882792012-08-03 16:49:39 -0700344
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700345 // clear links to/from si
346 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700347
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700348 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700349 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800350}
351
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700352static void parse_path(const char* path, const char* delimiters,
353 std::vector<std::string>* resolved_paths) {
354 std::vector<std::string> paths;
355 split_path(path, delimiters, &paths);
356 resolve_paths(paths, resolved_paths);
357}
358
Elliott Hughescade4c32012-12-20 14:42:14 -0800359static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700360 std::vector<std::string> ld_libary_paths;
361 parse_path(path, ":", &ld_libary_paths);
362 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800363}
364
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700365static bool realpath_fd(int fd, std::string* realpath) {
366 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700367 __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700368 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700369 PRINT("readlink(\"%s\") failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700370 return false;
371 }
372
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700373 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700374 return true;
375}
376
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700377#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700378
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700379// For a given PC, find the .so that it belongs to.
380// Returns the base address of the .ARM.exidx section
381// for that .so, and the number of 8-byte entries
382// in that section (via *pcount).
383//
384// Intended to be called by libc's __gnu_Unwind_Find_exidx().
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -0800385_Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800386 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800387
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700388 for (soinfo* si = solist_get_head(); si != 0; si = si->next) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700389 if ((addr >= si->base) && (addr < (si->base + si->size))) {
390 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800391 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800392 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700393 }
394 *pcount = 0;
395 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800396}
Elliott Hughes46882792012-08-03 16:49:39 -0700397
Christopher Ferris24053a42013-08-19 17:45:09 -0700398#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700399
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700400// Here, we only have to provide a callback to iterate across all the
401// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700402int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700403 int rv = 0;
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700404 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700405 dl_phdr_info dl_info;
406 dl_info.dlpi_addr = si->link_map_head.l_addr;
407 dl_info.dlpi_name = si->link_map_head.l_name;
408 dl_info.dlpi_phdr = si->phdr;
409 dl_info.dlpi_phnum = si->phnum;
410 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
411 if (rv != 0) {
412 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800413 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700414 }
415 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800416}
Elliott Hughes46882792012-08-03 16:49:39 -0700417
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800418
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700419bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
Dimitry Ivanovb943f302016-08-03 16:00:10 -0700420 soinfo** si_found_in, const soinfo_list_t& global_group,
421 const soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800422 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700423 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700424
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700425 /* "This element's presence in a shared object library alters the dynamic linker's
426 * symbol resolution algorithm for references within the library. Instead of starting
427 * a symbol search with the executable file, the dynamic linker starts from the shared
428 * object itself. If the shared object fails to supply the referenced symbol, the
429 * dynamic linker then searches the executable file and other shared objects as usual."
430 *
431 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
432 *
433 * Note that this is unlikely since static linker avoids generating
434 * relocations for -Bsymbolic linked dynamic executables.
435 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700436 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700437 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700438 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
439 return false;
440 }
441
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700442 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700443 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700444 }
445 }
446
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700447 // 1. Look for it in global_group
448 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700449 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700450 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700451 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700452 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700453 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
454 error = true;
455 return false;
456 }
457
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700458 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700459 *si_found_in = global_si;
460 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700461 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700462
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700463 return true;
464 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700465
466 if (error) {
467 return false;
468 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700469 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700470
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700471 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700472 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700473 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700474 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700475 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700476 // we already did this - skip
477 return true;
478 }
479
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700480 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700481 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700482 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
483 error = true;
484 return false;
485 }
486
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700487 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700488 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700489 return false;
490 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700491
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700492 return true;
493 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700494
495 if (error) {
496 return false;
497 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700498 }
499
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700500 if (s != nullptr) {
501 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
502 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700503 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
504 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700505 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700506 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700507
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700508 *symbol = s;
509 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700510}
511
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700512ProtectedDataGuard::ProtectedDataGuard() {
513 if (ref_count_++ == 0) {
514 protect_data(PROT_READ | PROT_WRITE);
515 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700516
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700517 if (ref_count_ == 0) { // overflow
518 __libc_fatal("Too many nested calls to dlopen()");
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800519 }
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800520}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800521
Dimitry Ivanov68e6c032017-02-01 12:55:11 -0800522ProtectedDataGuard::~ProtectedDataGuard() {
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700523 if (--ref_count_ == 0) {
524 protect_data(PROT_READ);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800525 }
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700526}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800527
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700528void ProtectedDataGuard::protect_data(int protection) {
529 g_soinfo_allocator.protect_all(protection);
530 g_soinfo_links_allocator.protect_all(protection);
531 g_namespace_allocator.protect_all(protection);
532 g_namespace_list_allocator.protect_all(protection);
533}
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -0800534
535size_t ProtectedDataGuard::ref_count_ = 0;
536
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700537// Each size has it's own allocator.
538template<size_t size>
539class SizeBasedAllocator {
540 public:
541 static void* alloc() {
542 return allocator_.alloc();
543 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700544
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700545 static void free(void* ptr) {
546 allocator_.free(ptr);
547 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -0700548
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700549 private:
550 static LinkerBlockAllocator allocator_;
551};
552
553template<size_t size>
554LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
555
556template<typename T>
557class TypeBasedAllocator {
558 public:
559 static T* alloc() {
560 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
561 }
562
563 static void free(T* ptr) {
564 SizeBasedAllocator<sizeof(T)>::free(ptr);
565 }
566};
567
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700568class LoadTask {
569 public:
570 struct deleter_t {
571 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700572 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700573 TypeBasedAllocator<LoadTask>::free(t);
574 }
575 };
576
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700577 static deleter_t deleter;
578
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800579 static LoadTask* create(const char* name,
580 soinfo* needed_by,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700581 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700582 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700583 return new (ptr) LoadTask(name, needed_by, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700584 }
585
586 const char* get_name() const {
587 return name_;
588 }
589
590 soinfo* get_needed_by() const {
591 return needed_by_;
592 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700593
594 soinfo* get_soinfo() const {
595 return si_;
596 }
597
598 void set_soinfo(soinfo* si) {
599 si_ = si;
600 }
601
602 off64_t get_file_offset() const {
603 return file_offset_;
604 }
605
606 void set_file_offset(off64_t offset) {
607 file_offset_ = offset;
608 }
609
610 int get_fd() const {
611 return fd_;
612 }
613
614 void set_fd(int fd, bool assume_ownership) {
615 fd_ = fd;
616 close_fd_ = assume_ownership;
617 }
618
619 const android_dlextinfo* get_extinfo() const {
620 return extinfo_;
621 }
622
623 void set_extinfo(const android_dlextinfo* extinfo) {
624 extinfo_ = extinfo;
625 }
626
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700627 bool is_dt_needed() const {
628 return is_dt_needed_;
629 }
630
631 void set_dt_needed(bool is_dt_needed) {
632 is_dt_needed_ = is_dt_needed;
633 }
634
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700635 const ElfReader& get_elf_reader() const {
636 CHECK(si_ != nullptr);
637 return (*elf_readers_map_)[si_];
638 }
639
640 ElfReader& get_elf_reader() {
641 CHECK(si_ != nullptr);
642 return (*elf_readers_map_)[si_];
643 }
644
645 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
646 return elf_readers_map_;
647 }
648
649 bool read(const char* realpath, off64_t file_size) {
650 ElfReader& elf_reader = get_elf_reader();
651 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
652 }
653
654 bool load() {
655 ElfReader& elf_reader = get_elf_reader();
656 if (!elf_reader.Load(extinfo_)) {
657 return false;
658 }
659
660 si_->base = elf_reader.load_start();
661 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800662 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700663 si_->load_bias = elf_reader.load_bias();
664 si_->phnum = elf_reader.phdr_count();
665 si_->phdr = elf_reader.loaded_phdr();
666
667 return true;
668 }
669
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700670 private:
Dimitry Ivanov7d429d32017-02-01 15:28:52 -0800671 LoadTask(const char* name,
672 soinfo* needed_by,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700673 std::unordered_map<const soinfo*, ElfReader>* readers_map)
674 : name_(name), needed_by_(needed_by), si_(nullptr),
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700675 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map),
676 is_dt_needed_(false) {}
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700677
678 ~LoadTask() {
679 if (fd_ != -1 && close_fd_) {
680 close(fd_);
681 }
682 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700683
684 const char* name_;
685 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700686 soinfo* si_;
687 const android_dlextinfo* extinfo_;
688 int fd_;
689 bool close_fd_;
690 off64_t file_offset_;
691 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700692 // TODO(dimitry): needed by workaround for http://b/26394120 (the grey-list)
693 bool is_dt_needed_;
694 // END OF WORKAROUND
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700695
696 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
697};
698
Ningsheng Jiane93be992014-09-16 15:22:10 +0800699LoadTask::deleter_t LoadTask::deleter;
700
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700701template <typename T>
702using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
703
704typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -0700705typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700706typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700707
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800708enum walk_action_result_t : uint32_t {
709 kWalkStop = 0,
710 kWalkContinue = 1,
711 kWalkSkip = 2
712};
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700713
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700714// This function walks down the tree of soinfo dependencies
715// in breadth-first order and
716// * calls action(soinfo* si) for each node, and
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800717// * terminates walk if action returns kWalkStop
718// * skips children of the node if action
719// return kWalkSkip
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700720//
721// walk_dependencies_tree returns false if walk was terminated
722// by the action and true otherwise.
723template<typename F>
724static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -0700725 SoinfoLinkedList visit_list;
726 SoinfoLinkedList visited;
727
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700728 for (size_t i = 0; i < root_soinfos_size; ++i) {
729 visit_list.push_back(root_soinfos[i]);
730 }
731
732 soinfo* si;
733 while ((si = visit_list.pop_front()) != nullptr) {
734 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -0700735 continue;
736 }
737
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800738 walk_action_result_t result = action(si);
739
740 if (result == kWalkStop) {
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700741 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700742 }
743
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700744 visited.push_back(si);
745
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800746 if (result != kWalkSkip) {
747 si->get_children().for_each([&](soinfo* child) {
748 visit_list.push_back(child);
749 });
750 }
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700751 }
752
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700753 return true;
754}
755
756
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800757static const ElfW(Sym)* dlsym_handle_lookup(android_namespace_t* ns,
758 soinfo* root,
759 soinfo* skip_until,
760 soinfo** found,
761 SymbolName& symbol_name,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800762 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700763 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700764 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700765
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700766 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
767 if (skip_lookup) {
768 skip_lookup = current_soinfo != skip_until;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800769 return kWalkContinue;
770 }
771
772 if (!ns->is_accessible(current_soinfo)) {
773 return kWalkSkip;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700774 }
775
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800776 if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700777 result = nullptr;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800778 return kWalkStop;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700779 }
780
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700781 if (result != nullptr) {
782 *found = current_soinfo;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800783 return kWalkStop;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700784 }
785
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800786 return kWalkContinue;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700787 });
788
789 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800790}
791
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800792static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
793 const char* name,
794 const version_info* vi,
795 soinfo** found,
796 soinfo* caller,
797 void* handle);
798
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700799// This is used by dlsym(3). It performs symbol lookup only within the
800// specified soinfo object and its dependencies in breadth first order.
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800801static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si,
802 soinfo** found,
803 const char* name,
804 const version_info* vi) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700805 // According to man dlopen(3) and posix docs in the case when si is handle
806 // of the main executable we need to search not only in the executable and its
807 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
808 //
809 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
810 // libraries and they are loaded in breath-first (correct) order we can just execute
811 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700812 if (si == solist_get_somain()) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800813 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -0700814 }
815
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700816 SymbolName symbol_name(name);
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800817 // note that the namespace is not the namespace associated with caller_addr
818 // we use ns associated with root si intentionally here. Using caller_ns
819 // causes problems when user uses dlopen_ext to open a library in the separate
820 // namespace and then calls dlsym() on the handle.
821 return dlsym_handle_lookup(si->get_primary_namespace(), si, nullptr, found, symbol_name, vi);
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700822}
823
Brian Carlstromd4ee82d2013-02-28 15:58:45 -0800824/* This is used by dlsym(3) to performs a global symbol lookup. If the
825 start value is null (for RTLD_DEFAULT), the search starts at the
826 beginning of the global solist. Otherwise the search starts at the
827 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700828 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800829static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
830 const char* name,
831 const version_info* vi,
832 soinfo** found,
833 soinfo* caller,
834 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800835 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800836
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700837 auto& soinfo_list = ns->soinfo_list();
838 auto start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700839
840 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -0700841 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700842 return nullptr;
843 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700844 auto it = soinfo_list.find(caller);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700845 CHECK (it != soinfo_list.end());
846 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700847 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800848 }
849
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700850 const ElfW(Sym)* s = nullptr;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700851 for (auto it = start, end = soinfo_list.end(); it != end; ++it) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700852 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700853 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800854 // if the library is opened by application with target api level < M.
Dmitriy Ivanov19133522015-06-02 17:36:54 -0700855 // See http://b/21565766
Elliott Hughes5bc78c82016-11-16 11:35:43 -0800856 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 &&
857 si->get_target_sdk_version() >= __ANDROID_API_M__) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700858 continue;
859 }
860
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800861 if (!si->find_symbol_by_name(symbol_name, vi, &s)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700862 return nullptr;
863 }
864
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700865 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -0800866 *found = si;
867 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -0600868 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800869 }
Matt Fischer1698d9e2009-12-31 12:17:56 -0600870
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -0700871 // If not found - use dlsym_handle_lookup for caller's
872 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700873 // case we already did it.
874 if (s == nullptr && caller != nullptr &&
875 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -0800876 soinfo* local_group_root = caller->get_local_group_root();
877
878 return dlsym_handle_lookup(local_group_root->get_primary_namespace(),
879 local_group_root,
880 (handle == RTLD_NEXT) ? caller : nullptr,
881 found,
882 symbol_name,
883 vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -0700884 }
885
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700886 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -0700887 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
888 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -0800889 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800890
Elliott Hughescade4c32012-12-20 14:42:14 -0800891 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800892}
893
Kito Chengfa8c05d2013-03-12 14:58:06 +0800894soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -0800895 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dimitry Ivanov3f660572016-09-09 10:00:39 -0700896 for (soinfo* si = solist_get_head(); si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +0800897 if (address >= si->base && address - si->base < si->size) {
898 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600899 }
Kito Chengfa8c05d2013-03-12 14:58:06 +0800900 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700901 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -0600902}
903
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700904class ZipArchiveCache {
905 public:
906 ZipArchiveCache() {}
907 ~ZipArchiveCache();
908
909 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
910 private:
911 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
912
913 std::unordered_map<std::string, ZipArchiveHandle> cache_;
914};
915
916bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
917 std::string key(zip_path);
918
919 auto it = cache_.find(key);
920 if (it != cache_.end()) {
921 *handle = it->second;
922 return true;
923 }
924
925 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
926 if (fd == -1) {
927 return false;
928 }
929
930 if (OpenArchiveFd(fd, "", handle) != 0) {
931 // invalid zip-file (?)
Yabin Cui722072d2016-03-21 17:10:12 -0700932 CloseArchive(handle);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700933 close(fd);
934 return false;
935 }
936
937 cache_[key] = *handle;
938 return true;
939}
940
941ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -0700942 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700943 CloseArchive(it.second);
944 }
945}
946
947static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700948 const char* const input_path,
949 off64_t* file_offset, std::string* realpath) {
950 std::string normalized_path;
951 if (!normalize_path(input_path, &normalized_path)) {
952 return -1;
953 }
954
955 const char* const path = normalized_path.c_str();
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700956 TRACE("Trying zip file open from path \"%s\" -> normalized \"%s\"", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +0000957
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700958 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +0000959 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700960 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +0000961 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700962 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +0000963 if (separator == nullptr) {
964 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -0700965 }
Simon Baldwinaef71952015-01-16 13:22:54 +0000966
967 char buf[512];
968 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
969 PRINT("Warning: ignoring very long library path: %s", path);
970 return -1;
971 }
972
973 buf[separator - path] = '\0';
974
975 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -0700976 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +0000977 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
978 if (fd == -1) {
979 return -1;
980 }
981
982 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -0700983 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000984 // invalid zip-file (?)
985 close(fd);
986 return -1;
987 }
988
Simon Baldwinaef71952015-01-16 13:22:54 +0000989 ZipEntry entry;
990
Yusuke Sato56f40fb2015-06-25 14:56:07 -0700991 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +0000992 // Entry was not found.
993 close(fd);
994 return -1;
995 }
996
997 // Check if it is properly stored
998 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
999 close(fd);
1000 return -1;
1001 }
1002
1003 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001004
1005 if (realpath_fd(fd, realpath)) {
1006 *realpath += separator;
1007 } else {
1008 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1009 normalized_path.c_str());
1010 *realpath = normalized_path;
1011 }
1012
Simon Baldwinaef71952015-01-16 13:22:54 +00001013 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001014}
1015
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001016static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
1017 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
1018 if (n < 0 || n >= static_cast<int>(buf_size)) {
1019 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1020 return false;
1021 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001022
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001023 return true;
1024}
1025
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001026static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1027 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001028 const std::vector<std::string>& paths,
1029 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001030 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001031 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001032 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001033 continue;
1034 }
1035
1036 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001037 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001038 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001039 }
1040
1041 if (fd == -1) {
1042 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1043 if (fd != -1) {
1044 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001045 if (!realpath_fd(fd, realpath)) {
1046 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1047 *realpath = buf;
1048 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001049 }
1050 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001051
1052 if (fd != -1) {
1053 return fd;
1054 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001055 }
1056
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001057 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001058}
1059
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001060static int open_library(android_namespace_t* ns,
1061 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001062 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001063 off64_t* file_offset, std::string* realpath) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001064 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001065
Elliott Hughes124fae92012-10-31 14:20:03 -07001066 // 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 -07001067 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001068 int fd = -1;
1069
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001070 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001071 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1072 }
1073
1074 if (fd == -1) {
1075 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001076 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001077 *file_offset = 0;
1078 if (!realpath_fd(fd, realpath)) {
1079 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1080 *realpath = name;
1081 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001082 }
1083 }
1084
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001085 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001086 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001087
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001088 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1089 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 -07001090 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001091 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001092 // Check if the library is accessible
1093 if (fd != -1 && !ns->is_accessible(*realpath)) {
1094 fd = -1;
1095 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001096 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001097
Elliott Hughes124fae92012-10-31 14:20:03 -07001098 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001099 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001100 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001101
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001102 // TODO(dimitry): workaround for http://b/26394120 (the grey-list)
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001103 if (fd == -1 && ns != &g_default_namespace && is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001104 // try searching for it on default_namespace default_library_path
1105 fd = open_library_on_paths(zip_archive_cache, name, file_offset,
1106 g_default_namespace.get_default_library_paths(), realpath);
1107 }
1108 // END OF WORKAROUND
1109
Elliott Hughes124fae92012-10-31 14:20:03 -07001110 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001111}
1112
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001113const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001114#if !defined(__LP64__)
1115 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Elliott Hughes5bc78c82016-11-16 11:35:43 -08001116 if (get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001117 const char* bname = basename(dt_needed);
1118 if (bname != dt_needed) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001119 DL_WARN("library \"%s\" has invalid DT_NEEDED entry \"%s\"", sopath, dt_needed);
1120 add_dlwarning(sopath, "invalid DT_NEEDED entry", dt_needed);
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001121 }
1122
1123 return bname;
1124 }
1125#endif
1126 return dt_needed;
1127}
1128
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001129template<typename F>
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001130static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1131 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1132 if (d->d_tag == DT_NEEDED) {
1133 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1134 }
1135 }
1136}
1137
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001138static bool load_library(android_namespace_t* ns,
1139 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001140 LoadTaskList* load_tasks,
1141 int rtld_flags,
1142 const std::string& realpath) {
1143 off64_t file_offset = task->get_file_offset();
1144 const char* name = task->get_name();
1145 const android_dlextinfo* extinfo = task->get_extinfo();
1146
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001147 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001148 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001149 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001150 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001151 if (file_offset < 0) {
1152 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001153 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001154 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001155
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001156 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001157 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001158 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001159 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001160 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001161 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001162 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1163 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001164 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001165 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001166
1167 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001168 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1169 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001170 auto predicate = [&](soinfo* si) {
1171 return si->get_st_dev() != 0 &&
1172 si->get_st_ino() != 0 &&
1173 si->get_st_dev() == file_stat.st_dev &&
1174 si->get_st_ino() == file_stat.st_ino &&
1175 si->get_file_offset() == file_offset;
1176 };
1177
1178 soinfo* si = ns->soinfo_list().find_if(predicate);
1179
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001180 if (si != nullptr) {
1181 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1182 "will return existing soinfo", name, si->get_realpath());
1183 task->set_soinfo(si);
1184 return true;
1185 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001186 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001187
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001188 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001189 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001190 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001191 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001192
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001193 if (!ns->is_accessible(realpath)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001194 // TODO(dimitry): workaround for http://b/26394120 - the grey-list
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001195
1196 // TODO(dimitry) before O release: add a namespace attribute to have this enabled
1197 // only for classloader-namespaces
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001198 const soinfo* needed_by = task->is_dt_needed() ? task->get_needed_by() : nullptr;
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001199 if (is_greylisted(ns, name, needed_by)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001200 // print warning only if needed by non-system library
1201 if (needed_by == nullptr || !is_system_library(needed_by->get_realpath())) {
1202 const soinfo* needed_or_dlopened_by = task->get_needed_by();
1203 const char* sopath = needed_or_dlopened_by == nullptr ? "(unknown)" :
1204 needed_or_dlopened_by->get_realpath();
1205 DL_WARN("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the namespace \"%s\""
1206 " - the access is temporarily granted as a workaround for http://b/26394120, note that the access"
1207 " will be removed in future releases of Android.",
1208 name, realpath.c_str(), sopath, ns->get_name());
1209 add_dlwarning(sopath, "unauthorized access to", name);
1210 }
1211 } else {
1212 // do not load libraries if they are not accessible for the specified namespace.
1213 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1214 "(unknown)" :
1215 task->get_needed_by()->get_realpath();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001216
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001217 DL_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
1218 name, needed_or_dlopened_by, ns->get_name());
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001219
Dimitry Ivanov35c8e3b2017-02-27 12:17:47 -08001220 // do not print this if a library is in the list of shared libraries for linked namespaces
1221 if (!maybe_accessible_via_namespace_links(ns, name)) {
1222 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1223 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1224 " permitted_paths=\"%s\"]",
1225 name, realpath.c_str(),
1226 needed_or_dlopened_by,
1227 ns->get_name(),
1228 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1229 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1230 android::base::Join(ns->get_permitted_paths(), ':').c_str());
1231 }
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001232 return false;
1233 }
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001234 }
1235
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001236 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001237 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001238 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001239 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001240
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001241 task->set_soinfo(si);
1242
1243 // Read the ELF header and some of the segments.
1244 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001245 soinfo_free(si);
1246 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001247 return false;
1248 }
1249
1250 // find and set DT_RUNPATH and dt_soname
1251 // Note that these field values are temporary and are
1252 // going to be overwritten on soinfo::prelink_image
1253 // with values from PT_LOAD segments.
1254 const ElfReader& elf_reader = task->get_elf_reader();
1255 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1256 if (d->d_tag == DT_RUNPATH) {
1257 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1258 }
1259 if (d->d_tag == DT_SONAME) {
1260 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1261 }
1262 }
1263
1264 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
1265 load_tasks->push_back(LoadTask::create(name, si, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001266 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001267
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001268 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001269}
1270
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001271static bool load_library(android_namespace_t* ns,
1272 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001273 ZipArchiveCache* zip_archive_cache,
1274 LoadTaskList* load_tasks,
1275 int rtld_flags) {
1276 const char* name = task->get_name();
1277 soinfo* needed_by = task->get_needed_by();
1278 const android_dlextinfo* extinfo = task->get_extinfo();
1279
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001280 off64_t file_offset;
1281 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001282 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001283 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001284 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1285 file_offset = extinfo->library_fd_offset;
1286 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001287
1288 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1289 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1290 "Will use given name.", name);
1291 realpath = name;
1292 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001293
1294 task->set_fd(extinfo->library_fd, false);
1295 task->set_file_offset(file_offset);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001296 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001297 }
1298
1299 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001300 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001301 if (fd == -1) {
1302 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001303 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001304 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001305
1306 task->set_fd(fd, true);
1307 task->set_file_offset(file_offset);
1308
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001309 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001310}
1311
Dimitry Ivanov3bd90612017-02-01 08:54:43 -08001312// Returns true if library was found and false otherwise
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001313static bool find_loaded_library_by_soname(android_namespace_t* ns,
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001314 const char* name, soinfo** candidate) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001315 *candidate = nullptr;
1316
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001317 // Ignore filename with path.
1318 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001319 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001320 }
1321
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001322 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001323 const char* soname = si->get_soname();
1324 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dimitry Ivanov3bd90612017-02-01 08:54:43 -08001325 *candidate = si;
1326 return false;
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001327 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001328
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001329 return true;
1330 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001331}
1332
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001333static bool find_library_in_linked_namespace(const android_namespace_link_t& namespace_link,
1334 LoadTask* task,
1335 int rtld_flags) {
1336 android_namespace_t* ns = namespace_link.linked_namespace();
1337
1338 soinfo* candidate;
1339 bool loaded = false;
1340
1341 std::string soname;
1342 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
1343 loaded = true;
1344 soname = candidate->get_soname();
1345 } else {
1346 soname = resolve_soname(task->get_name());
1347 }
1348
1349 if (!namespace_link.is_accessible(soname.c_str())) {
1350 // the library is not accessible via namespace_link
1351 return false;
1352 }
1353
1354 // if library is already loaded - return it
1355 if (loaded) {
1356 task->set_soinfo(candidate);
1357 return true;
1358 }
1359
1360 // try to load the library - once namespace boundary is crossed
1361 // we need to load a library within separate load_group
1362 // to avoid using symbols from foreign namespace while.
1363 //
1364 // All symbols during relocation should be resolved within a
1365 // namespace to preserve library locality to a namespace.
1366 const char* name = task->get_name();
1367 if (find_libraries(ns,
1368 task->get_needed_by(),
1369 &name,
1370 1,
1371 &candidate,
1372 nullptr /* ld_preloads */,
1373 0 /* ld_preload_count*/,
1374 rtld_flags,
1375 nullptr /* extinfo*/,
1376 false /* add_as_children */,
1377 false /* search_linked_namespaces */)) {
1378 task->set_soinfo(candidate);
1379 return true;
1380 }
1381
1382 return false;
1383}
1384
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001385static bool find_library_internal(android_namespace_t* ns,
1386 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001387 ZipArchiveCache* zip_archive_cache,
1388 LoadTaskList* load_tasks,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001389 int rtld_flags,
1390 bool search_linked_namespaces) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001391 soinfo* candidate;
1392
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001393 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001394 task->set_soinfo(candidate);
1395 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001396 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001397
1398 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001399 // of this fact is done by load_library.
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001400 TRACE("[ \"%s\" find_loaded_library_by_soname failed (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001401 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001402
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001403 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags)) {
1404 return true;
1405 }
1406
1407 if (search_linked_namespaces) {
1408 // if a library was not found - look into linked namespaces
1409 for (auto& linked_namespace : ns->linked_namespaces()) {
1410 if (find_library_in_linked_namespace(linked_namespace,
1411 task,
1412 rtld_flags)) {
1413 return true;
1414 }
1415 }
1416 }
1417
1418 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001419}
1420
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001421static void soinfo_unload(soinfo* si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001422static void soinfo_unload(soinfo* soinfos[], size_t count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001423
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001424// TODO: this is slightly unusual way to construct
1425// the global group for relocation. Not every RTLD_GLOBAL
1426// library is included in this group for backwards-compatibility
1427// reasons.
1428//
1429// This group consists of the main executable, LD_PRELOADs
1430// and libraries with the DF_1_GLOBAL flag set.
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001431static soinfo_list_t make_global_group(android_namespace_t* ns) {
1432 soinfo_list_t global_group;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001433 ns->soinfo_list().for_each([&](soinfo* si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001434 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1435 global_group.push_back(si);
1436 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001437 });
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001438
1439 return global_group;
1440}
1441
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001442// This function provides a list of libraries to be shared
1443// by the namespace. For the default namespace this is the global
1444// group (see make_global_group). For all others this is a group
1445// of RTLD_GLOBAL libraries (which includes the global group from
1446// the default namespace).
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001447static soinfo_list_t get_shared_group(android_namespace_t* ns) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001448 if (ns == &g_default_namespace) {
1449 return make_global_group(ns);
1450 }
1451
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001452 soinfo_list_t shared_group;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001453 ns->soinfo_list().for_each([&](soinfo* si) {
1454 if ((si->get_rtld_flags() & RTLD_GLOBAL) != 0) {
1455 shared_group.push_back(si);
1456 }
1457 });
1458
1459 return shared_group;
1460}
1461
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001462static void shuffle(std::vector<LoadTask*>* v) {
1463 for (size_t i = 0, size = v->size(); i < size; ++i) {
1464 size_t n = size - i;
1465 size_t r = arc4random_uniform(n);
1466 std::swap((*v)[n-1], (*v)[r]);
1467 }
1468}
1469
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001470// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1471// not their transitive dependencies) as children of the start_with library.
1472// This is false when find_libraries is called for dlopen(), when newly loaded
1473// libraries must form a disjoint tree.
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001474bool find_libraries(android_namespace_t* ns,
1475 soinfo* start_with,
1476 const char* const library_names[],
1477 size_t library_names_count,
1478 soinfo* soinfos[],
1479 std::vector<soinfo*>* ld_preloads,
1480 size_t ld_preloads_count,
1481 int rtld_flags,
1482 const android_dlextinfo* extinfo,
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001483 bool add_as_children,
1484 bool search_linked_namespaces) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001485 // Step 0: prepare.
1486 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001487 std::unordered_map<const soinfo*, ElfReader> readers_map;
1488
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001489 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001490 const char* name = library_names[i];
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001491 load_tasks.push_back(LoadTask::create(name, start_with, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001492 }
1493
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001494 // Construct global_group.
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001495 soinfo_list_t global_group = make_global_group(ns);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001496
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001497 // If soinfos array is null allocate one on stack.
1498 // The array is needed in case of failure; for example
1499 // when library_names[] = {libone.so, libtwo.so} and libone.so
1500 // is loaded correctly but libtwo.so failed for some reason.
1501 // In this case libone.so should be unloaded on return.
1502 // See also implementation of failure_guard below.
1503
1504 if (soinfos == nullptr) {
1505 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
1506 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
1507 memset(soinfos, 0, soinfos_size);
1508 }
1509
1510 // list of libraries to link - see step 2.
1511 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001512
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001513 auto scope_guard = make_scope_guard([&]() {
1514 for (LoadTask* t : load_tasks) {
1515 LoadTask::deleter(t);
1516 }
1517 });
1518
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07001519 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001520 // Housekeeping
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001521 soinfo_unload(soinfos, soinfos_count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001522 });
1523
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001524 ZipArchiveCache zip_archive_cache;
1525
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001526 // Step 1: expand the list of load_tasks to include
1527 // all DT_NEEDED libraries (do not load them just yet)
1528 for (size_t i = 0; i<load_tasks.size(); ++i) {
1529 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07001530 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001531
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001532 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001533 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001534 task->set_dt_needed(is_dt_needed);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001535
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001536 if (!find_library_internal(ns,
1537 task,
1538 &zip_archive_cache,
1539 &load_tasks,
1540 rtld_flags,
1541 search_linked_namespaces || is_dt_needed)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001542 return false;
1543 }
1544
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001545 soinfo* si = task->get_soinfo();
1546
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07001547 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001548 needed_by->add_child(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001549
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001550 if (si->is_linked()) {
1551 si->increment_ref_count();
1552 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001553 }
1554
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001555 // When ld_preloads is not null, the first
1556 // ld_preloads_count libs are in fact ld_preloads.
1557 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001558 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001559 }
1560
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001561 if (soinfos_count < library_names_count) {
1562 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001563 }
1564 }
1565
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001566 // Step 2: Load libraries in random order (see b/24047022)
1567 LoadTaskList load_list;
1568 for (auto&& task : load_tasks) {
1569 soinfo* si = task->get_soinfo();
1570 auto pred = [&](const LoadTask* t) {
1571 return t->get_soinfo() == si;
1572 };
1573
1574 if (!si->is_linked() &&
1575 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
1576 load_list.push_back(task);
1577 }
1578 }
1579 shuffle(&load_list);
1580
1581 for (auto&& task : load_list) {
1582 if (!task->load()) {
1583 return false;
1584 }
1585 }
1586
1587 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
1588 for (auto&& task : load_tasks) {
1589 soinfo* si = task->get_soinfo();
1590 if (!si->is_linked() && !si->prelink_image()) {
1591 return false;
1592 }
1593 }
1594
1595 // Step 4: Add LD_PRELOADed libraries to the global group for
1596 // future runs. There is no need to explicitly add them to
1597 // the global group for this run because they are going to
1598 // appear in the local group in the correct order.
1599 if (ld_preloads != nullptr) {
1600 for (auto&& si : *ld_preloads) {
1601 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
1602 }
1603 }
1604
1605
1606 // Step 5: link libraries.
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001607 soinfo_list_t local_group;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001608 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001609 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
1610 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001611 [&] (soinfo* si) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08001612 if (ns->is_accessible(si)) {
1613 local_group.push_back(si);
1614 return kWalkContinue;
1615 } else {
1616 return kWalkSkip;
1617 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001618 });
1619
1620 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001621 if (!si->is_linked()) {
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001622 if (!si->link_image(global_group, local_group, extinfo) ||
1623 !get_cfi_shadow()->AfterLoad(si, solist_get_head())) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001624 return false;
1625 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001626 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001627
1628 return true;
1629 });
1630
1631 if (linked) {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001632 local_group.for_each([](soinfo* si) {
1633 if (!si->is_linked()) {
1634 si->set_linked();
1635 }
1636 });
1637
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001638 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001639 }
1640
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001641 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001642}
1643
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001644static soinfo* find_library(android_namespace_t* ns,
1645 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001646 const android_dlextinfo* extinfo,
1647 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001648 soinfo* si;
1649
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001650 if (name == nullptr) {
Dimitry Ivanov3f660572016-09-09 10:00:39 -07001651 si = solist_get_somain();
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001652 } else if (!find_libraries(ns,
1653 needed_by,
1654 &name,
1655 1,
1656 &si,
1657 nullptr,
1658 0,
1659 rtld_flags,
1660 extinfo,
1661 false /* add_as_children */,
1662 true /* search_linked_namespaces */)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001663 return nullptr;
1664 }
1665
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08001666 si->increment_ref_count();
1667
Elliott Hughesd23736e2012-11-01 15:16:56 -07001668 return si;
1669}
Elliott Hughesbedfe382012-08-14 14:07:59 -07001670
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001671static void soinfo_unload(soinfo* root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001672 if (root->is_linked()) {
1673 root = root->get_local_group_root();
1674 }
1675
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07001676 ScopedTrace trace((std::string("unload ") + root->get_realpath()).c_str());
1677
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001678 if (!root->can_unload()) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001679 TRACE("not unloading \"%s\" - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001680 return;
1681 }
1682
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001683 soinfo_unload(&root, 1);
1684}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001685
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001686static void soinfo_unload(soinfo* soinfos[], size_t count) {
1687 // Note that the library can be loaded but not linked;
1688 // in which case there is no root but we still need
1689 // to walk the tree and unload soinfos involved.
1690 //
1691 // This happens on unsuccessful dlopen, when one of
1692 // the DT_NEEDED libraries could not be linked/found.
1693 if (count == 0) {
1694 return;
1695 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001696
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001697 soinfo_list_t unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001698 for (size_t i = 0; i < count; ++i) {
1699 soinfo* si = soinfos[i];
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001700
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001701 if (si->can_unload()) {
1702 size_t ref_count = si->is_linked() ? si->decrement_ref_count() : 0;
1703 if (ref_count == 0) {
1704 unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001705 } else {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001706 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
1707 si->get_realpath(), ref_count);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001708 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001709 } else {
1710 TRACE("not unloading '%s' - the binary is flagged with NODELETE", si->get_realpath());
1711 return;
1712 }
1713 }
1714
1715 // This is used to identify soinfos outside of the load-group
1716 // note that we cannot have > 1 in the array and have any of them
1717 // linked. This is why we can safely use the first one.
1718 soinfo* root = soinfos[0];
1719
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001720 soinfo_list_t local_unload_list;
1721 soinfo_list_t external_unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001722 soinfo* si = nullptr;
1723
1724 while ((si = unload_list.pop_front()) != nullptr) {
1725 if (local_unload_list.contains(si)) {
1726 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001727 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001728
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001729 local_unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001730
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001731 if (si->has_min_version(0)) {
1732 soinfo* child = nullptr;
1733 while ((child = si->get_children().pop_front()) != nullptr) {
1734 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
1735 child->get_realpath(), child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001736
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001737 child->get_parents().remove(si);
1738
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001739 if (local_unload_list.contains(child)) {
1740 continue;
1741 } else if (child->is_linked() && child->get_local_group_root() != root) {
1742 external_unload_list.push_back(child);
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001743 } else if (child->get_parents().empty()) {
1744 unload_list.push_back(child);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001745 }
1746 }
1747 } else {
1748#if !defined(__work_around_b_24465209__)
1749 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
1750#else
1751 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
1752 for_each_dt_needed(si, [&] (const char* library_name) {
1753 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
1754 si->get_realpath(), library_name);
1755
1756 soinfo* needed = find_library(si->get_primary_namespace(),
1757 library_name, RTLD_NOLOAD, nullptr, nullptr);
1758
1759 if (needed != nullptr) {
1760 // Not found: for example if symlink was deleted between dlopen and dlclose
1761 // Since we cannot really handle errors at this point - print and continue.
1762 PRINT("warning: couldn't find %s needed by %s on unload.",
1763 library_name, si->get_realpath());
1764 return;
1765 } else if (local_unload_list.contains(needed)) {
1766 // already visited
1767 return;
1768 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
1769 // external group
1770 external_unload_list.push_back(needed);
1771 } else {
1772 // local group
1773 unload_list.push_front(needed);
1774 }
1775 });
1776#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001777 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001778 }
1779
1780 local_unload_list.for_each([](soinfo* si) {
1781 si->call_destructors();
1782 });
1783
1784 while ((si = local_unload_list.pop_front()) != nullptr) {
1785 notify_gdb_of_unload(si);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001786 get_cfi_shadow()->BeforeUnload(si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001787 soinfo_free(si);
1788 }
1789
1790 while ((si = external_unload_list.pop_front()) != nullptr) {
1791 soinfo_unload(si);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001792 }
1793}
1794
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001795static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
1796 if (sym_ver == nullptr) {
1797 return sym_name;
1798 }
1799
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001800 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001801}
1802
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001803static android_namespace_t* get_caller_namespace(soinfo* caller) {
1804 return caller != nullptr ? caller->get_primary_namespace() : g_anonymous_namespace;
1805}
1806
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001807void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001808 // Use basic string manipulation calls to avoid snprintf.
1809 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1810 // When debug malloc is enabled, this call returns 0. This in turn causes
1811 // snprintf to do nothing, which causes libraries to fail to load.
1812 // See b/17302493 for further details.
1813 // Once the above bug is fixed, this code can be modified to use
1814 // snprintf again.
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001815 const auto& default_ld_paths = g_default_namespace.get_default_library_paths();
1816
1817 size_t required_size = 0;
1818 for (const auto& path : default_ld_paths) {
1819 required_size += path.size() + 1;
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001820 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001821
1822 if (buffer_size < required_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001823 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001824 "buffer len %zu, required len %zu", buffer_size, required_size);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001825 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001826
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001827 char* end = buffer;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001828 for (size_t i = 0; i < default_ld_paths.size(); ++i) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001829 if (i > 0) *end++ = ':';
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001830 end = stpcpy(end, default_ld_paths[i].c_str());
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001831 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001832}
1833
Elliott Hughescade4c32012-12-20 14:42:14 -08001834void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08001835 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08001836}
1837
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001838static std::string android_dlextinfo_to_string(const android_dlextinfo* info) {
1839 if (info == nullptr) {
1840 return "(null)";
1841 }
1842
1843 return android::base::StringPrintf("[flags=0x%" PRIx64 ","
1844 " reserved_addr=%p,"
1845 " reserved_size=0x%zx,"
1846 " relro_fd=%d,"
1847 " library_fd=%d,"
1848 " library_fd_offset=0x%" PRIx64 ","
1849 " library_namespace=%s@%p]",
1850 info->flags,
1851 info->reserved_addr,
1852 info->reserved_size,
1853 info->relro_fd,
1854 info->library_fd,
1855 info->library_fd_offset,
1856 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
1857 (info->library_namespace != nullptr ?
1858 info->library_namespace->get_name() : "(null)") : "(n/a)",
1859 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
1860 info->library_namespace : nullptr);
1861}
1862
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08001863void* do_dlopen(const char* name, int flags,
1864 const android_dlextinfo* extinfo,
1865 const void* caller_addr) {
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07001866 std::string trace_prefix = std::string("dlopen: ") + (name == nullptr ? "(nullptr)" : name);
1867 ScopedTrace trace(trace_prefix.c_str());
1868 ScopedTrace loading_trace((trace_prefix + " - loading and linking").c_str());
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001869 soinfo* const caller = find_containing_library(caller_addr);
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001870 android_namespace_t* ns = get_caller_namespace(caller);
1871
1872 LD_LOG(kLogDlopen,
1873 "dlopen(name=\"%s\", flags=0x%x, extinfo=%s, caller=\"%s\", caller_ns=%s@%p) ...",
1874 name,
1875 flags,
1876 android_dlextinfo_to_string(extinfo).c_str(),
1877 caller == nullptr ? "(null)" : caller->get_realpath(),
1878 ns == nullptr ? "(null)" : ns->get_name(),
1879 ns);
1880
1881 auto failure_guard = make_scope_guard([&]() {
1882 LD_LOG(kLogDlopen, "... dlopen failed: %s", linker_get_error_buffer());
1883 });
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001884
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001885 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001886 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001887 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001888 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001889
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001890 if (extinfo != nullptr) {
1891 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1892 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1893 return nullptr;
1894 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07001895
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001896 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001897 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001898 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
1899 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001900 return nullptr;
1901 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07001902
1903 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
1904 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
1905 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
1906 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
1907 return nullptr;
1908 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001909
1910 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
1911 if (extinfo->library_namespace == nullptr) {
1912 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
1913 return nullptr;
1914 }
1915 ns = extinfo->library_namespace;
1916 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001917 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001918
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001919 std::string asan_name_holder;
1920
1921 const char* translated_name = name;
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001922 if (g_is_asan && translated_name != nullptr && translated_name[0] == '/') {
1923 char translated_path[PATH_MAX];
1924 if (realpath(translated_name, translated_path) != nullptr) {
Evgenii Stepanov5b715002016-10-03 15:09:28 -07001925 if (file_is_under_dir(translated_path, kSystemLibDir)) {
1926 asan_name_holder = std::string(kAsanSystemLibDir) + "/" +
1927 (translated_path + strlen(kSystemLibDir) + 1);
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001928 if (file_exists(asan_name_holder.c_str())) {
1929 translated_name = asan_name_holder.c_str();
1930 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1931 }
Evgenii Stepanov5b715002016-10-03 15:09:28 -07001932 } else if (file_is_under_dir(translated_path, kVendorLibDir)) {
1933 asan_name_holder = std::string(kAsanVendorLibDir) + "/" +
1934 (translated_path + strlen(kVendorLibDir) + 1);
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001935 if (file_exists(asan_name_holder.c_str())) {
1936 translated_name = asan_name_holder.c_str();
1937 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1938 }
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001939 }
1940 }
1941 }
1942
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001943 ProtectedDataGuard guard;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001944 soinfo* si = find_library(ns, translated_name, flags, extinfo, caller);
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07001945 loading_trace.End();
1946
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001947 if (si != nullptr) {
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001948 void* handle = si->to_handle();
1949 LD_LOG(kLogDlopen,
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08001950 "... dlopen calling constructors: realpath=\"%s\", soname=\"%s\", handle=%p",
1951 si->get_realpath(), si->get_soname(), handle);
1952 si->call_constructors();
1953 failure_guard.disable();
1954 LD_LOG(kLogDlopen,
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001955 "... dlopen successful: realpath=\"%s\", soname=\"%s\", handle=%p",
1956 si->get_realpath(), si->get_soname(), handle);
1957 return handle;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001958 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001959
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001960 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001961}
1962
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001963int do_dladdr(const void* addr, Dl_info* info) {
1964 // Determine if this address can be found in any library currently mapped.
1965 soinfo* si = find_containing_library(addr);
1966 if (si == nullptr) {
1967 return 0;
1968 }
1969
1970 memset(info, 0, sizeof(Dl_info));
1971
1972 info->dli_fname = si->get_realpath();
1973 // Address at which the shared object is loaded.
1974 info->dli_fbase = reinterpret_cast<void*>(si->base);
1975
1976 // Determine if any symbol in the library contains the specified address.
1977 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
1978 if (sym != nullptr) {
1979 info->dli_sname = si->get_string(sym->st_name);
1980 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
1981 }
1982
1983 return 1;
1984}
1985
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001986static soinfo* soinfo_from_handle(void* handle) {
1987 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
1988 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
1989 if (it == g_soinfo_handles_map.end()) {
1990 return nullptr;
1991 } else {
1992 return it->second;
1993 }
1994 }
1995
1996 return static_cast<soinfo*>(handle);
1997}
1998
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08001999bool do_dlsym(void* handle,
2000 const char* sym_name,
2001 const char* sym_ver,
2002 const void* caller_addr,
2003 void** symbol) {
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07002004 ScopedTrace trace("dlsym");
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002005#if !defined(__LP64__)
2006 if (handle == nullptr) {
2007 DL_ERR("dlsym failed: library handle is null");
2008 return false;
2009 }
2010#endif
2011
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002012 soinfo* found = nullptr;
2013 const ElfW(Sym)* sym = nullptr;
2014 soinfo* caller = find_containing_library(caller_addr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002015 android_namespace_t* ns = get_caller_namespace(caller);
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002016 soinfo* si = nullptr;
2017 if (handle != RTLD_DEFAULT && handle != RTLD_NEXT) {
2018 si = soinfo_from_handle(handle);
2019 }
2020
2021 LD_LOG(kLogDlsym,
2022 "dlsym(handle=%p(\"%s\"), sym_name=\"%s\", sym_ver=\"%s\", caller=\"%s\", caller_ns=%s@%p) ...",
2023 handle,
2024 si != nullptr ? si->get_realpath() : "n/a",
2025 sym_name,
2026 sym_ver,
2027 caller == nullptr ? "(null)" : caller->get_realpath(),
2028 ns == nullptr ? "(null)" : ns->get_name(),
2029 ns);
2030
2031 auto failure_guard = make_scope_guard([&]() {
2032 LD_LOG(kLogDlsym, "... dlsym failed: %s", linker_get_error_buffer());
2033 });
2034
2035 if (sym_name == nullptr) {
2036 DL_ERR("dlsym failed: symbol name is null");
2037 return false;
2038 }
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002039
2040 version_info vi_instance;
2041 version_info* vi = nullptr;
2042
2043 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002044 vi_instance.name = sym_ver;
2045 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002046 vi = &vi_instance;
2047 }
2048
2049 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2050 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2051 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002052 if (si == nullptr) {
2053 DL_ERR("dlsym failed: invalid handle: %p", handle);
2054 return false;
2055 }
2056 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002057 }
2058
2059 if (sym != nullptr) {
2060 uint32_t bind = ELF_ST_BIND(sym->st_info);
2061
2062 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2063 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002064 failure_guard.disable();
2065 LD_LOG(kLogDlsym,
2066 "... dlsym successful: sym_name=\"%s\", sym_ver=\"%s\", found in=\"%s\", address=%p",
2067 sym_name, sym_ver, found->get_soname(), *symbol);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002068 return true;
2069 }
2070
2071 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2072 return false;
2073 }
2074
2075 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2076 return false;
2077}
2078
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002079int do_dlclose(void* handle) {
Dimitry Ivanov6705e8c2017-03-21 10:29:06 -07002080 ScopedTrace trace("dlclose");
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002081 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002082 soinfo* si = soinfo_from_handle(handle);
2083 if (si == nullptr) {
2084 DL_ERR("invalid handle: %p", handle);
2085 return -1;
2086 }
2087
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002088 soinfo_unload(si);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002089 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002090}
2091
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002092bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path) {
2093 if (g_anonymous_namespace_initialized) {
2094 DL_ERR("anonymous namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002095 return false;
2096 }
2097
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002098 ProtectedDataGuard guard;
2099
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002100 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002101 // When the caller is nullptr - create_namespace will take global group
2102 // from the anonymous namespace, which is fine because anonymous namespace
2103 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002104 android_namespace_t* anon_ns =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002105 create_namespace(nullptr,
2106 "(anonymous)",
2107 nullptr,
2108 library_search_path,
2109 // TODO (dimitry): change to isolated eventually.
2110 ANDROID_NAMESPACE_TYPE_REGULAR,
2111 nullptr,
2112 &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002113
2114 if (anon_ns == nullptr) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002115 return false;
2116 }
2117
2118 if (!link_namespaces(anon_ns, &g_default_namespace, shared_lib_sonames)) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002119 return false;
2120 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08002121
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002122 g_anonymous_namespace = anon_ns;
Dimitry Ivanov3e0821d2017-03-07 11:02:10 -08002123 g_anonymous_namespace_initialized = true;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002124
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002125 return true;
2126}
2127
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002128static void add_soinfos_to_namespace(const soinfo_list_t& soinfos, android_namespace_t* ns) {
2129 ns->add_soinfos(soinfos);
2130 for (auto si : soinfos) {
2131 si->add_secondary_namespace(ns);
2132 }
2133}
2134
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002135android_namespace_t* create_namespace(const void* caller_addr,
2136 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002137 const char* ld_library_path,
2138 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002139 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002140 const char* permitted_when_isolated_path,
2141 android_namespace_t* parent_namespace) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002142 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002143 // if parent_namespace is nullptr -> set it to the caller namespace
2144 soinfo* caller_soinfo = find_containing_library(caller_addr);
2145
2146 parent_namespace = caller_soinfo != nullptr ?
2147 caller_soinfo->get_primary_namespace() :
2148 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002149 }
2150
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002151 ProtectedDataGuard guard;
2152 std::vector<std::string> ld_library_paths;
2153 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002154 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002155
2156 parse_path(ld_library_path, ":", &ld_library_paths);
2157 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002158 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002159
2160 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2161 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002162 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002163 ns->set_ld_library_paths(std::move(ld_library_paths));
2164 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002165 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002166
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002167 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002168 // If shared - clone the parent namespace
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002169 add_soinfos_to_namespace(parent_namespace->soinfo_list(), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002170 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002171 // If not shared - copy only the shared group
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002172 add_soinfos_to_namespace(get_shared_group(parent_namespace), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002173 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002174
2175 return ns;
2176}
2177
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002178bool link_namespaces(android_namespace_t* namespace_from,
2179 android_namespace_t* namespace_to,
2180 const char* shared_lib_sonames) {
2181 if (namespace_to == nullptr) {
2182 namespace_to = &g_default_namespace;
2183 }
2184
2185 if (namespace_from == nullptr) {
2186 DL_ERR("error linking namespaces: namespace_from is null.");
2187 return false;
2188 }
2189
2190 if (shared_lib_sonames == nullptr || shared_lib_sonames[0] == '\0') {
2191 DL_ERR("error linking namespaces \"%s\"->\"%s\": the list of shared libraries is empty.",
2192 namespace_from->get_name(), namespace_to->get_name());
2193 return false;
2194 }
2195
2196 auto sonames = android::base::Split(shared_lib_sonames, ":");
2197 std::unordered_set<std::string> sonames_set(sonames.begin(), sonames.end());
2198
2199 ProtectedDataGuard guard;
2200 namespace_from->add_linked_namespace(namespace_to, sonames_set);
2201
2202 return true;
2203}
2204
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002205ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002206 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2207 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2208 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002209 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2210 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002211
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002212 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002213}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002214
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002215const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2216 if (source_symver < 2 ||
2217 source_symver >= version_infos.size() ||
2218 version_infos[source_symver].name == nullptr) {
2219 return nullptr;
2220 }
2221
2222 return &version_infos[source_symver];
2223}
2224
2225void VersionTracker::add_version_info(size_t source_index,
2226 ElfW(Word) elf_hash,
2227 const char* ver_name,
2228 const soinfo* target_si) {
2229 if (source_index >= version_infos.size()) {
2230 version_infos.resize(source_index+1);
2231 }
2232
2233 version_infos[source_index].elf_hash = elf_hash;
2234 version_infos[source_index].name = ver_name;
2235 version_infos[source_index].target_si = target_si;
2236}
2237
2238bool VersionTracker::init_verneed(const soinfo* si_from) {
2239 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2240
2241 if (verneed_ptr == 0) {
2242 return true;
2243 }
2244
2245 size_t verneed_cnt = si_from->get_verneed_cnt();
2246
2247 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2248 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2249 size_t vernaux_offset = offset + verneed->vn_aux;
2250 offset += verneed->vn_next;
2251
2252 if (verneed->vn_version != 1) {
2253 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2254 return false;
2255 }
2256
2257 const char* target_soname = si_from->get_string(verneed->vn_file);
2258 // find it in dependencies
2259 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002260 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002261 });
2262
2263 if (target_si == nullptr) {
2264 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002265 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002266 return false;
2267 }
2268
2269 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2270 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2271 vernaux_offset += vernaux->vna_next;
2272
2273 const ElfW(Word) elf_hash = vernaux->vna_hash;
2274 const char* ver_name = si_from->get_string(vernaux->vna_name);
2275 ElfW(Half) source_index = vernaux->vna_other;
2276
2277 add_version_info(source_index, elf_hash, ver_name, target_si);
2278 }
2279 }
2280
2281 return true;
2282}
2283
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002284template <typename F>
2285static bool for_each_verdef(const soinfo* si, F functor) {
2286 if (!si->has_min_version(2)) {
2287 return true;
2288 }
2289
2290 uintptr_t verdef_ptr = si->get_verdef_ptr();
2291 if (verdef_ptr == 0) {
2292 return true;
2293 }
2294
2295 size_t offset = 0;
2296
2297 size_t verdef_cnt = si->get_verdef_cnt();
2298 for (size_t i = 0; i<verdef_cnt; ++i) {
2299 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
2300 size_t verdaux_offset = offset + verdef->vd_aux;
2301 offset += verdef->vd_next;
2302
2303 if (verdef->vd_version != 1) {
2304 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
2305 i, verdef->vd_version, si->get_realpath());
2306 return false;
2307 }
2308
2309 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
2310 // "this is the version of the file itself. It must not be used for
2311 // matching a symbol. It can be used to match references."
2312 //
2313 // http://www.akkadia.org/drepper/symbol-versioning
2314 continue;
2315 }
2316
2317 if (verdef->vd_cnt == 0) {
2318 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
2319 return false;
2320 }
2321
2322 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
2323
2324 if (functor(i, verdef, verdaux) == true) {
2325 break;
2326 }
2327 }
2328
2329 return true;
2330}
2331
2332bool find_verdef_version_index(const soinfo* si, const version_info* vi, ElfW(Versym)* versym) {
2333 if (vi == nullptr) {
2334 *versym = kVersymNotNeeded;
2335 return true;
2336 }
2337
2338 *versym = kVersymGlobal;
2339
2340 return for_each_verdef(si,
2341 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2342 if (verdef->vd_hash == vi->elf_hash &&
2343 strcmp(vi->name, si->get_string(verdaux->vda_name)) == 0) {
2344 *versym = verdef->vd_ndx;
2345 return true;
2346 }
2347
2348 return false;
2349 }
2350 );
2351}
2352
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002353bool VersionTracker::init_verdef(const soinfo* si_from) {
2354 return for_each_verdef(si_from,
2355 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2356 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2357 si_from->get_string(verdaux->vda_name), si_from);
2358 return false;
2359 }
2360 );
2361}
2362
2363bool VersionTracker::init(const soinfo* si_from) {
2364 if (!si_from->has_min_version(2)) {
2365 return true;
2366 }
2367
2368 return init_verneed(si_from) && init_verdef(si_from);
2369}
2370
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002371// TODO (dimitry): Methods below need to be moved out of soinfo
2372// and in more isolated file in order minimize dependencies on
2373// unnecessary object in the linker binary. Consider making them
2374// independent from soinfo (?).
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002375bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2376 const char* sym_name, const version_info** vi) {
2377 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2378 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2379
2380 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2381 *vi = version_tracker.get_version_info(sym_ver);
2382
2383 if (*vi == nullptr) {
2384 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002385 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002386 return false;
2387 }
2388 } else {
2389 // there is no version info
2390 *vi = nullptr;
2391 }
2392
2393 return true;
2394}
2395
Dimitry Ivanov576a3752016-08-09 06:58:55 -07002396#if !defined(__mips__)
2397#if defined(USE_RELA)
2398static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2399 return rela->r_addend;
2400}
2401#else
2402static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
2403 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2404 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
2405 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2406 }
2407 return 0;
2408}
2409#endif
2410
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002411template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002412bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2413 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002414 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2415 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002416 if (rel == nullptr) {
2417 return false;
2418 }
2419
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002420 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2421 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2422
2423 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002424 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002425 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002426 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002427
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002428 DEBUG("Processing \"%s\" relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002429 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002430 continue;
2431 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002432
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002433 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002434 soinfo* lsi = nullptr;
2435
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002436 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002437 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002438 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002439
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002440 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2441 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002442 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002443
2444 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2445 return false;
2446 }
2447
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002448 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002449 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002450 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002451 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002452 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002453 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002454 }
2455
2456 /* IHI0044C AAELF 4.5.1.1:
2457
2458 Libraries are not searched to resolve weak references.
2459 It is not an error for a weak reference to remain unsatisfied.
2460
2461 During linking, the value of an undefined weak reference is:
2462 - Zero if the relocation type is absolute
2463 - The address of the place if the relocation is pc-relative
2464 - The address of nominal base address if the relocation
2465 type is base-relative.
2466 */
2467
2468 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002469 case R_GENERIC_JUMP_SLOT:
2470 case R_GENERIC_GLOB_DAT:
2471 case R_GENERIC_RELATIVE:
2472 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002473#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002474 case R_AARCH64_ABS64:
2475 case R_AARCH64_ABS32:
2476 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002477#elif defined(__x86_64__)
2478 case R_X86_64_32:
2479 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002480#elif defined(__arm__)
2481 case R_ARM_ABS32:
2482#elif defined(__i386__)
2483 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002484#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002485 /*
2486 * The sym_addr was initialized to be zero above, or the relocation
2487 * code below does not care about value of sym_addr.
2488 * No need to do anything.
2489 */
2490 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002491#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002492 case R_X86_64_PC32:
2493 sym_addr = reloc;
2494 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002495#elif defined(__i386__)
2496 case R_386_PC32:
2497 sym_addr = reloc;
2498 break;
2499#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002500 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002501 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002502 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002503 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002504 } else { // We got a definition.
2505#if !defined(__LP64__)
2506 // When relocating dso with text_relocation .text segment is
2507 // not executable. We need to restore elf flags before resolving
2508 // STT_GNU_IFUNC symbol.
2509 bool protect_segments = has_text_relocations &&
2510 lsi == this &&
2511 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2512 if (protect_segments) {
2513 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2514 DL_ERR("can't protect segments for \"%s\": %s",
2515 get_realpath(), strerror(errno));
2516 return false;
2517 }
2518 }
2519#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002520 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002521#if !defined(__LP64__)
2522 if (protect_segments) {
2523 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2524 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2525 get_realpath(), strerror(errno));
2526 return false;
2527 }
2528 }
2529#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002530 }
2531 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002532 }
2533
2534 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002535 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002536 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002537 MARK(rel->r_offset);
2538 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2539 reinterpret_cast<void*>(reloc),
2540 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2541
2542 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002543 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002544 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002545 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002546 MARK(rel->r_offset);
2547 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2548 reinterpret_cast<void*>(reloc),
2549 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2550 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002551 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002552 case R_GENERIC_RELATIVE:
2553 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002554 MARK(rel->r_offset);
2555 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2556 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002557 reinterpret_cast<void*>(load_bias + addend));
2558 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002559 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002560 case R_GENERIC_IRELATIVE:
2561 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002562 MARK(rel->r_offset);
2563 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2564 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002565 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002566 {
2567#if !defined(__LP64__)
2568 // When relocating dso with text_relocation .text segment is
2569 // not executable. We need to restore elf flags for this
2570 // particular call.
2571 if (has_text_relocations) {
2572 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2573 DL_ERR("can't protect segments for \"%s\": %s",
2574 get_realpath(), strerror(errno));
2575 return false;
2576 }
2577 }
2578#endif
2579 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2580#if !defined(__LP64__)
2581 // Unprotect it afterwards...
2582 if (has_text_relocations) {
2583 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2584 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2585 get_realpath(), strerror(errno));
2586 return false;
2587 }
2588 }
2589#endif
2590 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2591 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002592 break;
2593
2594#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002595 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002596 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002597 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002598 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002599 reloc, sym_addr + addend, sym_name);
2600 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002601 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002602 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002603 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002604 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002605 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002606 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002607 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002608 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2609 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002610 if ((min_value <= (sym_addr + addend)) &&
2611 ((sym_addr + addend) <= max_value)) {
2612 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002613 } else {
2614 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002615 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002616 return false;
2617 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002618 }
2619 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002620 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002621 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002622 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002623 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002624 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002625 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002626 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2627 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002628 if ((min_value <= (sym_addr + addend)) &&
2629 ((sym_addr + addend) <= max_value)) {
2630 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002631 } else {
2632 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002633 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002634 return false;
2635 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002636 }
2637 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002638 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002639 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002640 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002641 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002642 reloc, sym_addr + addend, rel->r_offset, sym_name);
2643 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002644 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002645 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002646 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002647 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002648 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002649 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002650 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002651 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2652 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002653 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2654 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2655 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002656 } else {
2657 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002658 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002659 return false;
2660 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002661 }
2662 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002663 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002664 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002665 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002666 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002667 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002668 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002669 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2670 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002671 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2672 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2673 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002674 } else {
2675 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002676 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002677 return false;
2678 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002679 }
2680 break;
2681
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002682 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002683 /*
2684 * ET_EXEC is not supported so this should not happen.
2685 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002686 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002687 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002688 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002689 * R_AARCH64_COPY may only appear in executable objects where e_type is
2690 * set to ET_EXEC.
2691 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002692 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002693 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002694 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002695 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002696 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002697 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002698 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002699 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002700 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002701 break;
2702#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002703 case R_X86_64_32:
2704 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002705 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002706 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2707 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002708 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002709 break;
2710 case R_X86_64_64:
2711 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002712 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002713 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2714 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002715 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002716 break;
2717 case R_X86_64_PC32:
2718 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002719 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002720 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2721 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2722 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002723 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002724 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002725#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002726 case R_ARM_ABS32:
2727 count_relocation(kRelocAbsolute);
2728 MARK(rel->r_offset);
2729 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2730 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2731 break;
2732 case R_ARM_REL32:
2733 count_relocation(kRelocRelative);
2734 MARK(rel->r_offset);
2735 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2736 reloc, sym_addr, rel->r_offset, sym_name);
2737 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2738 break;
2739 case R_ARM_COPY:
2740 /*
2741 * ET_EXEC is not supported so this should not happen.
2742 *
2743 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2744 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002745 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002746 * R_ARM_COPY may only appear in executable objects where e_type is
2747 * set to ET_EXEC.
2748 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002749 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002750 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002751#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002752 case R_386_32:
2753 count_relocation(kRelocRelative);
2754 MARK(rel->r_offset);
2755 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2756 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2757 break;
2758 case R_386_PC32:
2759 count_relocation(kRelocRelative);
2760 MARK(rel->r_offset);
2761 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2762 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2763 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2764 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002765#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002766 default:
2767 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002768 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002769 }
2770 }
2771 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002772}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002773#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002774
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002775// An empty list of soinfos
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002776static soinfo_list_t g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002777
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002778bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002779 /* Extract dynamic section */
2780 ElfW(Word) dynamic_flags = 0;
2781 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002782
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002783 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002784 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002785 if (!relocating_linker) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002786 INFO("[ Linking \"%s\" ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002787 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002788 }
2789
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002790 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002791 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002792 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002793 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002794 return false;
2795 } else {
2796 if (!relocating_linker) {
2797 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002798 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002799 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002800
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002801#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002802 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2803 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002804#endif
2805
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002806 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002807 // Note that: "Except for the DT_NULL element at the end of the array,
2808 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2809 //
2810 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002811 uint32_t needed_count = 0;
2812 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2813 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2814 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2815 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002816 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002817 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002818 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002819
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002820 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002821 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2822 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2823 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2824 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002825 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002826
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002827 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002828 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002829 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002830 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2831 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002832
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002833 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002834 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002835 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002836 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
2837 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002838
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002839 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002840 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002841 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002842 return false;
2843 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002844 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002845
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002846 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002847 break;
2848
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002849 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002850 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002851 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002852
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002853 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002854 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002855 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002856
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002857 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002858 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002859 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002860
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002861 case DT_SYMENT:
2862 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002863 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
2864 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002865 return false;
2866 }
2867 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002868
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002869 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002870#if defined(USE_RELA)
2871 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002872 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002873 return false;
2874 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002875#else
2876 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002877 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002878 return false;
2879 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002880#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002881 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002882
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002883 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002884#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002885 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002886#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002887 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002888#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002889 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002890
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002891 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002892#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002893 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002894#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002895 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002896#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002897 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002898
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002899 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002900#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002901 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002902 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002903#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002904 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2905 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002906
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002907 case DT_DEBUG:
2908 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2909 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002910// FIXME: not working currently for N64
2911// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002912// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002913// read-only, but the DYNAMIC header claims it is writable.
2914#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002915 if ((dynamic_flags & PF_W) != 0) {
2916 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2917 }
Chris Dearman99186652014-02-06 20:36:51 -08002918#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002919 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002920#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002921 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002922 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002923 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002924
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002925 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002926 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002927 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002928
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002929 case DT_ANDROID_RELA:
2930 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2931 break;
2932
2933 case DT_ANDROID_RELASZ:
2934 android_relocs_size_ = d->d_un.d_val;
2935 break;
2936
2937 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002938 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002939 return false;
2940
2941 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002942 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002943 return false;
2944
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002945 case DT_RELAENT:
2946 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002947 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002948 return false;
2949 }
2950 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002951
2952 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002953 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002954 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002955
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002956 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002957 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002958 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002959
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002960 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002961 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002962 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002963
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002964#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002965 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002966 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002967 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002968
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002969 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002970 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002971 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002972
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002973 case DT_RELENT:
2974 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002975 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002976 return false;
2977 }
2978 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002979
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002980 case DT_ANDROID_REL:
2981 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2982 break;
2983
2984 case DT_ANDROID_RELSZ:
2985 android_relocs_size_ = d->d_un.d_val;
2986 break;
2987
2988 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002989 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002990 return false;
2991
2992 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002993 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002994 return false;
2995
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002996 // "Indicates that all RELATIVE relocations have been concatenated together,
2997 // and specifies the RELATIVE relocation count."
2998 //
2999 // TODO: Spec also mentions that this can be used to optimize relocation process;
3000 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003001 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003002 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003003
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003004 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003005 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003006 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003007
3008 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003009 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003010 return false;
3011
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003012#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003013 case DT_INIT:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003014 init_func_ = reinterpret_cast<linker_ctor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003015 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003016 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003017
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003018 case DT_FINI:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003019 fini_func_ = reinterpret_cast<linker_dtor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003020 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003021 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003022
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003023 case DT_INIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003024 init_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003025 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003026 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003027
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003028 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003029 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003030 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003031
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003032 case DT_FINI_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003033 fini_array_ = reinterpret_cast<linker_dtor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003034 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003035 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003036
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003037 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003038 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003039 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003040
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003041 case DT_PREINIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003042 preinit_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003043 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003044 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003045
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003046 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003047 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003048 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003049
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003050 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003051#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003052 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003053 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003054#else
3055 has_text_relocations = true;
3056 break;
3057#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003058
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003059 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003060 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003061 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003062
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003063 case DT_NEEDED:
3064 ++needed_count;
3065 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003066
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003067 case DT_FLAGS:
3068 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003069#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003070 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003071 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003072#else
3073 has_text_relocations = true;
3074#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003075 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003076 if (d->d_un.d_val & DF_SYMBOLIC) {
3077 has_DT_SYMBOLIC = true;
3078 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003079 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003080
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003081 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003082 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003083
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003084 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003085 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 -07003086 }
3087 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003088#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003089 case DT_MIPS_RLD_MAP:
3090 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3091 {
3092 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3093 *dp = &_r_debug;
3094 }
3095 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003096 case DT_MIPS_RLD_MAP_REL:
3097 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003098 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003099 r_debug** dp = reinterpret_cast<r_debug**>(
3100 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003101 *dp = &_r_debug;
3102 }
3103 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003104
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003105 case DT_MIPS_RLD_VERSION:
3106 case DT_MIPS_FLAGS:
3107 case DT_MIPS_BASE_ADDRESS:
3108 case DT_MIPS_UNREFEXTNO:
3109 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003110
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003111 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003112 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003113 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003114
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003115 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003116 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003117 break;
3118
3119 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003120 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003121 break;
3122#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003123 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3124 case DT_BIND_NOW:
3125 break;
3126
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003127 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003128 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3129 break;
3130
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003131 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003132 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3133 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003134 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003135 verdef_cnt_ = d->d_un.d_val;
3136 break;
3137
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003138 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003139 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3140 break;
3141
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003142 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003143 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003144 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003145
Evgenii Stepanov68650822015-06-10 13:38:39 -07003146 case DT_RUNPATH:
3147 // this is parsed after we have strtab initialized (see below).
3148 break;
3149
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003150 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003151 if (!relocating_linker) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003152 DL_WARN("\"%s\" unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003153 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3154 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003155 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003156 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003157 }
3158
Duane Sandbc425c72015-06-01 16:29:14 -07003159#if defined(__mips__) && !defined(__LP64__)
3160 if (!mips_check_and_adjust_fp_modes()) {
3161 return false;
3162 }
3163#endif
3164
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003165 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003166 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003167
3168 // Sanity checks.
3169 if (relocating_linker && needed_count != 0) {
3170 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3171 return false;
3172 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003173 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003174 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003175 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003176 return false;
3177 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003178 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003179 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003180 return false;
3181 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003182 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003183 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003184 return false;
3185 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003186
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003187 // second pass - parse entries relying on strtab
3188 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003189 switch (d->d_tag) {
3190 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003191 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003192 break;
3193 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003194 set_dt_runpath(get_string(d->d_un.d_val));
3195 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003196 }
3197 }
3198
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003199 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003200 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003201 // because they can't find dt_needed library by soname.
3202 // This workaround should keep them working. (applies only
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003203 // for apps targeting sdk version < M). Make an exception for
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003204 // the main executable and linker; they do not need to have dt_soname
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003205 if (soname_ == nullptr &&
3206 this != solist_get_somain() &&
3207 (flags_ & FLAG_LINKER) == 0 &&
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003208 get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003209 soname_ = basename(realpath_.c_str());
3210 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3211 get_realpath(), soname_);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003212 // 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 -07003213 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003214 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003215}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003216
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003217bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3218 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003219
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003220 local_group_root_ = local_group.front();
3221 if (local_group_root_ == nullptr) {
3222 local_group_root_ = this;
3223 }
3224
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003225 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3226 target_sdk_version_ = get_application_target_sdk_version();
3227 }
3228
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003229 VersionTracker version_tracker;
3230
3231 if (!version_tracker.init(this)) {
3232 return false;
3233 }
3234
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003235#if !defined(__LP64__)
3236 if (has_text_relocations) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003237 // Fail if app is targeting M or above.
3238 if (get_application_target_sdk_version() >= __ANDROID_API_M__) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003239 DL_ERR_AND_LOG("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003240 return false;
3241 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003242 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003243 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003244 DL_WARN("\"%s\" has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003245 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003246 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003247 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3248 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003249 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003250 return false;
3251 }
3252 }
3253#endif
3254
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003255 if (android_relocs_ != nullptr) {
3256 // check signature
3257 if (android_relocs_size_ > 3 &&
3258 android_relocs_[0] == 'A' &&
3259 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003260 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003261 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003262 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003263
3264 bool relocated = false;
3265 const uint8_t* packed_relocs = android_relocs_ + 4;
3266 const size_t packed_relocs_size = android_relocs_size_ - 4;
3267
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003268 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003269 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003270 packed_reloc_iterator<sleb128_decoder>(
3271 sleb128_decoder(packed_relocs, packed_relocs_size)),
3272 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003273
3274 if (!relocated) {
3275 return false;
3276 }
3277 } else {
3278 DL_ERR("bad android relocation header.");
3279 return false;
3280 }
3281 }
3282
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003283#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003284 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003285 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003286 if (!relocate(version_tracker,
3287 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003288 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003289 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003290 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003291 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003292 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003293 if (!relocate(version_tracker,
3294 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003295 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003296 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003297 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003298#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003299 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003300 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003301 if (!relocate(version_tracker,
3302 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003303 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003304 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003305 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003306 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003307 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003308 if (!relocate(version_tracker,
3309 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003310 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003311 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003312 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003313#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003314
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003315#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003316 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003317 return false;
3318 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003319#endif
3320
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003321 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003322
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003323#if !defined(__LP64__)
3324 if (has_text_relocations) {
3325 // All relocations are done, we can protect our segments back to read-only.
3326 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3327 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003328 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003329 return false;
3330 }
3331 }
3332#endif
3333
Mingwei Shibe910522015-11-12 07:02:14 +00003334 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3335 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3336 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003337 return false;
3338 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003339
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003340 /* Handle serializing/sharing the RELRO segment */
3341 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3342 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3343 extinfo->relro_fd) < 0) {
3344 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003345 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003346 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003347 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003348 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3349 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3350 extinfo->relro_fd) < 0) {
3351 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003352 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003353 return false;
3354 }
3355 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003356
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003357 notify_gdb_of_load(this);
3358 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003359}
3360
Mingwei Shibe910522015-11-12 07:02:14 +00003361bool soinfo::protect_relro() {
3362 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3363 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3364 get_realpath(), strerror(errno));
3365 return false;
3366 }
3367 return true;
3368}
3369
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003370void init_default_namespace() {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003371 g_default_namespace.set_name("(default)");
3372 g_default_namespace.set_isolated(false);
3373
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003374 soinfo* somain = solist_get_somain();
3375
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003376 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3377 somain->load_bias);
3378 const char* bname = basename(interp);
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003379
3380 bool is_asan = bname != nullptr &&
3381 (strcmp(bname, "linker_asan") == 0 ||
3382 strcmp(bname, "linker_asan64") == 0);
3383 auto default_ld_paths = is_asan ? kAsanDefaultLdPaths : kDefaultLdPaths;
3384 g_is_asan = is_asan;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003385
neo.chae2589f9d2016-10-04 11:00:27 +09003386 char real_path[PATH_MAX];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003387 std::vector<std::string> ld_default_paths;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003388 for (size_t i = 0; default_ld_paths[i] != nullptr; ++i) {
3389 if (realpath(default_ld_paths[i], real_path) != nullptr) {
neo.chae2589f9d2016-10-04 11:00:27 +09003390 ld_default_paths.push_back(real_path);
3391 } else {
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003392 ld_default_paths.push_back(default_ld_paths[i]);
neo.chae2589f9d2016-10-04 11:00:27 +09003393 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003394 }
3395
3396 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003397};