blob: e5a356f794545d869529c35be3d64aa7380db5f2 [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,
Dimitry Ivanovc9dced22017-03-27 15:42:17 -07002105 ANDROID_NAMESPACE_TYPE_ISOLATED,
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002106 nullptr,
2107 &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002108
2109 if (anon_ns == nullptr) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002110 return false;
2111 }
2112
2113 if (!link_namespaces(anon_ns, &g_default_namespace, shared_lib_sonames)) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002114 return false;
2115 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08002116
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002117 g_anonymous_namespace = anon_ns;
Dimitry Ivanov3e0821d2017-03-07 11:02:10 -08002118 g_anonymous_namespace_initialized = true;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002119
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002120 return true;
2121}
2122
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002123static void add_soinfos_to_namespace(const soinfo_list_t& soinfos, android_namespace_t* ns) {
2124 ns->add_soinfos(soinfos);
2125 for (auto si : soinfos) {
2126 si->add_secondary_namespace(ns);
2127 }
2128}
2129
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002130android_namespace_t* create_namespace(const void* caller_addr,
2131 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002132 const char* ld_library_path,
2133 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002134 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002135 const char* permitted_when_isolated_path,
2136 android_namespace_t* parent_namespace) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002137 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002138 // if parent_namespace is nullptr -> set it to the caller namespace
2139 soinfo* caller_soinfo = find_containing_library(caller_addr);
2140
2141 parent_namespace = caller_soinfo != nullptr ?
2142 caller_soinfo->get_primary_namespace() :
2143 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002144 }
2145
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002146 ProtectedDataGuard guard;
2147 std::vector<std::string> ld_library_paths;
2148 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002149 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002150
2151 parse_path(ld_library_path, ":", &ld_library_paths);
2152 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002153 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002154
2155 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2156 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002157 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002158 ns->set_ld_library_paths(std::move(ld_library_paths));
2159 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002160 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002161
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002162 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002163 // If shared - clone the parent namespace
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002164 add_soinfos_to_namespace(parent_namespace->soinfo_list(), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002165 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002166 // If not shared - copy only the shared group
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002167 add_soinfos_to_namespace(get_shared_group(parent_namespace), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002168 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002169
2170 return ns;
2171}
2172
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002173bool link_namespaces(android_namespace_t* namespace_from,
2174 android_namespace_t* namespace_to,
2175 const char* shared_lib_sonames) {
2176 if (namespace_to == nullptr) {
2177 namespace_to = &g_default_namespace;
2178 }
2179
2180 if (namespace_from == nullptr) {
2181 DL_ERR("error linking namespaces: namespace_from is null.");
2182 return false;
2183 }
2184
2185 if (shared_lib_sonames == nullptr || shared_lib_sonames[0] == '\0') {
2186 DL_ERR("error linking namespaces \"%s\"->\"%s\": the list of shared libraries is empty.",
2187 namespace_from->get_name(), namespace_to->get_name());
2188 return false;
2189 }
2190
2191 auto sonames = android::base::Split(shared_lib_sonames, ":");
2192 std::unordered_set<std::string> sonames_set(sonames.begin(), sonames.end());
2193
2194 ProtectedDataGuard guard;
2195 namespace_from->add_linked_namespace(namespace_to, sonames_set);
2196
2197 return true;
2198}
2199
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002200ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002201 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2202 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2203 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002204 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2205 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002206
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002207 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002208}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002209
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002210const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2211 if (source_symver < 2 ||
2212 source_symver >= version_infos.size() ||
2213 version_infos[source_symver].name == nullptr) {
2214 return nullptr;
2215 }
2216
2217 return &version_infos[source_symver];
2218}
2219
2220void VersionTracker::add_version_info(size_t source_index,
2221 ElfW(Word) elf_hash,
2222 const char* ver_name,
2223 const soinfo* target_si) {
2224 if (source_index >= version_infos.size()) {
2225 version_infos.resize(source_index+1);
2226 }
2227
2228 version_infos[source_index].elf_hash = elf_hash;
2229 version_infos[source_index].name = ver_name;
2230 version_infos[source_index].target_si = target_si;
2231}
2232
2233bool VersionTracker::init_verneed(const soinfo* si_from) {
2234 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2235
2236 if (verneed_ptr == 0) {
2237 return true;
2238 }
2239
2240 size_t verneed_cnt = si_from->get_verneed_cnt();
2241
2242 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2243 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2244 size_t vernaux_offset = offset + verneed->vn_aux;
2245 offset += verneed->vn_next;
2246
2247 if (verneed->vn_version != 1) {
2248 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2249 return false;
2250 }
2251
2252 const char* target_soname = si_from->get_string(verneed->vn_file);
2253 // find it in dependencies
2254 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002255 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002256 });
2257
2258 if (target_si == nullptr) {
2259 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002260 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002261 return false;
2262 }
2263
2264 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2265 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2266 vernaux_offset += vernaux->vna_next;
2267
2268 const ElfW(Word) elf_hash = vernaux->vna_hash;
2269 const char* ver_name = si_from->get_string(vernaux->vna_name);
2270 ElfW(Half) source_index = vernaux->vna_other;
2271
2272 add_version_info(source_index, elf_hash, ver_name, target_si);
2273 }
2274 }
2275
2276 return true;
2277}
2278
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002279template <typename F>
2280static bool for_each_verdef(const soinfo* si, F functor) {
2281 if (!si->has_min_version(2)) {
2282 return true;
2283 }
2284
2285 uintptr_t verdef_ptr = si->get_verdef_ptr();
2286 if (verdef_ptr == 0) {
2287 return true;
2288 }
2289
2290 size_t offset = 0;
2291
2292 size_t verdef_cnt = si->get_verdef_cnt();
2293 for (size_t i = 0; i<verdef_cnt; ++i) {
2294 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
2295 size_t verdaux_offset = offset + verdef->vd_aux;
2296 offset += verdef->vd_next;
2297
2298 if (verdef->vd_version != 1) {
2299 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
2300 i, verdef->vd_version, si->get_realpath());
2301 return false;
2302 }
2303
2304 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
2305 // "this is the version of the file itself. It must not be used for
2306 // matching a symbol. It can be used to match references."
2307 //
2308 // http://www.akkadia.org/drepper/symbol-versioning
2309 continue;
2310 }
2311
2312 if (verdef->vd_cnt == 0) {
2313 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
2314 return false;
2315 }
2316
2317 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
2318
2319 if (functor(i, verdef, verdaux) == true) {
2320 break;
2321 }
2322 }
2323
2324 return true;
2325}
2326
2327bool find_verdef_version_index(const soinfo* si, const version_info* vi, ElfW(Versym)* versym) {
2328 if (vi == nullptr) {
2329 *versym = kVersymNotNeeded;
2330 return true;
2331 }
2332
2333 *versym = kVersymGlobal;
2334
2335 return for_each_verdef(si,
2336 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2337 if (verdef->vd_hash == vi->elf_hash &&
2338 strcmp(vi->name, si->get_string(verdaux->vda_name)) == 0) {
2339 *versym = verdef->vd_ndx;
2340 return true;
2341 }
2342
2343 return false;
2344 }
2345 );
2346}
2347
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002348bool VersionTracker::init_verdef(const soinfo* si_from) {
2349 return for_each_verdef(si_from,
2350 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2351 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2352 si_from->get_string(verdaux->vda_name), si_from);
2353 return false;
2354 }
2355 );
2356}
2357
2358bool VersionTracker::init(const soinfo* si_from) {
2359 if (!si_from->has_min_version(2)) {
2360 return true;
2361 }
2362
2363 return init_verneed(si_from) && init_verdef(si_from);
2364}
2365
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002366// TODO (dimitry): Methods below need to be moved out of soinfo
2367// and in more isolated file in order minimize dependencies on
2368// unnecessary object in the linker binary. Consider making them
2369// independent from soinfo (?).
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002370bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2371 const char* sym_name, const version_info** vi) {
2372 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2373 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2374
2375 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2376 *vi = version_tracker.get_version_info(sym_ver);
2377
2378 if (*vi == nullptr) {
2379 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002380 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002381 return false;
2382 }
2383 } else {
2384 // there is no version info
2385 *vi = nullptr;
2386 }
2387
2388 return true;
2389}
2390
Dimitry Ivanov576a3752016-08-09 06:58:55 -07002391#if !defined(__mips__)
2392#if defined(USE_RELA)
2393static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2394 return rela->r_addend;
2395}
2396#else
2397static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
2398 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2399 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
2400 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2401 }
2402 return 0;
2403}
2404#endif
2405
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002406template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002407bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2408 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002409 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2410 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002411 if (rel == nullptr) {
2412 return false;
2413 }
2414
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002415 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2416 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2417
2418 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002419 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002420 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002421 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002422
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002423 DEBUG("Processing \"%s\" relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002424 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002425 continue;
2426 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002427
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002428 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002429 soinfo* lsi = nullptr;
2430
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002431 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002432 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002433 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002434
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002435 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2436 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002437 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002438
2439 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2440 return false;
2441 }
2442
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002443 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002444 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002445 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002446 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002447 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002448 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002449 }
2450
2451 /* IHI0044C AAELF 4.5.1.1:
2452
2453 Libraries are not searched to resolve weak references.
2454 It is not an error for a weak reference to remain unsatisfied.
2455
2456 During linking, the value of an undefined weak reference is:
2457 - Zero if the relocation type is absolute
2458 - The address of the place if the relocation is pc-relative
2459 - The address of nominal base address if the relocation
2460 type is base-relative.
2461 */
2462
2463 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002464 case R_GENERIC_JUMP_SLOT:
2465 case R_GENERIC_GLOB_DAT:
2466 case R_GENERIC_RELATIVE:
2467 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002468#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002469 case R_AARCH64_ABS64:
2470 case R_AARCH64_ABS32:
2471 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002472#elif defined(__x86_64__)
2473 case R_X86_64_32:
2474 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002475#elif defined(__arm__)
2476 case R_ARM_ABS32:
2477#elif defined(__i386__)
2478 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002479#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002480 /*
2481 * The sym_addr was initialized to be zero above, or the relocation
2482 * code below does not care about value of sym_addr.
2483 * No need to do anything.
2484 */
2485 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002486#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002487 case R_X86_64_PC32:
2488 sym_addr = reloc;
2489 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002490#elif defined(__i386__)
2491 case R_386_PC32:
2492 sym_addr = reloc;
2493 break;
2494#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002495 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002496 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002497 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002498 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002499 } else { // We got a definition.
2500#if !defined(__LP64__)
2501 // When relocating dso with text_relocation .text segment is
2502 // not executable. We need to restore elf flags before resolving
2503 // STT_GNU_IFUNC symbol.
2504 bool protect_segments = has_text_relocations &&
2505 lsi == this &&
2506 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2507 if (protect_segments) {
2508 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2509 DL_ERR("can't protect segments for \"%s\": %s",
2510 get_realpath(), strerror(errno));
2511 return false;
2512 }
2513 }
2514#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002515 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002516#if !defined(__LP64__)
2517 if (protect_segments) {
2518 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2519 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2520 get_realpath(), strerror(errno));
2521 return false;
2522 }
2523 }
2524#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002525 }
2526 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002527 }
2528
2529 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002530 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002531 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002532 MARK(rel->r_offset);
2533 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2534 reinterpret_cast<void*>(reloc),
2535 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2536
2537 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002538 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002539 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002540 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002541 MARK(rel->r_offset);
2542 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2543 reinterpret_cast<void*>(reloc),
2544 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2545 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002546 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002547 case R_GENERIC_RELATIVE:
2548 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002549 MARK(rel->r_offset);
2550 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2551 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002552 reinterpret_cast<void*>(load_bias + addend));
2553 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002554 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002555 case R_GENERIC_IRELATIVE:
2556 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002557 MARK(rel->r_offset);
2558 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2559 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002560 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002561 {
2562#if !defined(__LP64__)
2563 // When relocating dso with text_relocation .text segment is
2564 // not executable. We need to restore elf flags for this
2565 // particular call.
2566 if (has_text_relocations) {
2567 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2568 DL_ERR("can't protect segments for \"%s\": %s",
2569 get_realpath(), strerror(errno));
2570 return false;
2571 }
2572 }
2573#endif
2574 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2575#if !defined(__LP64__)
2576 // Unprotect it afterwards...
2577 if (has_text_relocations) {
2578 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2579 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2580 get_realpath(), strerror(errno));
2581 return false;
2582 }
2583 }
2584#endif
2585 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2586 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002587 break;
2588
2589#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002590 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002591 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002592 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002593 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002594 reloc, sym_addr + addend, sym_name);
2595 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002596 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002597 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002598 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002599 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002600 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002601 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002602 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002603 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2604 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002605 if ((min_value <= (sym_addr + addend)) &&
2606 ((sym_addr + addend) <= max_value)) {
2607 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002608 } else {
2609 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002610 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002611 return false;
2612 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002613 }
2614 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002615 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002616 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002617 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002618 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002619 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002620 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002621 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2622 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002623 if ((min_value <= (sym_addr + addend)) &&
2624 ((sym_addr + addend) <= max_value)) {
2625 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002626 } else {
2627 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002628 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002629 return false;
2630 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002631 }
2632 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002633 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002634 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002635 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002636 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002637 reloc, sym_addr + addend, rel->r_offset, sym_name);
2638 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002639 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002640 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002641 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002642 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002643 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002644 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002645 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002646 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2647 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002648 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2649 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2650 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002651 } else {
2652 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002653 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002654 return false;
2655 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002656 }
2657 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002658 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002659 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002660 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002661 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002662 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002663 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002664 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2665 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002666 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2667 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2668 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002669 } else {
2670 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002671 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002672 return false;
2673 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002674 }
2675 break;
2676
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002677 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002678 /*
2679 * ET_EXEC is not supported so this should not happen.
2680 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002681 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002682 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002683 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002684 * R_AARCH64_COPY may only appear in executable objects where e_type is
2685 * set to ET_EXEC.
2686 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002687 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002688 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002689 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002690 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002691 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002692 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002693 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002694 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002695 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002696 break;
2697#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002698 case R_X86_64_32:
2699 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002700 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002701 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2702 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002703 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002704 break;
2705 case R_X86_64_64:
2706 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002707 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002708 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2709 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002710 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002711 break;
2712 case R_X86_64_PC32:
2713 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002714 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002715 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2716 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2717 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002718 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002719 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002720#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002721 case R_ARM_ABS32:
2722 count_relocation(kRelocAbsolute);
2723 MARK(rel->r_offset);
2724 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2725 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2726 break;
2727 case R_ARM_REL32:
2728 count_relocation(kRelocRelative);
2729 MARK(rel->r_offset);
2730 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2731 reloc, sym_addr, rel->r_offset, sym_name);
2732 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2733 break;
2734 case R_ARM_COPY:
2735 /*
2736 * ET_EXEC is not supported so this should not happen.
2737 *
2738 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2739 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002740 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002741 * R_ARM_COPY may only appear in executable objects where e_type is
2742 * set to ET_EXEC.
2743 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002744 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002745 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002746#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002747 case R_386_32:
2748 count_relocation(kRelocRelative);
2749 MARK(rel->r_offset);
2750 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2751 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2752 break;
2753 case R_386_PC32:
2754 count_relocation(kRelocRelative);
2755 MARK(rel->r_offset);
2756 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2757 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2758 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2759 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002760#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002761 default:
2762 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002763 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002764 }
2765 }
2766 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002767}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002768#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002769
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002770// An empty list of soinfos
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002771static soinfo_list_t g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002772
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002773bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002774 /* Extract dynamic section */
2775 ElfW(Word) dynamic_flags = 0;
2776 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002777
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002778 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002779 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002780 if (!relocating_linker) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002781 INFO("[ Linking \"%s\" ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002782 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002783 }
2784
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002785 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002786 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002787 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002788 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002789 return false;
2790 } else {
2791 if (!relocating_linker) {
2792 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002793 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002794 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002795
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002796#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002797 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2798 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002799#endif
2800
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002801 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002802 // Note that: "Except for the DT_NULL element at the end of the array,
2803 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2804 //
2805 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002806 uint32_t needed_count = 0;
2807 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2808 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2809 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2810 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002811 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002812 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002813 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002814
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002815 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002816 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2817 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2818 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2819 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002820 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002821
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002822 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002823 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002824 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002825 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2826 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002827
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002828 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002829 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002830 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002831 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
2832 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002833
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002834 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002835 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002836 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002837 return false;
2838 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002839 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002840
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002841 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002842 break;
2843
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002844 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002845 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002846 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002847
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002848 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002849 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002850 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002851
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002852 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002853 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002854 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002855
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002856 case DT_SYMENT:
2857 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002858 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
2859 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002860 return false;
2861 }
2862 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002863
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002864 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002865#if defined(USE_RELA)
2866 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002867 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002868 return false;
2869 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002870#else
2871 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002872 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002873 return false;
2874 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002875#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002876 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002877
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002878 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002879#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002880 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002881#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002882 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002883#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002884 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002885
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002886 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002887#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002888 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002889#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002890 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002891#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002892 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002893
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002894 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002895#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002896 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002897 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002898#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002899 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2900 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002901
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002902 case DT_DEBUG:
2903 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2904 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002905// FIXME: not working currently for N64
2906// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002907// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002908// read-only, but the DYNAMIC header claims it is writable.
2909#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002910 if ((dynamic_flags & PF_W) != 0) {
2911 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2912 }
Chris Dearman99186652014-02-06 20:36:51 -08002913#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002914 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002915#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002916 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002917 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002918 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002919
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002920 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002921 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002922 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002923
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002924 case DT_ANDROID_RELA:
2925 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2926 break;
2927
2928 case DT_ANDROID_RELASZ:
2929 android_relocs_size_ = d->d_un.d_val;
2930 break;
2931
2932 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002933 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002934 return false;
2935
2936 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002937 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002938 return false;
2939
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002940 case DT_RELAENT:
2941 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002942 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002943 return false;
2944 }
2945 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002946
2947 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002948 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002949 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002950
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002951 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002952 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002953 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002954
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002955 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002956 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002957 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002958
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002959#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002960 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002961 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002962 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002963
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002964 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002965 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002966 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002967
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002968 case DT_RELENT:
2969 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002970 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002971 return false;
2972 }
2973 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002974
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002975 case DT_ANDROID_REL:
2976 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2977 break;
2978
2979 case DT_ANDROID_RELSZ:
2980 android_relocs_size_ = d->d_un.d_val;
2981 break;
2982
2983 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002984 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002985 return false;
2986
2987 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002988 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002989 return false;
2990
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002991 // "Indicates that all RELATIVE relocations have been concatenated together,
2992 // and specifies the RELATIVE relocation count."
2993 //
2994 // TODO: Spec also mentions that this can be used to optimize relocation process;
2995 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002996 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002997 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002998
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002999 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003000 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003001 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003002
3003 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003004 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003005 return false;
3006
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003007#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003008 case DT_INIT:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003009 init_func_ = reinterpret_cast<linker_ctor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003010 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003011 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003012
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003013 case DT_FINI:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003014 fini_func_ = reinterpret_cast<linker_dtor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003015 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003016 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003017
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003018 case DT_INIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003019 init_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003020 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003021 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003022
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003023 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003024 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003025 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003026
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003027 case DT_FINI_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003028 fini_array_ = reinterpret_cast<linker_dtor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003029 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003030 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003031
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003032 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003033 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003034 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003035
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003036 case DT_PREINIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003037 preinit_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003038 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003039 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003040
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003041 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003042 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003043 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003044
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003045 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003046#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003047 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003048 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003049#else
3050 has_text_relocations = true;
3051 break;
3052#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003053
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003054 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003055 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003056 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003057
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003058 case DT_NEEDED:
3059 ++needed_count;
3060 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003061
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003062 case DT_FLAGS:
3063 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003064#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003065 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003066 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003067#else
3068 has_text_relocations = true;
3069#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003070 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003071 if (d->d_un.d_val & DF_SYMBOLIC) {
3072 has_DT_SYMBOLIC = true;
3073 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003074 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003075
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003076 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003077 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003078
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003079 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003080 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 -07003081 }
3082 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003083#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003084 case DT_MIPS_RLD_MAP:
3085 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3086 {
3087 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3088 *dp = &_r_debug;
3089 }
3090 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003091 case DT_MIPS_RLD_MAP_REL:
3092 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003093 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003094 r_debug** dp = reinterpret_cast<r_debug**>(
3095 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003096 *dp = &_r_debug;
3097 }
3098 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003099
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003100 case DT_MIPS_RLD_VERSION:
3101 case DT_MIPS_FLAGS:
3102 case DT_MIPS_BASE_ADDRESS:
3103 case DT_MIPS_UNREFEXTNO:
3104 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003105
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003106 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003107 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003108 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003109
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003110 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003111 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003112 break;
3113
3114 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003115 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003116 break;
3117#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003118 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3119 case DT_BIND_NOW:
3120 break;
3121
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003122 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003123 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3124 break;
3125
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003126 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003127 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3128 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003129 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003130 verdef_cnt_ = d->d_un.d_val;
3131 break;
3132
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003133 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003134 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3135 break;
3136
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003137 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003138 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003139 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003140
Evgenii Stepanov68650822015-06-10 13:38:39 -07003141 case DT_RUNPATH:
3142 // this is parsed after we have strtab initialized (see below).
3143 break;
3144
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003145 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003146 if (!relocating_linker) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003147 DL_WARN("\"%s\" unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003148 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3149 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003150 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003151 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003152 }
3153
Duane Sandbc425c72015-06-01 16:29:14 -07003154#if defined(__mips__) && !defined(__LP64__)
3155 if (!mips_check_and_adjust_fp_modes()) {
3156 return false;
3157 }
3158#endif
3159
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003160 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003161 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003162
3163 // Sanity checks.
3164 if (relocating_linker && needed_count != 0) {
3165 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3166 return false;
3167 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003168 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003169 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003170 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003171 return false;
3172 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003173 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003174 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003175 return false;
3176 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003177 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003178 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003179 return false;
3180 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003181
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003182 // second pass - parse entries relying on strtab
3183 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003184 switch (d->d_tag) {
3185 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003186 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003187 break;
3188 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003189 set_dt_runpath(get_string(d->d_un.d_val));
3190 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003191 }
3192 }
3193
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003194 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003195 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003196 // because they can't find dt_needed library by soname.
3197 // This workaround should keep them working. (applies only
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003198 // for apps targeting sdk version < M). Make an exception for
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003199 // the main executable and linker; they do not need to have dt_soname
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003200 if (soname_ == nullptr &&
3201 this != solist_get_somain() &&
3202 (flags_ & FLAG_LINKER) == 0 &&
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003203 get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003204 soname_ = basename(realpath_.c_str());
3205 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3206 get_realpath(), soname_);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003207 // 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 -07003208 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003209 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003210}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003211
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003212bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3213 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003214
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003215 local_group_root_ = local_group.front();
3216 if (local_group_root_ == nullptr) {
3217 local_group_root_ = this;
3218 }
3219
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003220 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3221 target_sdk_version_ = get_application_target_sdk_version();
3222 }
3223
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003224 VersionTracker version_tracker;
3225
3226 if (!version_tracker.init(this)) {
3227 return false;
3228 }
3229
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003230#if !defined(__LP64__)
3231 if (has_text_relocations) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003232 // Fail if app is targeting M or above.
3233 if (get_application_target_sdk_version() >= __ANDROID_API_M__) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003234 DL_ERR_AND_LOG("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003235 return false;
3236 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003237 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003238 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003239 DL_WARN("\"%s\" has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003240 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003241 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003242 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3243 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003244 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003245 return false;
3246 }
3247 }
3248#endif
3249
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003250 if (android_relocs_ != nullptr) {
3251 // check signature
3252 if (android_relocs_size_ > 3 &&
3253 android_relocs_[0] == 'A' &&
3254 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003255 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003256 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003257 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003258
3259 bool relocated = false;
3260 const uint8_t* packed_relocs = android_relocs_ + 4;
3261 const size_t packed_relocs_size = android_relocs_size_ - 4;
3262
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003263 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003264 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003265 packed_reloc_iterator<sleb128_decoder>(
3266 sleb128_decoder(packed_relocs, packed_relocs_size)),
3267 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003268
3269 if (!relocated) {
3270 return false;
3271 }
3272 } else {
3273 DL_ERR("bad android relocation header.");
3274 return false;
3275 }
3276 }
3277
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003278#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003279 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003280 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003281 if (!relocate(version_tracker,
3282 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003283 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003284 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003285 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003286 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003287 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003288 if (!relocate(version_tracker,
3289 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003290 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003291 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003292 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003293#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003294 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003295 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003296 if (!relocate(version_tracker,
3297 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003298 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003299 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003300 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003301 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003302 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003303 if (!relocate(version_tracker,
3304 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003305 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003306 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003307 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003308#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003309
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003310#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003311 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003312 return false;
3313 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003314#endif
3315
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003316 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003317
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003318#if !defined(__LP64__)
3319 if (has_text_relocations) {
3320 // All relocations are done, we can protect our segments back to read-only.
3321 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3322 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003323 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003324 return false;
3325 }
3326 }
3327#endif
3328
Mingwei Shibe910522015-11-12 07:02:14 +00003329 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3330 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3331 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003332 return false;
3333 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003334
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003335 /* Handle serializing/sharing the RELRO segment */
3336 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3337 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3338 extinfo->relro_fd) < 0) {
3339 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003340 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003341 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003342 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003343 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3344 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3345 extinfo->relro_fd) < 0) {
3346 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003347 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003348 return false;
3349 }
3350 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003351
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003352 notify_gdb_of_load(this);
3353 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003354}
3355
Mingwei Shibe910522015-11-12 07:02:14 +00003356bool soinfo::protect_relro() {
3357 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3358 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3359 get_realpath(), strerror(errno));
3360 return false;
3361 }
3362 return true;
3363}
3364
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003365void init_default_namespace() {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003366 g_default_namespace.set_name("(default)");
3367 g_default_namespace.set_isolated(false);
3368
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003369 soinfo* somain = solist_get_somain();
3370
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003371 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3372 somain->load_bias);
3373 const char* bname = basename(interp);
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003374
3375 bool is_asan = bname != nullptr &&
3376 (strcmp(bname, "linker_asan") == 0 ||
3377 strcmp(bname, "linker_asan64") == 0);
3378 auto default_ld_paths = is_asan ? kAsanDefaultLdPaths : kDefaultLdPaths;
3379 g_is_asan = is_asan;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003380
neo.chae2589f9d2016-10-04 11:00:27 +09003381 char real_path[PATH_MAX];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003382 std::vector<std::string> ld_default_paths;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003383 for (size_t i = 0; default_ld_paths[i] != nullptr; ++i) {
3384 if (realpath(default_ld_paths[i], real_path) != nullptr) {
neo.chae2589f9d2016-10-04 11:00:27 +09003385 ld_default_paths.push_back(real_path);
3386 } else {
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003387 ld_default_paths.push_back(default_ld_paths[i]);
neo.chae2589f9d2016-10-04 11:00:27 +09003388 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003389 }
3390
3391 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003392};