blob: a05cd3b8a730613c2d6bfc0a3e22677e713d5e7a [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
1676 if (!root->can_unload()) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001677 TRACE("not unloading \"%s\" - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001678 return;
1679 }
1680
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001681 soinfo_unload(&root, 1);
1682}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001683
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001684static void soinfo_unload(soinfo* soinfos[], size_t count) {
1685 // Note that the library can be loaded but not linked;
1686 // in which case there is no root but we still need
1687 // to walk the tree and unload soinfos involved.
1688 //
1689 // This happens on unsuccessful dlopen, when one of
1690 // the DT_NEEDED libraries could not be linked/found.
1691 if (count == 0) {
1692 return;
1693 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001694
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001695 soinfo_list_t unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001696 for (size_t i = 0; i < count; ++i) {
1697 soinfo* si = soinfos[i];
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08001698
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001699 if (si->can_unload()) {
1700 size_t ref_count = si->is_linked() ? si->decrement_ref_count() : 0;
1701 if (ref_count == 0) {
1702 unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001703 } else {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001704 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
1705 si->get_realpath(), ref_count);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001706 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001707 } else {
1708 TRACE("not unloading '%s' - the binary is flagged with NODELETE", si->get_realpath());
1709 return;
1710 }
1711 }
1712
1713 // This is used to identify soinfos outside of the load-group
1714 // note that we cannot have > 1 in the array and have any of them
1715 // linked. This is why we can safely use the first one.
1716 soinfo* root = soinfos[0];
1717
Dimitry Ivanovb943f302016-08-03 16:00:10 -07001718 soinfo_list_t local_unload_list;
1719 soinfo_list_t external_unload_list;
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001720 soinfo* si = nullptr;
1721
1722 while ((si = unload_list.pop_front()) != nullptr) {
1723 if (local_unload_list.contains(si)) {
1724 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001725 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001726
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001727 local_unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001728
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001729 if (si->has_min_version(0)) {
1730 soinfo* child = nullptr;
1731 while ((child = si->get_children().pop_front()) != nullptr) {
1732 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
1733 child->get_realpath(), child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001734
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001735 child->get_parents().remove(si);
1736
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001737 if (local_unload_list.contains(child)) {
1738 continue;
1739 } else if (child->is_linked() && child->get_local_group_root() != root) {
1740 external_unload_list.push_back(child);
Dimitry Ivanovec90e242017-02-10 11:04:20 -08001741 } else if (child->get_parents().empty()) {
1742 unload_list.push_back(child);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001743 }
1744 }
1745 } else {
1746#if !defined(__work_around_b_24465209__)
1747 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
1748#else
1749 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
1750 for_each_dt_needed(si, [&] (const char* library_name) {
1751 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
1752 si->get_realpath(), library_name);
1753
1754 soinfo* needed = find_library(si->get_primary_namespace(),
1755 library_name, RTLD_NOLOAD, nullptr, nullptr);
1756
1757 if (needed != nullptr) {
1758 // Not found: for example if symlink was deleted between dlopen and dlclose
1759 // Since we cannot really handle errors at this point - print and continue.
1760 PRINT("warning: couldn't find %s needed by %s on unload.",
1761 library_name, si->get_realpath());
1762 return;
1763 } else if (local_unload_list.contains(needed)) {
1764 // already visited
1765 return;
1766 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
1767 // external group
1768 external_unload_list.push_back(needed);
1769 } else {
1770 // local group
1771 unload_list.push_front(needed);
1772 }
1773 });
1774#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08001775 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001776 }
1777
1778 local_unload_list.for_each([](soinfo* si) {
1779 si->call_destructors();
1780 });
1781
1782 while ((si = local_unload_list.pop_front()) != nullptr) {
1783 notify_gdb_of_unload(si);
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -07001784 get_cfi_shadow()->BeforeUnload(si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001785 soinfo_free(si);
1786 }
1787
1788 while ((si = external_unload_list.pop_front()) != nullptr) {
1789 soinfo_unload(si);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08001790 }
1791}
1792
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001793static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
1794 if (sym_ver == nullptr) {
1795 return sym_name;
1796 }
1797
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08001798 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001799}
1800
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001801static android_namespace_t* get_caller_namespace(soinfo* caller) {
1802 return caller != nullptr ? caller->get_primary_namespace() : g_anonymous_namespace;
1803}
1804
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001805void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001806 // Use basic string manipulation calls to avoid snprintf.
1807 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
1808 // When debug malloc is enabled, this call returns 0. This in turn causes
1809 // snprintf to do nothing, which causes libraries to fail to load.
1810 // See b/17302493 for further details.
1811 // Once the above bug is fixed, this code can be modified to use
1812 // snprintf again.
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001813 const auto& default_ld_paths = g_default_namespace.get_default_library_paths();
1814
1815 size_t required_size = 0;
1816 for (const auto& path : default_ld_paths) {
1817 required_size += path.size() + 1;
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001818 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001819
1820 if (buffer_size < required_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001821 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001822 "buffer len %zu, required len %zu", buffer_size, required_size);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07001823 }
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001824
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001825 char* end = buffer;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001826 for (size_t i = 0; i < default_ld_paths.size(); ++i) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001827 if (i > 0) *end++ = ':';
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08001828 end = stpcpy(end, default_ld_paths[i].c_str());
Evgenii Stepanovd640b222015-07-10 17:54:01 -07001829 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08001830}
1831
Elliott Hughescade4c32012-12-20 14:42:14 -08001832void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08001833 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08001834}
1835
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001836static std::string android_dlextinfo_to_string(const android_dlextinfo* info) {
1837 if (info == nullptr) {
1838 return "(null)";
1839 }
1840
1841 return android::base::StringPrintf("[flags=0x%" PRIx64 ","
1842 " reserved_addr=%p,"
1843 " reserved_size=0x%zx,"
1844 " relro_fd=%d,"
1845 " library_fd=%d,"
1846 " library_fd_offset=0x%" PRIx64 ","
1847 " library_namespace=%s@%p]",
1848 info->flags,
1849 info->reserved_addr,
1850 info->reserved_size,
1851 info->relro_fd,
1852 info->library_fd,
1853 info->library_fd_offset,
1854 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
1855 (info->library_namespace != nullptr ?
1856 info->library_namespace->get_name() : "(null)") : "(n/a)",
1857 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
1858 info->library_namespace : nullptr);
1859}
1860
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08001861void* do_dlopen(const char* name, int flags,
1862 const android_dlextinfo* extinfo,
1863 const void* caller_addr) {
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07001864 std::string trace_prefix = std::string("dlopen: ") + (name == nullptr ? "(nullptr)" : name);
1865 ScopedTrace trace(trace_prefix.c_str());
1866 ScopedTrace loading_trace((trace_prefix + " - loading and linking").c_str());
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001867 soinfo* const caller = find_containing_library(caller_addr);
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001868 android_namespace_t* ns = get_caller_namespace(caller);
1869
1870 LD_LOG(kLogDlopen,
1871 "dlopen(name=\"%s\", flags=0x%x, extinfo=%s, caller=\"%s\", caller_ns=%s@%p) ...",
1872 name,
1873 flags,
1874 android_dlextinfo_to_string(extinfo).c_str(),
1875 caller == nullptr ? "(null)" : caller->get_realpath(),
1876 ns == nullptr ? "(null)" : ns->get_name(),
1877 ns);
1878
1879 auto failure_guard = make_scope_guard([&]() {
1880 LD_LOG(kLogDlopen, "... dlopen failed: %s", linker_get_error_buffer());
1881 });
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001882
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001883 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001884 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001885 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001886 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001887
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001888 if (extinfo != nullptr) {
1889 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1890 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1891 return nullptr;
1892 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07001893
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001894 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001895 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001896 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
1897 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001898 return nullptr;
1899 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07001900
1901 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
1902 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
1903 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
1904 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
1905 return nullptr;
1906 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001907
1908 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
1909 if (extinfo->library_namespace == nullptr) {
1910 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
1911 return nullptr;
1912 }
1913 ns = extinfo->library_namespace;
1914 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001915 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001916
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001917 std::string asan_name_holder;
1918
1919 const char* translated_name = name;
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001920 if (g_is_asan && translated_name != nullptr && translated_name[0] == '/') {
1921 char translated_path[PATH_MAX];
1922 if (realpath(translated_name, translated_path) != nullptr) {
Evgenii Stepanov5b715002016-10-03 15:09:28 -07001923 if (file_is_under_dir(translated_path, kSystemLibDir)) {
1924 asan_name_holder = std::string(kAsanSystemLibDir) + "/" +
1925 (translated_path + strlen(kSystemLibDir) + 1);
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001926 if (file_exists(asan_name_holder.c_str())) {
1927 translated_name = asan_name_holder.c_str();
1928 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1929 }
Evgenii Stepanov5b715002016-10-03 15:09:28 -07001930 } else if (file_is_under_dir(translated_path, kVendorLibDir)) {
1931 asan_name_holder = std::string(kAsanVendorLibDir) + "/" +
1932 (translated_path + strlen(kVendorLibDir) + 1);
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001933 if (file_exists(asan_name_holder.c_str())) {
1934 translated_name = asan_name_holder.c_str();
1935 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1936 }
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001937 }
1938 }
1939 }
1940
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001941 ProtectedDataGuard guard;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001942 soinfo* si = find_library(ns, translated_name, flags, extinfo, caller);
Dimitry Ivanov5c4a5802017-03-17 16:41:34 -07001943 loading_trace.End();
1944
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001945 if (si != nullptr) {
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001946 void* handle = si->to_handle();
1947 LD_LOG(kLogDlopen,
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08001948 "... dlopen calling constructors: realpath=\"%s\", soname=\"%s\", handle=%p",
1949 si->get_realpath(), si->get_soname(), handle);
1950 si->call_constructors();
1951 failure_guard.disable();
1952 LD_LOG(kLogDlopen,
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001953 "... dlopen successful: realpath=\"%s\", soname=\"%s\", handle=%p",
1954 si->get_realpath(), si->get_soname(), handle);
1955 return handle;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001956 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001957
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001958 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001959}
1960
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001961int do_dladdr(const void* addr, Dl_info* info) {
1962 // Determine if this address can be found in any library currently mapped.
1963 soinfo* si = find_containing_library(addr);
1964 if (si == nullptr) {
1965 return 0;
1966 }
1967
1968 memset(info, 0, sizeof(Dl_info));
1969
1970 info->dli_fname = si->get_realpath();
1971 // Address at which the shared object is loaded.
1972 info->dli_fbase = reinterpret_cast<void*>(si->base);
1973
1974 // Determine if any symbol in the library contains the specified address.
1975 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
1976 if (sym != nullptr) {
1977 info->dli_sname = si->get_string(sym->st_name);
1978 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
1979 }
1980
1981 return 1;
1982}
1983
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001984static soinfo* soinfo_from_handle(void* handle) {
1985 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
1986 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
1987 if (it == g_soinfo_handles_map.end()) {
1988 return nullptr;
1989 } else {
1990 return it->second;
1991 }
1992 }
1993
1994 return static_cast<soinfo*>(handle);
1995}
1996
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08001997bool do_dlsym(void* handle,
1998 const char* sym_name,
1999 const char* sym_ver,
2000 const void* caller_addr,
2001 void** symbol) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002002#if !defined(__LP64__)
2003 if (handle == nullptr) {
2004 DL_ERR("dlsym failed: library handle is null");
2005 return false;
2006 }
2007#endif
2008
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002009 soinfo* found = nullptr;
2010 const ElfW(Sym)* sym = nullptr;
2011 soinfo* caller = find_containing_library(caller_addr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002012 android_namespace_t* ns = get_caller_namespace(caller);
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002013 soinfo* si = nullptr;
2014 if (handle != RTLD_DEFAULT && handle != RTLD_NEXT) {
2015 si = soinfo_from_handle(handle);
2016 }
2017
2018 LD_LOG(kLogDlsym,
2019 "dlsym(handle=%p(\"%s\"), sym_name=\"%s\", sym_ver=\"%s\", caller=\"%s\", caller_ns=%s@%p) ...",
2020 handle,
2021 si != nullptr ? si->get_realpath() : "n/a",
2022 sym_name,
2023 sym_ver,
2024 caller == nullptr ? "(null)" : caller->get_realpath(),
2025 ns == nullptr ? "(null)" : ns->get_name(),
2026 ns);
2027
2028 auto failure_guard = make_scope_guard([&]() {
2029 LD_LOG(kLogDlsym, "... dlsym failed: %s", linker_get_error_buffer());
2030 });
2031
2032 if (sym_name == nullptr) {
2033 DL_ERR("dlsym failed: symbol name is null");
2034 return false;
2035 }
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002036
2037 version_info vi_instance;
2038 version_info* vi = nullptr;
2039
2040 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002041 vi_instance.name = sym_ver;
2042 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002043 vi = &vi_instance;
2044 }
2045
2046 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2047 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2048 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002049 if (si == nullptr) {
2050 DL_ERR("dlsym failed: invalid handle: %p", handle);
2051 return false;
2052 }
2053 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002054 }
2055
2056 if (sym != nullptr) {
2057 uint32_t bind = ELF_ST_BIND(sym->st_info);
2058
2059 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2060 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002061 failure_guard.disable();
2062 LD_LOG(kLogDlsym,
2063 "... dlsym successful: sym_name=\"%s\", sym_ver=\"%s\", found in=\"%s\", address=%p",
2064 sym_name, sym_ver, found->get_soname(), *symbol);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002065 return true;
2066 }
2067
2068 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2069 return false;
2070 }
2071
2072 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2073 return false;
2074}
2075
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002076int do_dlclose(void* handle) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002077 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002078 soinfo* si = soinfo_from_handle(handle);
2079 if (si == nullptr) {
2080 DL_ERR("invalid handle: %p", handle);
2081 return -1;
2082 }
2083
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002084 soinfo_unload(si);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002085 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002086}
2087
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002088bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path) {
2089 if (g_anonymous_namespace_initialized) {
2090 DL_ERR("anonymous namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002091 return false;
2092 }
2093
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002094 ProtectedDataGuard guard;
2095
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002096 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002097 // When the caller is nullptr - create_namespace will take global group
2098 // from the anonymous namespace, which is fine because anonymous namespace
2099 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002100 android_namespace_t* anon_ns =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002101 create_namespace(nullptr,
2102 "(anonymous)",
2103 nullptr,
2104 library_search_path,
2105 // TODO (dimitry): change to isolated eventually.
2106 ANDROID_NAMESPACE_TYPE_REGULAR,
2107 nullptr,
2108 &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002109
2110 if (anon_ns == nullptr) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002111 return false;
2112 }
2113
2114 if (!link_namespaces(anon_ns, &g_default_namespace, shared_lib_sonames)) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002115 return false;
2116 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08002117
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002118 g_anonymous_namespace = anon_ns;
Dimitry Ivanov3e0821d2017-03-07 11:02:10 -08002119 g_anonymous_namespace_initialized = true;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002120
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002121 return true;
2122}
2123
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002124static void add_soinfos_to_namespace(const soinfo_list_t& soinfos, android_namespace_t* ns) {
2125 ns->add_soinfos(soinfos);
2126 for (auto si : soinfos) {
2127 si->add_secondary_namespace(ns);
2128 }
2129}
2130
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002131android_namespace_t* create_namespace(const void* caller_addr,
2132 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002133 const char* ld_library_path,
2134 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002135 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002136 const char* permitted_when_isolated_path,
2137 android_namespace_t* parent_namespace) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002138 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002139 // if parent_namespace is nullptr -> set it to the caller namespace
2140 soinfo* caller_soinfo = find_containing_library(caller_addr);
2141
2142 parent_namespace = caller_soinfo != nullptr ?
2143 caller_soinfo->get_primary_namespace() :
2144 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002145 }
2146
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002147 ProtectedDataGuard guard;
2148 std::vector<std::string> ld_library_paths;
2149 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002150 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002151
2152 parse_path(ld_library_path, ":", &ld_library_paths);
2153 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002154 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002155
2156 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2157 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002158 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002159 ns->set_ld_library_paths(std::move(ld_library_paths));
2160 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002161 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002162
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002163 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002164 // If shared - clone the parent namespace
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002165 add_soinfos_to_namespace(parent_namespace->soinfo_list(), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002166 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002167 // If not shared - copy only the shared group
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002168 add_soinfos_to_namespace(get_shared_group(parent_namespace), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002169 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002170
2171 return ns;
2172}
2173
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002174bool link_namespaces(android_namespace_t* namespace_from,
2175 android_namespace_t* namespace_to,
2176 const char* shared_lib_sonames) {
2177 if (namespace_to == nullptr) {
2178 namespace_to = &g_default_namespace;
2179 }
2180
2181 if (namespace_from == nullptr) {
2182 DL_ERR("error linking namespaces: namespace_from is null.");
2183 return false;
2184 }
2185
2186 if (shared_lib_sonames == nullptr || shared_lib_sonames[0] == '\0') {
2187 DL_ERR("error linking namespaces \"%s\"->\"%s\": the list of shared libraries is empty.",
2188 namespace_from->get_name(), namespace_to->get_name());
2189 return false;
2190 }
2191
2192 auto sonames = android::base::Split(shared_lib_sonames, ":");
2193 std::unordered_set<std::string> sonames_set(sonames.begin(), sonames.end());
2194
2195 ProtectedDataGuard guard;
2196 namespace_from->add_linked_namespace(namespace_to, sonames_set);
2197
2198 return true;
2199}
2200
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002201ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002202 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2203 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2204 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002205 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2206 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002207
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002208 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002209}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002210
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002211const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2212 if (source_symver < 2 ||
2213 source_symver >= version_infos.size() ||
2214 version_infos[source_symver].name == nullptr) {
2215 return nullptr;
2216 }
2217
2218 return &version_infos[source_symver];
2219}
2220
2221void VersionTracker::add_version_info(size_t source_index,
2222 ElfW(Word) elf_hash,
2223 const char* ver_name,
2224 const soinfo* target_si) {
2225 if (source_index >= version_infos.size()) {
2226 version_infos.resize(source_index+1);
2227 }
2228
2229 version_infos[source_index].elf_hash = elf_hash;
2230 version_infos[source_index].name = ver_name;
2231 version_infos[source_index].target_si = target_si;
2232}
2233
2234bool VersionTracker::init_verneed(const soinfo* si_from) {
2235 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2236
2237 if (verneed_ptr == 0) {
2238 return true;
2239 }
2240
2241 size_t verneed_cnt = si_from->get_verneed_cnt();
2242
2243 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2244 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2245 size_t vernaux_offset = offset + verneed->vn_aux;
2246 offset += verneed->vn_next;
2247
2248 if (verneed->vn_version != 1) {
2249 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2250 return false;
2251 }
2252
2253 const char* target_soname = si_from->get_string(verneed->vn_file);
2254 // find it in dependencies
2255 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002256 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002257 });
2258
2259 if (target_si == nullptr) {
2260 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002261 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002262 return false;
2263 }
2264
2265 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2266 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2267 vernaux_offset += vernaux->vna_next;
2268
2269 const ElfW(Word) elf_hash = vernaux->vna_hash;
2270 const char* ver_name = si_from->get_string(vernaux->vna_name);
2271 ElfW(Half) source_index = vernaux->vna_other;
2272
2273 add_version_info(source_index, elf_hash, ver_name, target_si);
2274 }
2275 }
2276
2277 return true;
2278}
2279
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002280template <typename F>
2281static bool for_each_verdef(const soinfo* si, F functor) {
2282 if (!si->has_min_version(2)) {
2283 return true;
2284 }
2285
2286 uintptr_t verdef_ptr = si->get_verdef_ptr();
2287 if (verdef_ptr == 0) {
2288 return true;
2289 }
2290
2291 size_t offset = 0;
2292
2293 size_t verdef_cnt = si->get_verdef_cnt();
2294 for (size_t i = 0; i<verdef_cnt; ++i) {
2295 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
2296 size_t verdaux_offset = offset + verdef->vd_aux;
2297 offset += verdef->vd_next;
2298
2299 if (verdef->vd_version != 1) {
2300 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
2301 i, verdef->vd_version, si->get_realpath());
2302 return false;
2303 }
2304
2305 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
2306 // "this is the version of the file itself. It must not be used for
2307 // matching a symbol. It can be used to match references."
2308 //
2309 // http://www.akkadia.org/drepper/symbol-versioning
2310 continue;
2311 }
2312
2313 if (verdef->vd_cnt == 0) {
2314 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
2315 return false;
2316 }
2317
2318 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
2319
2320 if (functor(i, verdef, verdaux) == true) {
2321 break;
2322 }
2323 }
2324
2325 return true;
2326}
2327
2328bool find_verdef_version_index(const soinfo* si, const version_info* vi, ElfW(Versym)* versym) {
2329 if (vi == nullptr) {
2330 *versym = kVersymNotNeeded;
2331 return true;
2332 }
2333
2334 *versym = kVersymGlobal;
2335
2336 return for_each_verdef(si,
2337 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2338 if (verdef->vd_hash == vi->elf_hash &&
2339 strcmp(vi->name, si->get_string(verdaux->vda_name)) == 0) {
2340 *versym = verdef->vd_ndx;
2341 return true;
2342 }
2343
2344 return false;
2345 }
2346 );
2347}
2348
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002349bool VersionTracker::init_verdef(const soinfo* si_from) {
2350 return for_each_verdef(si_from,
2351 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2352 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2353 si_from->get_string(verdaux->vda_name), si_from);
2354 return false;
2355 }
2356 );
2357}
2358
2359bool VersionTracker::init(const soinfo* si_from) {
2360 if (!si_from->has_min_version(2)) {
2361 return true;
2362 }
2363
2364 return init_verneed(si_from) && init_verdef(si_from);
2365}
2366
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002367// TODO (dimitry): Methods below need to be moved out of soinfo
2368// and in more isolated file in order minimize dependencies on
2369// unnecessary object in the linker binary. Consider making them
2370// independent from soinfo (?).
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002371bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2372 const char* sym_name, const version_info** vi) {
2373 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2374 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2375
2376 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2377 *vi = version_tracker.get_version_info(sym_ver);
2378
2379 if (*vi == nullptr) {
2380 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002381 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002382 return false;
2383 }
2384 } else {
2385 // there is no version info
2386 *vi = nullptr;
2387 }
2388
2389 return true;
2390}
2391
Dimitry Ivanov576a3752016-08-09 06:58:55 -07002392#if !defined(__mips__)
2393#if defined(USE_RELA)
2394static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2395 return rela->r_addend;
2396}
2397#else
2398static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
2399 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2400 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
2401 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2402 }
2403 return 0;
2404}
2405#endif
2406
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002407template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002408bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2409 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002410 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2411 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002412 if (rel == nullptr) {
2413 return false;
2414 }
2415
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002416 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2417 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2418
2419 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002420 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002421 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002422 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002423
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002424 DEBUG("Processing \"%s\" relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002425 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002426 continue;
2427 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002428
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002429 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002430 soinfo* lsi = nullptr;
2431
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002432 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002433 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002434 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002435
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002436 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2437 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002438 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002439
2440 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2441 return false;
2442 }
2443
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002444 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002445 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002446 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002447 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002448 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002449 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002450 }
2451
2452 /* IHI0044C AAELF 4.5.1.1:
2453
2454 Libraries are not searched to resolve weak references.
2455 It is not an error for a weak reference to remain unsatisfied.
2456
2457 During linking, the value of an undefined weak reference is:
2458 - Zero if the relocation type is absolute
2459 - The address of the place if the relocation is pc-relative
2460 - The address of nominal base address if the relocation
2461 type is base-relative.
2462 */
2463
2464 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002465 case R_GENERIC_JUMP_SLOT:
2466 case R_GENERIC_GLOB_DAT:
2467 case R_GENERIC_RELATIVE:
2468 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002469#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002470 case R_AARCH64_ABS64:
2471 case R_AARCH64_ABS32:
2472 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002473#elif defined(__x86_64__)
2474 case R_X86_64_32:
2475 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002476#elif defined(__arm__)
2477 case R_ARM_ABS32:
2478#elif defined(__i386__)
2479 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002480#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002481 /*
2482 * The sym_addr was initialized to be zero above, or the relocation
2483 * code below does not care about value of sym_addr.
2484 * No need to do anything.
2485 */
2486 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002487#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002488 case R_X86_64_PC32:
2489 sym_addr = reloc;
2490 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002491#elif defined(__i386__)
2492 case R_386_PC32:
2493 sym_addr = reloc;
2494 break;
2495#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002496 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002497 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002498 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002499 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002500 } else { // We got a definition.
2501#if !defined(__LP64__)
2502 // When relocating dso with text_relocation .text segment is
2503 // not executable. We need to restore elf flags before resolving
2504 // STT_GNU_IFUNC symbol.
2505 bool protect_segments = has_text_relocations &&
2506 lsi == this &&
2507 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2508 if (protect_segments) {
2509 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2510 DL_ERR("can't protect segments for \"%s\": %s",
2511 get_realpath(), strerror(errno));
2512 return false;
2513 }
2514 }
2515#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002516 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002517#if !defined(__LP64__)
2518 if (protect_segments) {
2519 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2520 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2521 get_realpath(), strerror(errno));
2522 return false;
2523 }
2524 }
2525#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002526 }
2527 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002528 }
2529
2530 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002531 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002532 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002533 MARK(rel->r_offset);
2534 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2535 reinterpret_cast<void*>(reloc),
2536 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2537
2538 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002539 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002540 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002541 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002542 MARK(rel->r_offset);
2543 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2544 reinterpret_cast<void*>(reloc),
2545 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2546 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002547 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002548 case R_GENERIC_RELATIVE:
2549 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002550 MARK(rel->r_offset);
2551 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2552 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002553 reinterpret_cast<void*>(load_bias + addend));
2554 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002555 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002556 case R_GENERIC_IRELATIVE:
2557 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002558 MARK(rel->r_offset);
2559 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2560 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002561 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002562 {
2563#if !defined(__LP64__)
2564 // When relocating dso with text_relocation .text segment is
2565 // not executable. We need to restore elf flags for this
2566 // particular call.
2567 if (has_text_relocations) {
2568 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2569 DL_ERR("can't protect segments for \"%s\": %s",
2570 get_realpath(), strerror(errno));
2571 return false;
2572 }
2573 }
2574#endif
2575 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2576#if !defined(__LP64__)
2577 // Unprotect it afterwards...
2578 if (has_text_relocations) {
2579 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2580 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2581 get_realpath(), strerror(errno));
2582 return false;
2583 }
2584 }
2585#endif
2586 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2587 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002588 break;
2589
2590#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002591 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002592 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002593 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002594 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002595 reloc, sym_addr + addend, sym_name);
2596 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002597 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002598 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002599 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002600 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002601 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002602 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002603 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002604 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2605 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002606 if ((min_value <= (sym_addr + addend)) &&
2607 ((sym_addr + addend) <= max_value)) {
2608 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002609 } else {
2610 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002611 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002612 return false;
2613 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002614 }
2615 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002616 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002617 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002618 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002619 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002620 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002621 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002622 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2623 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002624 if ((min_value <= (sym_addr + addend)) &&
2625 ((sym_addr + addend) <= max_value)) {
2626 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002627 } else {
2628 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002629 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002630 return false;
2631 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002632 }
2633 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002634 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002635 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002636 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002637 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002638 reloc, sym_addr + addend, rel->r_offset, sym_name);
2639 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002640 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002641 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002642 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002643 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002644 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002645 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002646 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002647 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2648 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002649 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2650 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2651 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002652 } else {
2653 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002654 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002655 return false;
2656 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002657 }
2658 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002659 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002660 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002661 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002662 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002663 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002664 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002665 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2666 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002667 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2668 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2669 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002670 } else {
2671 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002672 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002673 return false;
2674 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002675 }
2676 break;
2677
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002678 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002679 /*
2680 * ET_EXEC is not supported so this should not happen.
2681 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002682 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002683 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002684 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002685 * R_AARCH64_COPY may only appear in executable objects where e_type is
2686 * set to ET_EXEC.
2687 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002688 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002689 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002690 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002691 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002692 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002693 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002694 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002695 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %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;
2698#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002699 case R_X86_64_32:
2700 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002701 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002702 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2703 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002704 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002705 break;
2706 case R_X86_64_64:
2707 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002708 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002709 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2710 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002711 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002712 break;
2713 case R_X86_64_PC32:
2714 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002715 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002716 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2717 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2718 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002719 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002720 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002721#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002722 case R_ARM_ABS32:
2723 count_relocation(kRelocAbsolute);
2724 MARK(rel->r_offset);
2725 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2726 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2727 break;
2728 case R_ARM_REL32:
2729 count_relocation(kRelocRelative);
2730 MARK(rel->r_offset);
2731 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2732 reloc, sym_addr, rel->r_offset, sym_name);
2733 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2734 break;
2735 case R_ARM_COPY:
2736 /*
2737 * ET_EXEC is not supported so this should not happen.
2738 *
2739 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2740 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002741 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002742 * R_ARM_COPY may only appear in executable objects where e_type is
2743 * set to ET_EXEC.
2744 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002745 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002746 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002747#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002748 case R_386_32:
2749 count_relocation(kRelocRelative);
2750 MARK(rel->r_offset);
2751 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2752 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2753 break;
2754 case R_386_PC32:
2755 count_relocation(kRelocRelative);
2756 MARK(rel->r_offset);
2757 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2758 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2759 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2760 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002761#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002762 default:
2763 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002764 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002765 }
2766 }
2767 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002768}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002769#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002770
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002771// An empty list of soinfos
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002772static soinfo_list_t g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002773
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002774bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002775 /* Extract dynamic section */
2776 ElfW(Word) dynamic_flags = 0;
2777 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002778
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002779 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002780 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002781 if (!relocating_linker) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002782 INFO("[ Linking \"%s\" ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002783 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002784 }
2785
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002786 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002787 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002788 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002789 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002790 return false;
2791 } else {
2792 if (!relocating_linker) {
2793 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002794 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002795 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002796
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002797#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002798 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2799 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002800#endif
2801
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002802 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002803 // Note that: "Except for the DT_NULL element at the end of the array,
2804 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2805 //
2806 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002807 uint32_t needed_count = 0;
2808 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2809 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2810 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2811 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002812 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002813 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002814 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002815
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002816 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002817 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2818 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2819 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2820 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002821 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002822
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002823 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002824 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002825 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002826 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2827 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002828
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002829 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002830 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002831 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002832 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
2833 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002834
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002835 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002836 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002837 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002838 return false;
2839 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002840 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002841
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002842 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002843 break;
2844
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002845 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002846 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002847 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002848
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002849 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002850 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002851 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002852
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002853 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002854 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002855 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002856
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002857 case DT_SYMENT:
2858 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002859 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
2860 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002861 return false;
2862 }
2863 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002864
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002865 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002866#if defined(USE_RELA)
2867 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002868 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002869 return false;
2870 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002871#else
2872 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002873 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002874 return false;
2875 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002876#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002877 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002878
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002879 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002880#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002881 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002882#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002883 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002884#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002885 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002886
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002887 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002888#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002889 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002890#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002891 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002892#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002893 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002894
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002895 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002896#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002897 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002898 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002899#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002900 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2901 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002902
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002903 case DT_DEBUG:
2904 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2905 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002906// FIXME: not working currently for N64
2907// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002908// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002909// read-only, but the DYNAMIC header claims it is writable.
2910#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002911 if ((dynamic_flags & PF_W) != 0) {
2912 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2913 }
Chris Dearman99186652014-02-06 20:36:51 -08002914#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002915 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002916#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002917 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002918 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002919 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002920
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002921 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002922 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002923 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002924
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002925 case DT_ANDROID_RELA:
2926 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2927 break;
2928
2929 case DT_ANDROID_RELASZ:
2930 android_relocs_size_ = d->d_un.d_val;
2931 break;
2932
2933 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002934 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002935 return false;
2936
2937 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002938 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002939 return false;
2940
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002941 case DT_RELAENT:
2942 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002943 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002944 return false;
2945 }
2946 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002947
2948 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002949 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002950 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002951
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002952 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002953 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002954 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002955
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002956 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002957 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002958 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002959
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002960#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002961 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002962 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002963 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002964
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002965 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002966 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002967 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002968
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002969 case DT_RELENT:
2970 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002971 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002972 return false;
2973 }
2974 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002975
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002976 case DT_ANDROID_REL:
2977 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2978 break;
2979
2980 case DT_ANDROID_RELSZ:
2981 android_relocs_size_ = d->d_un.d_val;
2982 break;
2983
2984 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002985 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002986 return false;
2987
2988 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002989 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002990 return false;
2991
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002992 // "Indicates that all RELATIVE relocations have been concatenated together,
2993 // and specifies the RELATIVE relocation count."
2994 //
2995 // TODO: Spec also mentions that this can be used to optimize relocation process;
2996 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002997 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002998 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002999
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003000 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003001 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003002 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003003
3004 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003005 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003006 return false;
3007
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003008#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003009 case DT_INIT:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003010 init_func_ = reinterpret_cast<linker_ctor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003011 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003012 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003013
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003014 case DT_FINI:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003015 fini_func_ = reinterpret_cast<linker_dtor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003016 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003017 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003018
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003019 case DT_INIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003020 init_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003021 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003022 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003023
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003024 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003025 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
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_FINI_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003029 fini_array_ = reinterpret_cast<linker_dtor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003030 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003031 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003032
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003033 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003034 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
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_PREINIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003038 preinit_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003039 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003040 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003041
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003042 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003043 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
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_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003047#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003048 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003049 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003050#else
3051 has_text_relocations = true;
3052 break;
3053#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003054
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003055 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003056 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003057 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003058
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003059 case DT_NEEDED:
3060 ++needed_count;
3061 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003062
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003063 case DT_FLAGS:
3064 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003065#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003066 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003067 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003068#else
3069 has_text_relocations = true;
3070#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003071 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003072 if (d->d_un.d_val & DF_SYMBOLIC) {
3073 has_DT_SYMBOLIC = true;
3074 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003075 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003076
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003077 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003078 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003079
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003080 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003081 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 -07003082 }
3083 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003084#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003085 case DT_MIPS_RLD_MAP:
3086 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3087 {
3088 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3089 *dp = &_r_debug;
3090 }
3091 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003092 case DT_MIPS_RLD_MAP_REL:
3093 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003094 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003095 r_debug** dp = reinterpret_cast<r_debug**>(
3096 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003097 *dp = &_r_debug;
3098 }
3099 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003100
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003101 case DT_MIPS_RLD_VERSION:
3102 case DT_MIPS_FLAGS:
3103 case DT_MIPS_BASE_ADDRESS:
3104 case DT_MIPS_UNREFEXTNO:
3105 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003106
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003107 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003108 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003109 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003110
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003111 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003112 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003113 break;
3114
3115 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003116 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003117 break;
3118#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003119 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3120 case DT_BIND_NOW:
3121 break;
3122
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003123 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003124 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3125 break;
3126
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003127 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003128 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3129 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003130 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003131 verdef_cnt_ = d->d_un.d_val;
3132 break;
3133
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003134 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003135 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3136 break;
3137
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003138 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003139 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003140 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003141
Evgenii Stepanov68650822015-06-10 13:38:39 -07003142 case DT_RUNPATH:
3143 // this is parsed after we have strtab initialized (see below).
3144 break;
3145
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003146 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003147 if (!relocating_linker) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003148 DL_WARN("\"%s\" unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003149 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3150 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003151 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003152 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003153 }
3154
Duane Sandbc425c72015-06-01 16:29:14 -07003155#if defined(__mips__) && !defined(__LP64__)
3156 if (!mips_check_and_adjust_fp_modes()) {
3157 return false;
3158 }
3159#endif
3160
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003161 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003162 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003163
3164 // Sanity checks.
3165 if (relocating_linker && needed_count != 0) {
3166 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3167 return false;
3168 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003169 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003170 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003171 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003172 return false;
3173 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003174 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003175 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003176 return false;
3177 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003178 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003179 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003180 return false;
3181 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003182
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003183 // second pass - parse entries relying on strtab
3184 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003185 switch (d->d_tag) {
3186 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003187 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003188 break;
3189 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003190 set_dt_runpath(get_string(d->d_un.d_val));
3191 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003192 }
3193 }
3194
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003195 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003196 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003197 // because they can't find dt_needed library by soname.
3198 // This workaround should keep them working. (applies only
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003199 // for apps targeting sdk version < M). Make an exception for
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003200 // the main executable and linker; they do not need to have dt_soname
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003201 if (soname_ == nullptr &&
3202 this != solist_get_somain() &&
3203 (flags_ & FLAG_LINKER) == 0 &&
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003204 get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003205 soname_ = basename(realpath_.c_str());
3206 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3207 get_realpath(), soname_);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003208 // 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 -07003209 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003210 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003211}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003212
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003213bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3214 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003215
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003216 local_group_root_ = local_group.front();
3217 if (local_group_root_ == nullptr) {
3218 local_group_root_ = this;
3219 }
3220
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003221 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3222 target_sdk_version_ = get_application_target_sdk_version();
3223 }
3224
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003225 VersionTracker version_tracker;
3226
3227 if (!version_tracker.init(this)) {
3228 return false;
3229 }
3230
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003231#if !defined(__LP64__)
3232 if (has_text_relocations) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003233 // Fail if app is targeting M or above.
3234 if (get_application_target_sdk_version() >= __ANDROID_API_M__) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003235 DL_ERR_AND_LOG("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003236 return false;
3237 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003238 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003239 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003240 DL_WARN("\"%s\" has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003241 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003242 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003243 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3244 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003245 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003246 return false;
3247 }
3248 }
3249#endif
3250
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003251 if (android_relocs_ != nullptr) {
3252 // check signature
3253 if (android_relocs_size_ > 3 &&
3254 android_relocs_[0] == 'A' &&
3255 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003256 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003257 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003258 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003259
3260 bool relocated = false;
3261 const uint8_t* packed_relocs = android_relocs_ + 4;
3262 const size_t packed_relocs_size = android_relocs_size_ - 4;
3263
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003264 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003265 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003266 packed_reloc_iterator<sleb128_decoder>(
3267 sleb128_decoder(packed_relocs, packed_relocs_size)),
3268 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003269
3270 if (!relocated) {
3271 return false;
3272 }
3273 } else {
3274 DL_ERR("bad android relocation header.");
3275 return false;
3276 }
3277 }
3278
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003279#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003280 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003281 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003282 if (!relocate(version_tracker,
3283 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003284 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003285 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003286 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003287 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003288 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003289 if (!relocate(version_tracker,
3290 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003291 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003292 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003293 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003294#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003295 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003296 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003297 if (!relocate(version_tracker,
3298 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003299 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003300 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003301 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003302 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003303 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003304 if (!relocate(version_tracker,
3305 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003306 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003307 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003308 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003309#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003310
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003311#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003312 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003313 return false;
3314 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003315#endif
3316
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003317 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003318
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003319#if !defined(__LP64__)
3320 if (has_text_relocations) {
3321 // All relocations are done, we can protect our segments back to read-only.
3322 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3323 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003324 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003325 return false;
3326 }
3327 }
3328#endif
3329
Mingwei Shibe910522015-11-12 07:02:14 +00003330 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3331 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3332 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003333 return false;
3334 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003335
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003336 /* Handle serializing/sharing the RELRO segment */
3337 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3338 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3339 extinfo->relro_fd) < 0) {
3340 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003341 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003342 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003343 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003344 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3345 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3346 extinfo->relro_fd) < 0) {
3347 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003348 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003349 return false;
3350 }
3351 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003352
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003353 notify_gdb_of_load(this);
3354 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003355}
3356
Mingwei Shibe910522015-11-12 07:02:14 +00003357bool soinfo::protect_relro() {
3358 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3359 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3360 get_realpath(), strerror(errno));
3361 return false;
3362 }
3363 return true;
3364}
3365
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003366void init_default_namespace() {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003367 g_default_namespace.set_name("(default)");
3368 g_default_namespace.set_isolated(false);
3369
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003370 soinfo* somain = solist_get_somain();
3371
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003372 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3373 somain->load_bias);
3374 const char* bname = basename(interp);
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003375
3376 bool is_asan = bname != nullptr &&
3377 (strcmp(bname, "linker_asan") == 0 ||
3378 strcmp(bname, "linker_asan64") == 0);
3379 auto default_ld_paths = is_asan ? kAsanDefaultLdPaths : kDefaultLdPaths;
3380 g_is_asan = is_asan;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003381
neo.chae2589f9d2016-10-04 11:00:27 +09003382 char real_path[PATH_MAX];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003383 std::vector<std::string> ld_default_paths;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003384 for (size_t i = 0; default_ld_paths[i] != nullptr; ++i) {
3385 if (realpath(default_ld_paths[i], real_path) != nullptr) {
neo.chae2589f9d2016-10-04 11:00:27 +09003386 ld_default_paths.push_back(real_path);
3387 } else {
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003388 ld_default_paths.push_back(default_ld_paths[i]);
neo.chae2589f9d2016-10-04 11:00:27 +09003389 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003390 }
3391
3392 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003393};