blob: 7b16cc38a2a0f9030e05bd2ac37a300b5fc9fb2f [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 Ivanov4a2c5aa2015-12-10 16:08:14 -08001864 soinfo* const caller = find_containing_library(caller_addr);
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001865 android_namespace_t* ns = get_caller_namespace(caller);
1866
1867 LD_LOG(kLogDlopen,
1868 "dlopen(name=\"%s\", flags=0x%x, extinfo=%s, caller=\"%s\", caller_ns=%s@%p) ...",
1869 name,
1870 flags,
1871 android_dlextinfo_to_string(extinfo).c_str(),
1872 caller == nullptr ? "(null)" : caller->get_realpath(),
1873 ns == nullptr ? "(null)" : ns->get_name(),
1874 ns);
1875
1876 auto failure_guard = make_scope_guard([&]() {
1877 LD_LOG(kLogDlopen, "... dlopen failed: %s", linker_get_error_buffer());
1878 });
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001879
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07001880 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08001881 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001882 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08001883 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001884
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001885 if (extinfo != nullptr) {
1886 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
1887 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
1888 return nullptr;
1889 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07001890
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001891 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001892 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001893 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
1894 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001895 return nullptr;
1896 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07001897
1898 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
1899 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
1900 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
1901 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
1902 return nullptr;
1903 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001904
1905 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
1906 if (extinfo->library_namespace == nullptr) {
1907 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
1908 return nullptr;
1909 }
1910 ns = extinfo->library_namespace;
1911 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001912 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001913
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001914 std::string asan_name_holder;
1915
1916 const char* translated_name = name;
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001917 if (g_is_asan && translated_name != nullptr && translated_name[0] == '/') {
1918 char translated_path[PATH_MAX];
1919 if (realpath(translated_name, translated_path) != nullptr) {
Evgenii Stepanov5b715002016-10-03 15:09:28 -07001920 if (file_is_under_dir(translated_path, kSystemLibDir)) {
1921 asan_name_holder = std::string(kAsanSystemLibDir) + "/" +
1922 (translated_path + strlen(kSystemLibDir) + 1);
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001923 if (file_exists(asan_name_holder.c_str())) {
1924 translated_name = asan_name_holder.c_str();
1925 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1926 }
Evgenii Stepanov5b715002016-10-03 15:09:28 -07001927 } else if (file_is_under_dir(translated_path, kVendorLibDir)) {
1928 asan_name_holder = std::string(kAsanVendorLibDir) + "/" +
1929 (translated_path + strlen(kVendorLibDir) + 1);
Dimitry Ivanov6c14f862016-12-05 13:35:47 -08001930 if (file_exists(asan_name_holder.c_str())) {
1931 translated_name = asan_name_holder.c_str();
1932 PRINT("linker_asan dlopen translating \"%s\" -> \"%s\"", name, translated_name);
1933 }
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001934 }
1935 }
1936 }
1937
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001938 ProtectedDataGuard guard;
Dimitry Ivanov45d25ca2016-08-09 19:38:43 -07001939 soinfo* si = find_library(ns, translated_name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001940 if (si != nullptr) {
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001941 void* handle = si->to_handle();
1942 LD_LOG(kLogDlopen,
Dimitry Ivanovae4a0c12016-11-21 10:44:35 -08001943 "... dlopen calling constructors: realpath=\"%s\", soname=\"%s\", handle=%p",
1944 si->get_realpath(), si->get_soname(), handle);
1945 si->call_constructors();
1946 failure_guard.disable();
1947 LD_LOG(kLogDlopen,
Dimitry Ivanovb996d602016-07-11 18:11:39 -07001948 "... dlopen successful: realpath=\"%s\", soname=\"%s\", handle=%p",
1949 si->get_realpath(), si->get_soname(), handle);
1950 return handle;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001951 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001952
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001953 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07001954}
1955
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001956int do_dladdr(const void* addr, Dl_info* info) {
1957 // Determine if this address can be found in any library currently mapped.
1958 soinfo* si = find_containing_library(addr);
1959 if (si == nullptr) {
1960 return 0;
1961 }
1962
1963 memset(info, 0, sizeof(Dl_info));
1964
1965 info->dli_fname = si->get_realpath();
1966 // Address at which the shared object is loaded.
1967 info->dli_fbase = reinterpret_cast<void*>(si->base);
1968
1969 // Determine if any symbol in the library contains the specified address.
1970 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
1971 if (sym != nullptr) {
1972 info->dli_sname = si->get_string(sym->st_name);
1973 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
1974 }
1975
1976 return 1;
1977}
1978
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001979static soinfo* soinfo_from_handle(void* handle) {
1980 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
1981 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
1982 if (it == g_soinfo_handles_map.end()) {
1983 return nullptr;
1984 } else {
1985 return it->second;
1986 }
1987 }
1988
1989 return static_cast<soinfo*>(handle);
1990}
1991
Dimitry Ivanovd9e427c2016-11-22 16:55:25 -08001992bool do_dlsym(void* handle,
1993 const char* sym_name,
1994 const char* sym_ver,
1995 const void* caller_addr,
1996 void** symbol) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001997#if !defined(__LP64__)
1998 if (handle == nullptr) {
1999 DL_ERR("dlsym failed: library handle is null");
2000 return false;
2001 }
2002#endif
2003
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002004 soinfo* found = nullptr;
2005 const ElfW(Sym)* sym = nullptr;
2006 soinfo* caller = find_containing_library(caller_addr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002007 android_namespace_t* ns = get_caller_namespace(caller);
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002008 soinfo* si = nullptr;
2009 if (handle != RTLD_DEFAULT && handle != RTLD_NEXT) {
2010 si = soinfo_from_handle(handle);
2011 }
2012
2013 LD_LOG(kLogDlsym,
2014 "dlsym(handle=%p(\"%s\"), sym_name=\"%s\", sym_ver=\"%s\", caller=\"%s\", caller_ns=%s@%p) ...",
2015 handle,
2016 si != nullptr ? si->get_realpath() : "n/a",
2017 sym_name,
2018 sym_ver,
2019 caller == nullptr ? "(null)" : caller->get_realpath(),
2020 ns == nullptr ? "(null)" : ns->get_name(),
2021 ns);
2022
2023 auto failure_guard = make_scope_guard([&]() {
2024 LD_LOG(kLogDlsym, "... dlsym failed: %s", linker_get_error_buffer());
2025 });
2026
2027 if (sym_name == nullptr) {
2028 DL_ERR("dlsym failed: symbol name is null");
2029 return false;
2030 }
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002031
2032 version_info vi_instance;
2033 version_info* vi = nullptr;
2034
2035 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002036 vi_instance.name = sym_ver;
2037 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002038 vi = &vi_instance;
2039 }
2040
2041 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2042 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2043 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002044 if (si == nullptr) {
2045 DL_ERR("dlsym failed: invalid handle: %p", handle);
2046 return false;
2047 }
2048 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002049 }
2050
2051 if (sym != nullptr) {
2052 uint32_t bind = ELF_ST_BIND(sym->st_info);
2053
2054 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2055 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
Dimitry Ivanov4742abd2016-12-12 16:30:15 -08002056 failure_guard.disable();
2057 LD_LOG(kLogDlsym,
2058 "... dlsym successful: sym_name=\"%s\", sym_ver=\"%s\", found in=\"%s\", address=%p",
2059 sym_name, sym_ver, found->get_soname(), *symbol);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002060 return true;
2061 }
2062
2063 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2064 return false;
2065 }
2066
2067 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2068 return false;
2069}
2070
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002071int do_dlclose(void* handle) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002072 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002073 soinfo* si = soinfo_from_handle(handle);
2074 if (si == nullptr) {
2075 DL_ERR("invalid handle: %p", handle);
2076 return -1;
2077 }
2078
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002079 soinfo_unload(si);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002080 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002081}
2082
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002083bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path) {
2084 if (g_anonymous_namespace_initialized) {
2085 DL_ERR("anonymous namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002086 return false;
2087 }
2088
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002089 ProtectedDataGuard guard;
2090
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002091 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002092 // When the caller is nullptr - create_namespace will take global group
2093 // from the anonymous namespace, which is fine because anonymous namespace
2094 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002095 android_namespace_t* anon_ns =
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002096 create_namespace(nullptr,
2097 "(anonymous)",
2098 nullptr,
2099 library_search_path,
2100 // TODO (dimitry): change to isolated eventually.
2101 ANDROID_NAMESPACE_TYPE_REGULAR,
2102 nullptr,
2103 &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002104
2105 if (anon_ns == nullptr) {
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002106 return false;
2107 }
2108
2109 if (!link_namespaces(anon_ns, &g_default_namespace, shared_lib_sonames)) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002110 return false;
2111 }
Dimitry Ivanov7d429d32017-02-01 15:28:52 -08002112
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002113 g_anonymous_namespace = anon_ns;
Dimitry Ivanov3e0821d2017-03-07 11:02:10 -08002114 g_anonymous_namespace_initialized = true;
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002115
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002116 return true;
2117}
2118
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002119static void add_soinfos_to_namespace(const soinfo_list_t& soinfos, android_namespace_t* ns) {
2120 ns->add_soinfos(soinfos);
2121 for (auto si : soinfos) {
2122 si->add_secondary_namespace(ns);
2123 }
2124}
2125
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002126android_namespace_t* create_namespace(const void* caller_addr,
2127 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002128 const char* ld_library_path,
2129 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002130 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002131 const char* permitted_when_isolated_path,
2132 android_namespace_t* parent_namespace) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002133 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002134 // if parent_namespace is nullptr -> set it to the caller namespace
2135 soinfo* caller_soinfo = find_containing_library(caller_addr);
2136
2137 parent_namespace = caller_soinfo != nullptr ?
2138 caller_soinfo->get_primary_namespace() :
2139 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002140 }
2141
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002142 ProtectedDataGuard guard;
2143 std::vector<std::string> ld_library_paths;
2144 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002145 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002146
2147 parse_path(ld_library_path, ":", &ld_library_paths);
2148 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002149 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002150
2151 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2152 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002153 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002154 ns->set_ld_library_paths(std::move(ld_library_paths));
2155 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002156 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002157
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002158 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002159 // If shared - clone the parent namespace
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002160 add_soinfos_to_namespace(parent_namespace->soinfo_list(), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002161 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002162 // If not shared - copy only the shared group
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002163 add_soinfos_to_namespace(get_shared_group(parent_namespace), ns);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002164 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002165
2166 return ns;
2167}
2168
Dimitry Ivanov7a34b9d2017-02-03 14:07:34 -08002169bool link_namespaces(android_namespace_t* namespace_from,
2170 android_namespace_t* namespace_to,
2171 const char* shared_lib_sonames) {
2172 if (namespace_to == nullptr) {
2173 namespace_to = &g_default_namespace;
2174 }
2175
2176 if (namespace_from == nullptr) {
2177 DL_ERR("error linking namespaces: namespace_from is null.");
2178 return false;
2179 }
2180
2181 if (shared_lib_sonames == nullptr || shared_lib_sonames[0] == '\0') {
2182 DL_ERR("error linking namespaces \"%s\"->\"%s\": the list of shared libraries is empty.",
2183 namespace_from->get_name(), namespace_to->get_name());
2184 return false;
2185 }
2186
2187 auto sonames = android::base::Split(shared_lib_sonames, ":");
2188 std::unordered_set<std::string> sonames_set(sonames.begin(), sonames.end());
2189
2190 ProtectedDataGuard guard;
2191 namespace_from->add_linked_namespace(namespace_to, sonames_set);
2192
2193 return true;
2194}
2195
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002196ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002197 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2198 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2199 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002200 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2201 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002202
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002203 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002204}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002205
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002206const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2207 if (source_symver < 2 ||
2208 source_symver >= version_infos.size() ||
2209 version_infos[source_symver].name == nullptr) {
2210 return nullptr;
2211 }
2212
2213 return &version_infos[source_symver];
2214}
2215
2216void VersionTracker::add_version_info(size_t source_index,
2217 ElfW(Word) elf_hash,
2218 const char* ver_name,
2219 const soinfo* target_si) {
2220 if (source_index >= version_infos.size()) {
2221 version_infos.resize(source_index+1);
2222 }
2223
2224 version_infos[source_index].elf_hash = elf_hash;
2225 version_infos[source_index].name = ver_name;
2226 version_infos[source_index].target_si = target_si;
2227}
2228
2229bool VersionTracker::init_verneed(const soinfo* si_from) {
2230 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2231
2232 if (verneed_ptr == 0) {
2233 return true;
2234 }
2235
2236 size_t verneed_cnt = si_from->get_verneed_cnt();
2237
2238 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2239 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2240 size_t vernaux_offset = offset + verneed->vn_aux;
2241 offset += verneed->vn_next;
2242
2243 if (verneed->vn_version != 1) {
2244 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2245 return false;
2246 }
2247
2248 const char* target_soname = si_from->get_string(verneed->vn_file);
2249 // find it in dependencies
2250 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002251 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002252 });
2253
2254 if (target_si == nullptr) {
2255 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002256 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002257 return false;
2258 }
2259
2260 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2261 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2262 vernaux_offset += vernaux->vna_next;
2263
2264 const ElfW(Word) elf_hash = vernaux->vna_hash;
2265 const char* ver_name = si_from->get_string(vernaux->vna_name);
2266 ElfW(Half) source_index = vernaux->vna_other;
2267
2268 add_version_info(source_index, elf_hash, ver_name, target_si);
2269 }
2270 }
2271
2272 return true;
2273}
2274
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002275template <typename F>
2276static bool for_each_verdef(const soinfo* si, F functor) {
2277 if (!si->has_min_version(2)) {
2278 return true;
2279 }
2280
2281 uintptr_t verdef_ptr = si->get_verdef_ptr();
2282 if (verdef_ptr == 0) {
2283 return true;
2284 }
2285
2286 size_t offset = 0;
2287
2288 size_t verdef_cnt = si->get_verdef_cnt();
2289 for (size_t i = 0; i<verdef_cnt; ++i) {
2290 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
2291 size_t verdaux_offset = offset + verdef->vd_aux;
2292 offset += verdef->vd_next;
2293
2294 if (verdef->vd_version != 1) {
2295 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
2296 i, verdef->vd_version, si->get_realpath());
2297 return false;
2298 }
2299
2300 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
2301 // "this is the version of the file itself. It must not be used for
2302 // matching a symbol. It can be used to match references."
2303 //
2304 // http://www.akkadia.org/drepper/symbol-versioning
2305 continue;
2306 }
2307
2308 if (verdef->vd_cnt == 0) {
2309 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
2310 return false;
2311 }
2312
2313 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
2314
2315 if (functor(i, verdef, verdaux) == true) {
2316 break;
2317 }
2318 }
2319
2320 return true;
2321}
2322
2323bool find_verdef_version_index(const soinfo* si, const version_info* vi, ElfW(Versym)* versym) {
2324 if (vi == nullptr) {
2325 *versym = kVersymNotNeeded;
2326 return true;
2327 }
2328
2329 *versym = kVersymGlobal;
2330
2331 return for_each_verdef(si,
2332 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2333 if (verdef->vd_hash == vi->elf_hash &&
2334 strcmp(vi->name, si->get_string(verdaux->vda_name)) == 0) {
2335 *versym = verdef->vd_ndx;
2336 return true;
2337 }
2338
2339 return false;
2340 }
2341 );
2342}
2343
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002344bool VersionTracker::init_verdef(const soinfo* si_from) {
2345 return for_each_verdef(si_from,
2346 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2347 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2348 si_from->get_string(verdaux->vda_name), si_from);
2349 return false;
2350 }
2351 );
2352}
2353
2354bool VersionTracker::init(const soinfo* si_from) {
2355 if (!si_from->has_min_version(2)) {
2356 return true;
2357 }
2358
2359 return init_verneed(si_from) && init_verdef(si_from);
2360}
2361
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002362// TODO (dimitry): Methods below need to be moved out of soinfo
2363// and in more isolated file in order minimize dependencies on
2364// unnecessary object in the linker binary. Consider making them
2365// independent from soinfo (?).
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002366bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2367 const char* sym_name, const version_info** vi) {
2368 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2369 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2370
2371 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2372 *vi = version_tracker.get_version_info(sym_ver);
2373
2374 if (*vi == nullptr) {
2375 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002376 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002377 return false;
2378 }
2379 } else {
2380 // there is no version info
2381 *vi = nullptr;
2382 }
2383
2384 return true;
2385}
2386
Dimitry Ivanov576a3752016-08-09 06:58:55 -07002387#if !defined(__mips__)
2388#if defined(USE_RELA)
2389static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2390 return rela->r_addend;
2391}
2392#else
2393static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
2394 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2395 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
2396 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2397 }
2398 return 0;
2399}
2400#endif
2401
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002402template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002403bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2404 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002405 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2406 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002407 if (rel == nullptr) {
2408 return false;
2409 }
2410
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002411 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2412 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2413
2414 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002415 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002416 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002417 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002418
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002419 DEBUG("Processing \"%s\" relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002420 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002421 continue;
2422 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002423
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002424 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002425 soinfo* lsi = nullptr;
2426
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002427 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002428 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002429 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002430
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002431 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2432 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002433 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002434
2435 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2436 return false;
2437 }
2438
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002439 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002440 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002441 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002442 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002443 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002444 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002445 }
2446
2447 /* IHI0044C AAELF 4.5.1.1:
2448
2449 Libraries are not searched to resolve weak references.
2450 It is not an error for a weak reference to remain unsatisfied.
2451
2452 During linking, the value of an undefined weak reference is:
2453 - Zero if the relocation type is absolute
2454 - The address of the place if the relocation is pc-relative
2455 - The address of nominal base address if the relocation
2456 type is base-relative.
2457 */
2458
2459 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002460 case R_GENERIC_JUMP_SLOT:
2461 case R_GENERIC_GLOB_DAT:
2462 case R_GENERIC_RELATIVE:
2463 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002464#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002465 case R_AARCH64_ABS64:
2466 case R_AARCH64_ABS32:
2467 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002468#elif defined(__x86_64__)
2469 case R_X86_64_32:
2470 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002471#elif defined(__arm__)
2472 case R_ARM_ABS32:
2473#elif defined(__i386__)
2474 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002475#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002476 /*
2477 * The sym_addr was initialized to be zero above, or the relocation
2478 * code below does not care about value of sym_addr.
2479 * No need to do anything.
2480 */
2481 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002482#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002483 case R_X86_64_PC32:
2484 sym_addr = reloc;
2485 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002486#elif defined(__i386__)
2487 case R_386_PC32:
2488 sym_addr = reloc;
2489 break;
2490#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002491 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002492 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002493 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002494 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002495 } else { // We got a definition.
2496#if !defined(__LP64__)
2497 // When relocating dso with text_relocation .text segment is
2498 // not executable. We need to restore elf flags before resolving
2499 // STT_GNU_IFUNC symbol.
2500 bool protect_segments = has_text_relocations &&
2501 lsi == this &&
2502 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2503 if (protect_segments) {
2504 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2505 DL_ERR("can't protect segments for \"%s\": %s",
2506 get_realpath(), strerror(errno));
2507 return false;
2508 }
2509 }
2510#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002511 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002512#if !defined(__LP64__)
2513 if (protect_segments) {
2514 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2515 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2516 get_realpath(), strerror(errno));
2517 return false;
2518 }
2519 }
2520#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002521 }
2522 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002523 }
2524
2525 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002526 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002527 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002528 MARK(rel->r_offset);
2529 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2530 reinterpret_cast<void*>(reloc),
2531 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2532
2533 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002534 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002535 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002536 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002537 MARK(rel->r_offset);
2538 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2539 reinterpret_cast<void*>(reloc),
2540 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2541 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002542 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002543 case R_GENERIC_RELATIVE:
2544 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002545 MARK(rel->r_offset);
2546 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2547 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002548 reinterpret_cast<void*>(load_bias + addend));
2549 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002550 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002551 case R_GENERIC_IRELATIVE:
2552 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002553 MARK(rel->r_offset);
2554 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2555 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002556 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002557 {
2558#if !defined(__LP64__)
2559 // When relocating dso with text_relocation .text segment is
2560 // not executable. We need to restore elf flags for this
2561 // particular call.
2562 if (has_text_relocations) {
2563 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2564 DL_ERR("can't protect segments for \"%s\": %s",
2565 get_realpath(), strerror(errno));
2566 return false;
2567 }
2568 }
2569#endif
2570 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2571#if !defined(__LP64__)
2572 // Unprotect it afterwards...
2573 if (has_text_relocations) {
2574 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2575 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2576 get_realpath(), strerror(errno));
2577 return false;
2578 }
2579 }
2580#endif
2581 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2582 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002583 break;
2584
2585#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002586 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002587 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002588 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002589 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002590 reloc, sym_addr + addend, sym_name);
2591 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002592 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002593 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002594 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002595 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002596 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002597 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002598 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002599 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2600 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002601 if ((min_value <= (sym_addr + addend)) &&
2602 ((sym_addr + addend) <= max_value)) {
2603 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002604 } else {
2605 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002606 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002607 return false;
2608 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002609 }
2610 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002611 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002612 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002613 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002614 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002615 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002616 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002617 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2618 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002619 if ((min_value <= (sym_addr + addend)) &&
2620 ((sym_addr + addend) <= max_value)) {
2621 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002622 } else {
2623 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002624 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002625 return false;
2626 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002627 }
2628 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002629 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002630 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002631 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002632 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002633 reloc, sym_addr + addend, rel->r_offset, sym_name);
2634 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002635 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002636 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002637 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002638 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002639 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002640 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002641 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002642 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2643 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002644 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2645 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2646 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002647 } else {
2648 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002649 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002650 return false;
2651 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002652 }
2653 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002654 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002655 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002656 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002657 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002658 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002659 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002660 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2661 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002662 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
2663 ((sym_addr + addend - rel->r_offset) <= max_value)) {
2664 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002665 } else {
2666 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002667 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002668 return false;
2669 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002670 }
2671 break;
2672
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002673 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07002674 /*
2675 * ET_EXEC is not supported so this should not happen.
2676 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002677 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07002678 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002679 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07002680 * R_AARCH64_COPY may only appear in executable objects where e_type is
2681 * set to ET_EXEC.
2682 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002683 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002684 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002685 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002686 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002687 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002688 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002689 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08002690 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %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;
2693#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002694 case R_X86_64_32:
2695 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002696 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002697 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2698 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002699 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002700 break;
2701 case R_X86_64_64:
2702 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002703 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002704 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
2705 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002706 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002707 break;
2708 case R_X86_64_PC32:
2709 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002710 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002711 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
2712 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
2713 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09002714 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002715 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002716#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002717 case R_ARM_ABS32:
2718 count_relocation(kRelocAbsolute);
2719 MARK(rel->r_offset);
2720 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
2721 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2722 break;
2723 case R_ARM_REL32:
2724 count_relocation(kRelocRelative);
2725 MARK(rel->r_offset);
2726 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
2727 reloc, sym_addr, rel->r_offset, sym_name);
2728 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
2729 break;
2730 case R_ARM_COPY:
2731 /*
2732 * ET_EXEC is not supported so this should not happen.
2733 *
2734 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
2735 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002736 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002737 * R_ARM_COPY may only appear in executable objects where e_type is
2738 * set to ET_EXEC.
2739 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002740 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002741 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002742#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002743 case R_386_32:
2744 count_relocation(kRelocRelative);
2745 MARK(rel->r_offset);
2746 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
2747 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
2748 break;
2749 case R_386_PC32:
2750 count_relocation(kRelocRelative);
2751 MARK(rel->r_offset);
2752 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
2753 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
2754 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
2755 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002756#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002757 default:
2758 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002759 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002760 }
2761 }
2762 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002763}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002764#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07002765
Dimitry Ivanov48ec2882016-08-04 11:50:36 -07002766// An empty list of soinfos
Dimitry Ivanovb943f302016-08-03 16:00:10 -07002767static soinfo_list_t g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07002768
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002769bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08002770 /* Extract dynamic section */
2771 ElfW(Word) dynamic_flags = 0;
2772 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07002773
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002774 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002775 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002776 if (!relocating_linker) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002777 INFO("[ Linking \"%s\" ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002778 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002779 }
2780
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002781 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002782 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002783 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02002784 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002785 return false;
2786 } else {
2787 if (!relocating_linker) {
2788 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002789 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002790 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002791
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002792#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002793 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
2794 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02002795#endif
2796
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002797 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002798 // Note that: "Except for the DT_NULL element at the end of the array,
2799 // and the relative order of DT_NEEDED elements, entries may appear in any order."
2800 //
2801 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002802 uint32_t needed_count = 0;
2803 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2804 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
2805 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2806 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002807 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07002808 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002809 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002810
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002811 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002812 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
2813 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
2814 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
2815 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002816 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002817
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002818 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002819 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002820 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002821 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
2822 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002823
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002824 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07002825 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002826 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002827 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
2828 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002829
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002830 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002831 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002832 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002833 return false;
2834 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002835 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002836
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002837 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08002838 break;
2839
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002840 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002841 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002842 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002843
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002844 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002845 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07002846 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002847
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002848 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002849 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002850 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002851
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002852 case DT_SYMENT:
2853 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002854 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
2855 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002856 return false;
2857 }
2858 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002859
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002860 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002861#if defined(USE_RELA)
2862 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002863 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002864 return false;
2865 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002866#else
2867 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002868 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002869 return false;
2870 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002871#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07002872 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002873
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002874 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002875#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002876 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002877#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002878 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002879#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002880 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002881
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002882 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002883#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002884 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002885#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002886 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002887#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002888 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002889
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002890 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002891#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002892 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002893 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002894#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002895 // Ignore for other platforms... (because RTLD_LAZY is not supported)
2896 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002897
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002898 case DT_DEBUG:
2899 // Set the DT_DEBUG entry to the address of _r_debug for GDB
2900 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08002901// FIXME: not working currently for N64
2902// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002903// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08002904// read-only, but the DYNAMIC header claims it is writable.
2905#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002906 if ((dynamic_flags & PF_W) != 0) {
2907 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
2908 }
Chris Dearman99186652014-02-06 20:36:51 -08002909#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08002910 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002911#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002912 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002913 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002914 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002915
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002916 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002917 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002918 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002919
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002920 case DT_ANDROID_RELA:
2921 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2922 break;
2923
2924 case DT_ANDROID_RELASZ:
2925 android_relocs_size_ = d->d_un.d_val;
2926 break;
2927
2928 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002929 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002930 return false;
2931
2932 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002933 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002934 return false;
2935
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002936 case DT_RELAENT:
2937 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002938 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002939 return false;
2940 }
2941 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002942
2943 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002944 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002945 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002946
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002947 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002948 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002949 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002950
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002951 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002952 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002953 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002954
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002955#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002956 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002957 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002958 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002959
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002960 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002961 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002962 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002963
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002964 case DT_RELENT:
2965 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07002966 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002967 return false;
2968 }
2969 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002970
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002971 case DT_ANDROID_REL:
2972 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
2973 break;
2974
2975 case DT_ANDROID_RELSZ:
2976 android_relocs_size_ = d->d_un.d_val;
2977 break;
2978
2979 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002980 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002981 return false;
2982
2983 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002984 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002985 return false;
2986
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07002987 // "Indicates that all RELATIVE relocations have been concatenated together,
2988 // and specifies the RELATIVE relocation count."
2989 //
2990 // TODO: Spec also mentions that this can be used to optimize relocation process;
2991 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002992 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07002993 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002994
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002995 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07002996 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002997 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002998
2999 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003000 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003001 return false;
3002
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003003#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003004 case DT_INIT:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003005 init_func_ = reinterpret_cast<linker_ctor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003006 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003007 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003008
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003009 case DT_FINI:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003010 fini_func_ = reinterpret_cast<linker_dtor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003011 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003012 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003013
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003014 case DT_INIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003015 init_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003016 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003017 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003018
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003019 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003020 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
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_FINI_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003024 fini_array_ = reinterpret_cast<linker_dtor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003025 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003026 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003027
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003028 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003029 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003030 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003031
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003032 case DT_PREINIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003033 preinit_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003034 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003035 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003036
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003037 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003038 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003039 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003040
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003041 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003042#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003043 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003044 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003045#else
3046 has_text_relocations = true;
3047 break;
3048#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003049
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003050 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003051 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003052 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003053
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003054 case DT_NEEDED:
3055 ++needed_count;
3056 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003057
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003058 case DT_FLAGS:
3059 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003060#if defined(__LP64__)
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003061 DL_ERR("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003062 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003063#else
3064 has_text_relocations = true;
3065#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003066 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003067 if (d->d_un.d_val & DF_SYMBOLIC) {
3068 has_DT_SYMBOLIC = true;
3069 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003070 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003071
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003072 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003073 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003074
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003075 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003076 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 -07003077 }
3078 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003079#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003080 case DT_MIPS_RLD_MAP:
3081 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3082 {
3083 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3084 *dp = &_r_debug;
3085 }
3086 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003087 case DT_MIPS_RLD_MAP_REL:
3088 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003089 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003090 r_debug** dp = reinterpret_cast<r_debug**>(
3091 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003092 *dp = &_r_debug;
3093 }
3094 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003095
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003096 case DT_MIPS_RLD_VERSION:
3097 case DT_MIPS_FLAGS:
3098 case DT_MIPS_BASE_ADDRESS:
3099 case DT_MIPS_UNREFEXTNO:
3100 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003101
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003102 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003103 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003104 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003105
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003106 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003107 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003108 break;
3109
3110 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003111 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003112 break;
3113#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003114 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3115 case DT_BIND_NOW:
3116 break;
3117
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003118 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003119 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3120 break;
3121
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003122 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003123 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3124 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003125 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003126 verdef_cnt_ = d->d_un.d_val;
3127 break;
3128
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003129 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003130 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3131 break;
3132
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003133 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003134 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003135 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003136
Evgenii Stepanov68650822015-06-10 13:38:39 -07003137 case DT_RUNPATH:
3138 // this is parsed after we have strtab initialized (see below).
3139 break;
3140
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003141 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003142 if (!relocating_linker) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003143 DL_WARN("\"%s\" unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003144 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3145 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003146 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003147 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003148 }
3149
Duane Sandbc425c72015-06-01 16:29:14 -07003150#if defined(__mips__) && !defined(__LP64__)
3151 if (!mips_check_and_adjust_fp_modes()) {
3152 return false;
3153 }
3154#endif
3155
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003156 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003157 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003158
3159 // Sanity checks.
3160 if (relocating_linker && needed_count != 0) {
3161 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3162 return false;
3163 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003164 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003165 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003166 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003167 return false;
3168 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003169 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003170 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003171 return false;
3172 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003173 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003174 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003175 return false;
3176 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003177
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003178 // second pass - parse entries relying on strtab
3179 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003180 switch (d->d_tag) {
3181 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003182 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003183 break;
3184 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003185 set_dt_runpath(get_string(d->d_un.d_val));
3186 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003187 }
3188 }
3189
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003190 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003191 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003192 // because they can't find dt_needed library by soname.
3193 // This workaround should keep them working. (applies only
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003194 // for apps targeting sdk version < M). Make an exception for
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003195 // the main executable and linker; they do not need to have dt_soname
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003196 if (soname_ == nullptr &&
3197 this != solist_get_somain() &&
3198 (flags_ & FLAG_LINKER) == 0 &&
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003199 get_application_target_sdk_version() < __ANDROID_API_M__) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003200 soname_ = basename(realpath_.c_str());
3201 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3202 get_realpath(), soname_);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003203 // 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 -07003204 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003205 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003206}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003207
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003208bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3209 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003210
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003211 local_group_root_ = local_group.front();
3212 if (local_group_root_ == nullptr) {
3213 local_group_root_ = this;
3214 }
3215
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003216 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3217 target_sdk_version_ = get_application_target_sdk_version();
3218 }
3219
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003220 VersionTracker version_tracker;
3221
3222 if (!version_tracker.init(this)) {
3223 return false;
3224 }
3225
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003226#if !defined(__LP64__)
3227 if (has_text_relocations) {
Elliott Hughes5bc78c82016-11-16 11:35:43 -08003228 // Fail if app is targeting M or above.
3229 if (get_application_target_sdk_version() >= __ANDROID_API_M__) {
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003230 DL_ERR_AND_LOG("\"%s\" has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003231 return false;
3232 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003233 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003234 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov816676e2016-10-19 11:00:28 -07003235 DL_WARN("\"%s\" has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003236 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003237 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003238 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
3239 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003240 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003241 return false;
3242 }
3243 }
3244#endif
3245
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003246 if (android_relocs_ != nullptr) {
3247 // check signature
3248 if (android_relocs_size_ > 3 &&
3249 android_relocs_[0] == 'A' &&
3250 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003251 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003252 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003253 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003254
3255 bool relocated = false;
3256 const uint8_t* packed_relocs = android_relocs_ + 4;
3257 const size_t packed_relocs_size = android_relocs_size_ - 4;
3258
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003259 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003260 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07003261 packed_reloc_iterator<sleb128_decoder>(
3262 sleb128_decoder(packed_relocs, packed_relocs_size)),
3263 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003264
3265 if (!relocated) {
3266 return false;
3267 }
3268 } else {
3269 DL_ERR("bad android relocation header.");
3270 return false;
3271 }
3272 }
3273
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003274#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003275 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003276 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003277 if (!relocate(version_tracker,
3278 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003279 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003280 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003281 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003282 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003283 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003284 if (!relocate(version_tracker,
3285 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003286 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003287 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003288 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003289#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003290 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003291 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003292 if (!relocate(version_tracker,
3293 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003294 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003295 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003296 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003297 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003298 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003299 if (!relocate(version_tracker,
3300 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003301 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003302 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003303 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003304#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07003305
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003306#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07003307 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003308 return false;
3309 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003310#endif
3311
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003312 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003313
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003314#if !defined(__LP64__)
3315 if (has_text_relocations) {
3316 // All relocations are done, we can protect our segments back to read-only.
3317 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
3318 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003319 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003320 return false;
3321 }
3322 }
3323#endif
3324
Mingwei Shibe910522015-11-12 07:02:14 +00003325 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
3326 // itself --- it can't make system calls yet, and will have to call protect_relro later.
3327 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003328 return false;
3329 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08003330
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003331 /* Handle serializing/sharing the RELRO segment */
3332 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
3333 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
3334 extinfo->relro_fd) < 0) {
3335 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003336 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003337 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003338 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003339 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
3340 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
3341 extinfo->relro_fd) < 0) {
3342 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003343 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003344 return false;
3345 }
3346 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00003347
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003348 notify_gdb_of_load(this);
3349 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003350}
3351
Mingwei Shibe910522015-11-12 07:02:14 +00003352bool soinfo::protect_relro() {
3353 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
3354 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
3355 get_realpath(), strerror(errno));
3356 return false;
3357 }
3358 return true;
3359}
3360
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003361void init_default_namespace() {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003362 g_default_namespace.set_name("(default)");
3363 g_default_namespace.set_isolated(false);
3364
Dimitry Ivanov3f660572016-09-09 10:00:39 -07003365 soinfo* somain = solist_get_somain();
3366
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003367 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
3368 somain->load_bias);
3369 const char* bname = basename(interp);
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003370
3371 bool is_asan = bname != nullptr &&
3372 (strcmp(bname, "linker_asan") == 0 ||
3373 strcmp(bname, "linker_asan64") == 0);
3374 auto default_ld_paths = is_asan ? kAsanDefaultLdPaths : kDefaultLdPaths;
3375 g_is_asan = is_asan;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003376
neo.chae2589f9d2016-10-04 11:00:27 +09003377 char real_path[PATH_MAX];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003378 std::vector<std::string> ld_default_paths;
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003379 for (size_t i = 0; default_ld_paths[i] != nullptr; ++i) {
3380 if (realpath(default_ld_paths[i], real_path) != nullptr) {
neo.chae2589f9d2016-10-04 11:00:27 +09003381 ld_default_paths.push_back(real_path);
3382 } else {
Dimitry Ivanov77ad6422017-03-06 13:02:29 -08003383 ld_default_paths.push_back(default_ld_paths[i]);
neo.chae2589f9d2016-10-04 11:00:27 +09003384 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003385 }
3386
3387 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07003388};