blob: 68b3a537a201024c03d2e6d0242107ab893a22a3 [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.
Mingwei Shibe910522015-11-12 07:02:14 +000047#include "private/bionic_globals.h"
Elliott Hugheseb847bc2013-10-09 15:50:50 -070048#include "private/bionic_tls.h"
49#include "private/KernelArgumentBlock.h"
50#include "private/ScopedPthreadMutexLocker.h"
Dmitriy Ivanov14669a92014-09-05 16:42:53 -070051#include "private/ScopeGuard.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080052
53#include "linker.h"
Dmitriy Ivanovc9ce70d2015-03-10 15:30:26 -070054#include "linker_block_allocator.h"
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -080055#include "linker_gdb_support.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080056#include "linker_debug.h"
Dimitry Ivanov769b33f2016-07-21 11:33:40 -070057#include "linker_dlwarning.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"
Josh Gao3cc387e2016-06-15 18:33:26 -070066#include "debuggerd/client.h"
Simon Baldwinaef71952015-01-16 13:22:54 +000067#include "ziparchive/zip_archive.h"
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080068
Josh Gao93c0f5e2015-10-06 11:08:13 -070069extern void __libc_init_globals(KernelArgumentBlock&);
Elliott Hughes1801db32015-06-08 18:04:00 -070070extern void __libc_init_AT_SECURE(KernelArgumentBlock&);
71
Mingwei Shibe910522015-11-12 07:02:14 +000072extern "C" void _start();
73
Elliott Hughes1801db32015-06-08 18:04:00 -070074// Override macros to use C++ style casts.
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -080075#undef ELF_ST_TYPE
76#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
77
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -070078struct android_namespace_t {
79 public:
80 android_namespace_t() : name_(nullptr), is_isolated_(false) {}
81
82 const char* get_name() const { return name_; }
83 void set_name(const char* name) { name_ = name; }
84
85 bool is_isolated() const { return is_isolated_; }
86 void set_isolated(bool isolated) { is_isolated_ = isolated; }
87
88 const std::vector<std::string>& get_ld_library_paths() const {
89 return ld_library_paths_;
90 }
91 void set_ld_library_paths(std::vector<std::string>&& library_paths) {
92 ld_library_paths_ = library_paths;
93 }
94
95 const std::vector<std::string>& get_default_library_paths() const {
96 return default_library_paths_;
97 }
98 void set_default_library_paths(std::vector<std::string>&& library_paths) {
99 default_library_paths_ = library_paths;
100 }
101
Dimitry Ivanovd17a3772016-03-01 13:11:28 -0800102 const std::vector<std::string>& get_permitted_paths() const {
103 return permitted_paths_;
104 }
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800105 void set_permitted_paths(std::vector<std::string>&& permitted_paths) {
106 permitted_paths_ = permitted_paths;
107 }
108
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700109 void add_soinfo(soinfo* si) {
110 soinfo_list_.push_back(si);
111 }
112
113 void add_soinfos(const soinfo::soinfo_list_t& soinfos) {
114 for (auto si : soinfos) {
115 add_soinfo(si);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700116 si->add_secondary_namespace(this);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700117 }
118 }
119
120 void remove_soinfo(soinfo* si) {
121 soinfo_list_.remove_if([&](soinfo* candidate) {
122 return si == candidate;
123 });
124 }
125
126 const soinfo::soinfo_list_t& soinfo_list() const { return soinfo_list_; }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700127
128 // For isolated namespaces - checks if the file is on the search path;
129 // always returns true for not isolated namespace.
130 bool is_accessible(const std::string& path);
131
132 private:
133 const char* name_;
134 bool is_isolated_;
135 std::vector<std::string> ld_library_paths_;
136 std::vector<std::string> default_library_paths_;
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800137 std::vector<std::string> permitted_paths_;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700138 soinfo::soinfo_list_t soinfo_list_;
139
140 DISALLOW_COPY_AND_ASSIGN(android_namespace_t);
141};
142
143android_namespace_t g_default_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -0700144
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700145static std::unordered_map<uintptr_t, soinfo*> g_soinfo_handles_map;
146static android_namespace_t* g_anonymous_namespace = &g_default_namespace;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700147
Elliott Hughes0266ae52014-02-10 17:46:57 -0800148static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800149
Dmitriy Ivanov600bc3c2015-03-10 15:43:50 -0700150static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
151static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200152
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700153static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700154static LinkerTypeAllocator<LinkedListEntry<android_namespace_t>> g_namespace_list_allocator;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700155
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700156static soinfo* solist;
157static soinfo* sonext;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700158static soinfo* somain; // main process, always the one after libdl_info
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800159
Elliott Hughes1728b232014-05-14 10:02:03 -0700160static const char* const kDefaultLdPaths[] = {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700161#if defined(__LP64__)
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700162 "/system/lib64",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800163 "/vendor/lib64",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700164#else
Elliott Hughes124fae92012-10-31 14:20:03 -0700165 "/system/lib",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800166 "/vendor/lib",
Elliott Hughes011bc0b2013-10-08 14:27:10 -0700167#endif
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700168 nullptr
Elliott Hughes124fae92012-10-31 14:20:03 -0700169};
David Bartleybc3a5c22009-06-02 18:27:28 -0700170
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700171static const char* const kAsanDefaultLdPaths[] = {
172#if defined(__LP64__)
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700173 "/data/lib64",
174 "/system/lib64",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800175 "/data/vendor/lib64",
176 "/vendor/lib64",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700177#else
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700178 "/data/lib",
179 "/system/lib",
Dimitry Ivanov88f51112016-02-01 23:00:55 -0800180 "/data/vendor/lib",
181 "/vendor/lib",
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700182#endif
183 nullptr
184};
185
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700186static bool is_system_library(const std::string& realpath) {
187 for (const auto& dir : g_default_namespace.get_default_library_paths()) {
188 if (file_is_in_dir(realpath, dir)) {
189 return true;
190 }
191 }
192 return false;
193}
194
195#if defined(__LP64__)
196static const char* const kSystemLibDir = "/system/lib64";
197#else
198static const char* const kSystemLibDir = "/system/lib";
199#endif
200
201static std::string dirname(const char *path);
202
203// TODO(dimitry): The grey-list is a workaround for http://b/26394120 ---
204// gradually remove libraries from this list until it is gone.
205static bool is_greylisted(const char* name, const soinfo* needed_by) {
206 static const char* const kLibraryGreyList[] = {
207 "libandroid_runtime.so",
208 "libbinder.so",
209 "libcrypto.so",
210 "libcutils.so",
211 "libexpat.so",
212 "libgui.so",
213 "libmedia.so",
214 "libnativehelper.so",
215 "libskia.so",
216 "libssl.so",
217 "libstagefright.so",
218 "libsqlite.so",
219 "libui.so",
220 "libutils.so",
221 "libvorbisidec.so",
222 nullptr
223 };
224
225 // limit greylisting to apps targeting sdk version 23 and below
226 if (get_application_target_sdk_version() > 23) {
227 return false;
228 }
229
230 // if the library needed by a system library - implicitly assume it
231 // is greylisted
232
233 if (needed_by != nullptr && is_system_library(needed_by->get_realpath())) {
234 return true;
235 }
236
237 // if this is an absolute path - make sure it points to /system/lib(64)
238 if (name[0] == '/' && dirname(name) == kSystemLibDir) {
239 // and reduce the path to basename
240 name = basename(name);
241 }
242
243 for (size_t i = 0; kLibraryGreyList[i] != nullptr; ++i) {
244 if (strcmp(name, kLibraryGreyList[i]) == 0) {
245 return true;
246 }
247 }
248
249 return false;
250}
251// END OF WORKAROUND
252
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700253static const ElfW(Versym) kVersymNotNeeded = 0;
254static const ElfW(Versym) kVersymGlobal = 1;
255
Evgenii Stepanovd640b222015-07-10 17:54:01 -0700256static const char* const* g_default_ld_paths;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700257static std::vector<std::string> g_ld_preload_names;
Elliott Hughesa4aafd12014-01-13 16:37:47 -0800258
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700259static std::vector<soinfo*> g_ld_preloads;
Matt Fischer4fd42c12009-12-31 12:09:10 -0600260
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700261static bool g_public_namespace_initialized;
262static soinfo::soinfo_list_t g_public_namespace;
263
Dimitry Ivanovb996d602016-07-11 18:11:39 -0700264int g_ld_debug_verbosity;
265abort_msg_t* g_abort_message = nullptr; // For debuggerd.
Dimitry Ivanov55437462016-07-20 15:33:07 -0700266
267// These values are used to call constructors for .init_array && .preinit_array
268int g_argc = 0;
269char** g_argv = nullptr;
270char** g_envp = nullptr;
Elliott Hughes0d787c12013-04-04 13:46:46 -0700271
Evgenii Stepanov68650822015-06-10 13:38:39 -0700272static std::string dirname(const char *path) {
273 const char* last_slash = strrchr(path, '/');
274 if (last_slash == path) return "/";
275 else if (last_slash == nullptr) return ".";
276 else
277 return std::string(path, last_slash - path);
278}
279
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800280#if STATS
Elliott Hughesbedfe382012-08-14 14:07:59 -0700281struct linker_stats_t {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700282 int count[kRelocMax];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700283};
284
285static linker_stats_t linker_stats;
286
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800287void count_relocation(RelocationKind kind) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700288 ++linker_stats.count[kind];
Elliott Hughesbedfe382012-08-14 14:07:59 -0700289}
290#else
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800291void count_relocation(RelocationKind) {
Elliott Hughesbedfe382012-08-14 14:07:59 -0700292}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800293#endif
294
295#if COUNT_PAGES
Dmitriy Ivanov114ff692015-01-14 11:36:38 -0800296uint32_t bitmask[4096];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800297#endif
298
Dima Zavin2e855792009-05-20 18:28:09 -0700299static char __linker_dl_err_buf[768];
Dima Zavin2e855792009-05-20 18:28:09 -0700300
Elliott Hughes650be4e2013-03-05 18:47:58 -0800301char* linker_get_error_buffer() {
Elliott Hughes5419b942012-10-16 15:54:46 -0700302 return &__linker_dl_err_buf[0];
Dima Zavin2e855792009-05-20 18:28:09 -0700303}
304
Elliott Hughes650be4e2013-03-05 18:47:58 -0800305size_t linker_get_error_buffer_size() {
306 return sizeof(__linker_dl_err_buf);
307}
308
Elliott Hughesbedfe382012-08-14 14:07:59 -0700309static void notify_gdb_of_load(soinfo* info) {
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800310 if (info->is_linker() || info->is_main_executable()) {
311 // gdb already knows about the linker and the main executable.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700312 return;
313 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800314
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800315 link_map* map = &(info->link_map_head);
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000316
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800317 map->l_addr = info->load_bias;
318 // link_map l_name field is not const.
319 map->l_name = const_cast<char*>(info->get_realpath());
320 map->l_ld = info->dynamic;
Nicolas Geoffray0fa54102016-02-18 09:31:24 +0000321
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -0800322 CHECK(map->l_name != nullptr);
323 CHECK(map->l_name[0] != '\0');
324
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800325 notify_gdb_of_load(map);
Iliyan Malchev5e12d7e2009-03-24 19:02:00 -0700326}
327
Elliott Hughesbedfe382012-08-14 14:07:59 -0700328static void notify_gdb_of_unload(soinfo* info) {
Dimitry Ivanov6b788ee2016-02-17 16:08:03 -0800329 notify_gdb_of_unload(&(info->link_map_head));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800330}
331
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700332bool android_namespace_t::is_accessible(const std::string& file) {
333 if (!is_isolated_) {
334 return true;
335 }
336
337 for (const auto& dir : ld_library_paths_) {
338 if (file_is_in_dir(file, dir)) {
339 return true;
340 }
341 }
342
343 for (const auto& dir : default_library_paths_) {
344 if (file_is_in_dir(file, dir)) {
345 return true;
346 }
347 }
348
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800349 for (const auto& dir : permitted_paths_) {
350 if (file_is_under_dir(file, dir)) {
351 return true;
352 }
353 }
354
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700355 return false;
356}
357
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700358LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
359 return g_soinfo_links_allocator.alloc();
360}
361
362void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
363 g_soinfo_links_allocator.free(entry);
364}
365
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700366LinkedListEntry<android_namespace_t>* NamespaceListAllocator::alloc() {
367 return g_namespace_list_allocator.alloc();
368}
369
370void NamespaceListAllocator::free(LinkedListEntry<android_namespace_t>* entry) {
371 g_namespace_list_allocator.free(entry);
372}
373
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700374static soinfo* soinfo_alloc(android_namespace_t* ns, const char* name,
375 struct stat* file_stat, off64_t file_offset,
376 uint32_t rtld_flags) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700377 if (strlen(name) >= PATH_MAX) {
Magnus Malmbornba98d922012-09-12 13:00:55 +0200378 DL_ERR("library name \"%s\" too long", name);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700379 return nullptr;
Magnus Malmbornba98d922012-09-12 13:00:55 +0200380 }
381
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700382 soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat,
383 file_offset, rtld_flags);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700384
Magnus Malmbornba98d922012-09-12 13:00:55 +0200385 sonext->next = si;
386 sonext = si;
387
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700388 si->generate_handle();
389 ns->add_soinfo(si);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700390
Elliott Hughesca0c11b2013-03-12 10:40:45 -0700391 TRACE("name %s: allocated soinfo @ %p", name, si);
Magnus Malmbornba98d922012-09-12 13:00:55 +0200392 return si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800393}
394
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800395static void soinfo_free(soinfo* si) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700396 if (si == nullptr) {
397 return;
398 }
399
400 if (si->base != 0 && si->size != 0) {
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -0800401 if (!si->is_mapped_by_caller()) {
402 munmap(reinterpret_cast<void*>(si->base), si->size);
403 } else {
404 // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE
405 mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE,
406 MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0);
407 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700408 }
409
410 soinfo *prev = nullptr, *trav;
411
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700412 TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700413
414 for (trav = solist; trav != nullptr; trav = trav->next) {
415 if (trav == si) {
416 break;
Elliott Hughes46882792012-08-03 16:49:39 -0700417 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700418 prev = trav;
419 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800420
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700421 if (trav == nullptr) {
422 // si was not in solist
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700423 DL_ERR("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700424 return;
425 }
Elliott Hughes46882792012-08-03 16:49:39 -0700426
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700427 // clear links to/from si
428 si->remove_all_links();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -0700429
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700430 // prev will never be null, because the first entry in solist is
431 // always the static libdl_info.
432 prev->next = si->next;
433 if (si == sonext) {
434 sonext = prev;
435 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800436
Dmitriy Ivanov609f11b2015-07-08 15:26:46 -0700437 si->~soinfo();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700438 g_soinfo_allocator.free(si);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800439}
440
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700441// For every path element this function checks of it exists, and is a directory,
442// and normalizes it:
443// 1. For regular path it converts it to realpath()
444// 2. For path in a zip file it uses realpath on the zipfile
445// normalizes entry name by calling normalize_path function.
446static void resolve_paths(std::vector<std::string>& paths,
447 std::vector<std::string>* resolved_paths) {
448 resolved_paths->clear();
449 for (const auto& path : paths) {
450 char resolved_path[PATH_MAX];
451 const char* original_path = path.c_str();
452 if (realpath(original_path, resolved_path) != nullptr) {
453 struct stat s;
454 if (stat(resolved_path, &s) == 0) {
455 if (S_ISDIR(s.st_mode)) {
456 resolved_paths->push_back(resolved_path);
457 } else {
458 DL_WARN("Warning: \"%s\" is not a directory (excluding from path)", resolved_path);
459 continue;
460 }
461 } else {
462 DL_WARN("Warning: cannot stat file \"%s\": %s", resolved_path, strerror(errno));
463 continue;
464 }
465 } else {
466 std::string zip_path;
467 std::string entry_path;
468
469 std::string normalized_path;
470
471 if (!normalize_path(original_path, &normalized_path)) {
472 DL_WARN("Warning: unable to normalize \"%s\"", original_path);
473 continue;
474 }
475
476 if (parse_zip_path(normalized_path.c_str(), &zip_path, &entry_path)) {
477 if (realpath(zip_path.c_str(), resolved_path) == nullptr) {
478 DL_WARN("Warning: unable to resolve \"%s\": %s", zip_path.c_str(), strerror(errno));
479 continue;
480 }
481
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700482 resolved_paths->push_back(std::string(resolved_path) + kZipFileSeparator + entry_path);
483 }
484 }
485 }
486}
487
488static void split_path(const char* path, const char* delimiters,
Dmitriy Ivanovd165f562015-03-23 18:43:02 -0700489 std::vector<std::string>* paths) {
Dmitriy Ivanovfbfba642015-11-16 14:23:37 -0800490 if (path != nullptr && path[0] != 0) {
tony.ys_liub4474402015-07-29 18:00:22 +0800491 *paths = android::base::Split(path, delimiters);
Elliott Hughescade4c32012-12-20 14:42:14 -0800492 }
493}
494
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700495static void parse_path(const char* path, const char* delimiters,
496 std::vector<std::string>* resolved_paths) {
497 std::vector<std::string> paths;
498 split_path(path, delimiters, &paths);
499 resolve_paths(paths, resolved_paths);
500}
501
Elliott Hughescade4c32012-12-20 14:42:14 -0800502static void parse_LD_LIBRARY_PATH(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700503 std::vector<std::string> ld_libary_paths;
504 parse_path(path, ":", &ld_libary_paths);
505 g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths));
Elliott Hughescade4c32012-12-20 14:42:14 -0800506}
507
Evgenii Stepanov68650822015-06-10 13:38:39 -0700508void soinfo::set_dt_runpath(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700509 if (!has_min_version(3)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -0700510 return;
511 }
512
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700513 std::vector<std::string> runpaths;
514
515 split_path(path, ":", &runpaths);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700516
517 std::string origin = dirname(get_realpath());
518 // FIXME: add $LIB and $PLATFORM.
519 std::pair<std::string, std::string> substs[] = {{"ORIGIN", origin}};
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700520 for (auto&& s : runpaths) {
Evgenii Stepanov68650822015-06-10 13:38:39 -0700521 size_t pos = 0;
522 while (pos < s.size()) {
523 pos = s.find("$", pos);
524 if (pos == std::string::npos) break;
525 for (const auto& subst : substs) {
526 const std::string& token = subst.first;
527 const std::string& replacement = subst.second;
528 if (s.substr(pos + 1, token.size()) == token) {
529 s.replace(pos, token.size() + 1, replacement);
530 // -1 to compensate for the ++pos below.
531 pos += replacement.size() - 1;
532 break;
533 } else if (s.substr(pos + 1, token.size() + 2) == "{" + token + "}") {
534 s.replace(pos, token.size() + 3, replacement);
535 pos += replacement.size() - 1;
536 break;
537 }
538 }
539 // Skip $ in case it did not match any of the known substitutions.
540 ++pos;
541 }
542 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700543
544 resolve_paths(runpaths, &dt_runpath_);
Evgenii Stepanov68650822015-06-10 13:38:39 -0700545}
546
Elliott Hughescade4c32012-12-20 14:42:14 -0800547static void parse_LD_PRELOAD(const char* path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700548 g_ld_preload_names.clear();
549 if (path != nullptr) {
550 // We have historically supported ':' as well as ' ' in LD_PRELOAD.
551 g_ld_preload_names = android::base::Split(path, " :");
Dimitry Ivanovd799b2b2016-05-24 14:29:56 -0700552 std::remove_if(g_ld_preload_names.begin(),
553 g_ld_preload_names.end(),
554 [] (const std::string& s) { return s.empty(); });
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700555 }
Elliott Hughescade4c32012-12-20 14:42:14 -0800556}
557
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700558static bool realpath_fd(int fd, std::string* realpath) {
559 std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700560 __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700561 if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700562 PRINT("readlink(\"%s\") failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700563 return false;
564 }
565
Dmitriy Ivanova1feb112015-10-01 18:41:57 -0700566 *realpath = &buf[0];
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700567 return true;
568}
569
Elliott Hughes4eeb1f12013-10-25 17:38:02 -0700570#if defined(__arm__)
Elliott Hughes46882792012-08-03 16:49:39 -0700571
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700572// For a given PC, find the .so that it belongs to.
573// Returns the base address of the .ARM.exidx section
574// for that .so, and the number of 8-byte entries
575// in that section (via *pcount).
576//
577// Intended to be called by libc's __gnu_Unwind_Find_exidx().
578//
579// This function is exposed via dlfcn.cpp and libdl.so.
Elliott Hughesfaf05ba2014-02-11 16:59:37 -0800580_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800581 uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800582
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700583 for (soinfo* si = solist; si != 0; si = si->next) {
584 if ((addr >= si->base) && (addr < (si->base + si->size))) {
585 *pcount = si->ARM_exidx_count;
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -0800586 return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800587 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700588 }
589 *pcount = 0;
590 return nullptr;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800591}
Elliott Hughes46882792012-08-03 16:49:39 -0700592
Christopher Ferris24053a42013-08-19 17:45:09 -0700593#endif
Elliott Hughes46882792012-08-03 16:49:39 -0700594
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700595// Here, we only have to provide a callback to iterate across all the
596// loaded libraries. gcc_eh does the rest.
Dmitriy Ivanov7271caf2015-06-29 14:48:25 -0700597int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700598 int rv = 0;
599 for (soinfo* si = solist; si != nullptr; si = si->next) {
600 dl_phdr_info dl_info;
601 dl_info.dlpi_addr = si->link_map_head.l_addr;
602 dl_info.dlpi_name = si->link_map_head.l_name;
603 dl_info.dlpi_phdr = si->phdr;
604 dl_info.dlpi_phnum = si->phnum;
605 rv = cb(&dl_info, sizeof(dl_phdr_info), data);
606 if (rv != 0) {
607 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800608 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700609 }
610 return rv;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800611}
Elliott Hughes46882792012-08-03 16:49:39 -0700612
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700613const ElfW(Versym)* soinfo::get_versym(size_t n) const {
614 if (has_min_version(2) && versym_ != nullptr) {
615 return versym_ + n;
616 }
617
618 return nullptr;
619}
620
621ElfW(Addr) soinfo::get_verneed_ptr() const {
622 if (has_min_version(2)) {
623 return verneed_ptr_;
624 }
625
626 return 0;
627}
628
629size_t soinfo::get_verneed_cnt() const {
630 if (has_min_version(2)) {
631 return verneed_cnt_;
632 }
633
634 return 0;
635}
636
637ElfW(Addr) soinfo::get_verdef_ptr() const {
638 if (has_min_version(2)) {
639 return verdef_ptr_;
640 }
641
642 return 0;
643}
644
645size_t soinfo::get_verdef_cnt() const {
646 if (has_min_version(2)) {
647 return verdef_cnt_;
648 }
649
650 return 0;
651}
652
653template<typename F>
654static bool for_each_verdef(const soinfo* si, F functor) {
655 if (!si->has_min_version(2)) {
656 return true;
657 }
658
659 uintptr_t verdef_ptr = si->get_verdef_ptr();
660 if (verdef_ptr == 0) {
661 return true;
662 }
663
664 size_t offset = 0;
665
666 size_t verdef_cnt = si->get_verdef_cnt();
667 for (size_t i = 0; i<verdef_cnt; ++i) {
668 const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
669 size_t verdaux_offset = offset + verdef->vd_aux;
670 offset += verdef->vd_next;
671
672 if (verdef->vd_version != 1) {
Dmitriy Ivanov3d7bea12015-04-20 17:40:39 -0700673 DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700674 i, verdef->vd_version, si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700675 return false;
676 }
677
678 if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
679 // "this is the version of the file itself. It must not be used for
680 // matching a symbol. It can be used to match references."
681 //
682 // http://www.akkadia.org/drepper/symbol-versioning
683 continue;
684 }
685
686 if (verdef->vd_cnt == 0) {
687 DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
688 return false;
689 }
690
691 const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
692
693 if (functor(i, verdef, verdaux) == true) {
694 break;
695 }
696 }
697
698 return true;
699}
700
701bool soinfo::find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const {
702 if (vi == nullptr) {
703 *versym = kVersymNotNeeded;
704 return true;
705 }
706
707 *versym = kVersymGlobal;
708
709 return for_each_verdef(this,
710 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
711 if (verdef->vd_hash == vi->elf_hash &&
712 strcmp(vi->name, get_string(verdaux->vda_name)) == 0) {
713 *versym = verdef->vd_ndx;
714 return true;
715 }
716
717 return false;
718 }
719 );
720}
721
722bool soinfo::find_symbol_by_name(SymbolName& symbol_name,
723 const version_info* vi,
724 const ElfW(Sym)** symbol) const {
725 uint32_t symbol_index;
726 bool success =
727 is_gnu_hash() ?
728 gnu_lookup(symbol_name, vi, &symbol_index) :
729 elf_lookup(symbol_name, vi, &symbol_index);
730
731 if (success) {
732 *symbol = symbol_index == 0 ? nullptr : symtab_ + symbol_index;
733 }
734
735 return success;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800736}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800737
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800738static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
739 if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
740 ELF_ST_BIND(s->st_info) == STB_WEAK) {
741 return s->st_shndx != SHN_UNDEF;
742 } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -0700743 DL_WARN("unexpected ST_BIND value: %d for \"%s\" in \"%s\"",
744 ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800745 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800746
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800747 return false;
748}
749
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700750static const ElfW(Versym) kVersymHiddenBit = 0x8000;
751
752static inline bool is_versym_hidden(const ElfW(Versym)* versym) {
753 // the symbol is hidden if bit 15 of versym is set.
754 return versym != nullptr && (*versym & kVersymHiddenBit) != 0;
755}
756
757static inline bool check_symbol_version(const ElfW(Versym) verneed,
758 const ElfW(Versym)* verdef) {
759 return verneed == kVersymNotNeeded ||
760 verdef == nullptr ||
761 verneed == (*verdef & ~kVersymHiddenBit);
762}
763
764bool soinfo::gnu_lookup(SymbolName& symbol_name,
765 const version_info* vi,
766 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800767 uint32_t hash = symbol_name.gnu_hash();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800768 uint32_t h2 = hash >> gnu_shift2_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800769
770 uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800771 uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
772 ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800773
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700774 *symbol_index = 0;
775
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700776 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700777 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700778
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800779 // test against bloom filter
780 if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700781 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700782 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700783
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700784 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800785 }
786
787 // bloom test says "probably yes"...
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700788 uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800789
790 if (n == 0) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700791 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700792 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700793
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700794 return true;
795 }
796
797 // lookup versym for the version definition in this library
798 // note the difference between "version is not requested" (vi == nullptr)
799 // and "version not found". In the first case verneed is kVersymNotNeeded
800 // which implies that the default version can be accepted; the second case results in
801 // verneed = 1 (kVersymGlobal) and implies that we should ignore versioned symbols
802 // for this library and consider only *global* ones.
803 ElfW(Versym) verneed = 0;
804 if (!find_verdef_version_index(vi, &verneed)) {
805 return false;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800806 }
807
808 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800809 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700810 const ElfW(Versym)* verdef = get_versym(n);
811 // skip hidden versions when verneed == kVersymNotNeeded (0)
812 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
813 continue;
814 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700815 if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700816 check_symbol_version(verneed, verdef) &&
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800817 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
818 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700819 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700820 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value),
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700821 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700822 *symbol_index = n;
823 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800824 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -0700825 } while ((gnu_chain_[n++] & 1) == 0);
826
827 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700828 symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800829
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700830 return true;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800831}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800832
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700833bool soinfo::elf_lookup(SymbolName& symbol_name,
834 const version_info* vi,
835 uint32_t* symbol_index) const {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800836 uint32_t hash = symbol_name.elf_hash();
837
838 TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700839 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700840 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800841
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700842 ElfW(Versym) verneed = 0;
843 if (!find_verdef_version_index(vi, &verneed)) {
844 return false;
845 }
846
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800847 for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
848 ElfW(Sym)* s = symtab_ + n;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700849 const ElfW(Versym)* verdef = get_versym(n);
850
851 // skip hidden versions when verneed == 0
852 if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
853 continue;
854 }
855
856 if (check_symbol_version(verneed, verdef) &&
857 strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -0700858 is_symbol_global_and_defined(this, s)) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800859 TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700860 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700861 reinterpret_cast<void*>(s->st_value),
862 static_cast<size_t>(s->st_size));
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700863 *symbol_index = n;
864 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800865 }
Elliott Hughes0266ae52014-02-10 17:46:57 -0800866 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800867
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700868 TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700869 symbol_name.get_name(), get_realpath(),
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700870 reinterpret_cast<void*>(base), hash, hash % nbucket_);
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -0700871
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700872 *symbol_index = 0;
873 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800874}
875
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700876soinfo::soinfo(android_namespace_t* ns, const char* realpath,
877 const struct stat* file_stat, off64_t file_offset,
878 int rtld_flags) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700879 memset(this, 0, sizeof(*this));
880
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700881 if (realpath != nullptr) {
882 realpath_ = realpath;
883 }
884
Dmitriy Ivanovab972b92014-11-29 13:57:41 -0800885 flags_ = FLAG_NEW_SOINFO;
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800886 version_ = SOINFO_VERSION;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700887
Dmitriy Ivanov851135b2014-08-29 12:02:36 -0700888 if (file_stat != nullptr) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800889 this->st_dev_ = file_stat->st_dev;
890 this->st_ino_ = file_stat->st_ino;
891 this->file_offset_ = file_offset;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700892 }
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -0700893
Dmitriy Ivanov047b5932014-11-13 09:39:20 -0800894 this->rtld_flags_ = rtld_flags;
Dimitry Ivanovaca299a2016-04-11 12:42:58 -0700895 this->primary_namespace_ = ns;
Dmitriy Ivanov0d150942014-08-22 12:25:04 -0700896}
897
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -0700898soinfo::~soinfo() {
899 g_soinfo_handles_map.erase(handle_);
900}
901
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800902static uint32_t calculate_elf_hash(const char* name) {
903 const uint8_t* name_bytes = reinterpret_cast<const uint8_t*>(name);
904 uint32_t h = 0, g;
905
906 while (*name_bytes) {
907 h = (h << 4) + *name_bytes++;
908 g = h & 0xf0000000;
909 h ^= g;
910 h ^= g >> 24;
911 }
912
913 return h;
914}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800915
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800916uint32_t SymbolName::elf_hash() {
917 if (!has_elf_hash_) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -0800918 elf_hash_ = calculate_elf_hash(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800919 has_elf_hash_ = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700920 }
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800921
922 return elf_hash_;
923}
924
925uint32_t SymbolName::gnu_hash() {
926 if (!has_gnu_hash_) {
927 uint32_t h = 5381;
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700928 const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800929 while (*name != 0) {
930 h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
931 }
932
933 gnu_hash_ = h;
934 has_gnu_hash_ = true;
935 }
936
937 return gnu_hash_;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800938}
939
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700940bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
941 soinfo** si_found_in, const soinfo::soinfo_list_t& global_group,
942 const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -0800943 SymbolName symbol_name(name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700944 const ElfW(Sym)* s = nullptr;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -0700945
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700946 /* "This element's presence in a shared object library alters the dynamic linker's
947 * symbol resolution algorithm for references within the library. Instead of starting
948 * a symbol search with the executable file, the dynamic linker starts from the shared
949 * object itself. If the shared object fails to supply the referenced symbol, the
950 * dynamic linker then searches the executable file and other shared objects as usual."
951 *
952 * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
953 *
954 * Note that this is unlikely since static linker avoids generating
955 * relocations for -Bsymbolic linked dynamic executables.
956 */
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700957 if (si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700958 DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700959 if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
960 return false;
961 }
962
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -0700963 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700964 *si_found_in = si_from;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700965 }
966 }
967
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700968 // 1. Look for it in global_group
969 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700970 bool error = false;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700971 global_group.visit([&](soinfo* global_si) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -0700972 DEBUG("%s: looking up %s in %s (from global group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -0700973 si_from->get_realpath(), name, global_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700974 if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
975 error = true;
976 return false;
977 }
978
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700979 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700980 *si_found_in = global_si;
981 return false;
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700982 }
Dmitriy Ivanovc2048942014-08-29 10:15:25 -0700983
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700984 return true;
985 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700986
987 if (error) {
988 return false;
989 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -0700990 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -0700991
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700992 // 2. Look for it in the local group
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700993 if (s == nullptr) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -0700994 bool error = false;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -0700995 local_group.visit([&](soinfo* local_si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -0700996 if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
Dmitriy Ivanove47b3f82014-10-23 14:19:07 -0700997 // we already did this - skip
998 return true;
999 }
1000
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07001001 DEBUG("%s: looking up %s in %s (from local group)",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001002 si_from->get_realpath(), name, local_si->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001003 if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
1004 error = true;
1005 return false;
1006 }
1007
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001008 if (s != nullptr) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001009 *si_found_in = local_si;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001010 return false;
1011 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001012
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001013 return true;
1014 });
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001015
1016 if (error) {
1017 return false;
1018 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001019 }
1020
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001021 if (s != nullptr) {
1022 TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
1023 "found in %s, base = %p, load bias = %p",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07001024 si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
1025 (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001026 reinterpret_cast<void*>((*si_found_in)->load_bias));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07001027 }
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001028
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001029 *symbol = s;
1030 return true;
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001031}
1032
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001033class ProtectedDataGuard {
1034 public:
1035 ProtectedDataGuard() {
1036 if (ref_count_++ == 0) {
1037 protect_data(PROT_READ | PROT_WRITE);
1038 }
1039 }
1040
1041 ~ProtectedDataGuard() {
1042 if (ref_count_ == 0) { // overflow
1043 __libc_fatal("Too many nested calls to dlopen()");
1044 }
1045
1046 if (--ref_count_ == 0) {
1047 protect_data(PROT_READ);
1048 }
1049 }
1050 private:
1051 void protect_data(int protection) {
1052 g_soinfo_allocator.protect_all(protection);
1053 g_soinfo_links_allocator.protect_all(protection);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001054 g_namespace_allocator.protect_all(protection);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07001055 g_namespace_list_allocator.protect_all(protection);
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08001056 }
1057
1058 static size_t ref_count_;
1059};
1060
1061size_t ProtectedDataGuard::ref_count_ = 0;
1062
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001063// Each size has it's own allocator.
1064template<size_t size>
1065class SizeBasedAllocator {
1066 public:
1067 static void* alloc() {
1068 return allocator_.alloc();
1069 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001070
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001071 static void free(void* ptr) {
1072 allocator_.free(ptr);
1073 }
Dmitriy Ivanov4bea4982014-08-29 14:01:48 -07001074
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001075 private:
1076 static LinkerBlockAllocator allocator_;
1077};
1078
1079template<size_t size>
1080LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
1081
1082template<typename T>
1083class TypeBasedAllocator {
1084 public:
1085 static T* alloc() {
1086 return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
1087 }
1088
1089 static void free(T* ptr) {
1090 SizeBasedAllocator<sizeof(T)>::free(ptr);
1091 }
1092};
1093
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001094class LoadTask {
1095 public:
1096 struct deleter_t {
1097 void operator()(LoadTask* t) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001098 t->~LoadTask();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001099 TypeBasedAllocator<LoadTask>::free(t);
1100 }
1101 };
1102
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001103 static deleter_t deleter;
1104
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001105 static LoadTask* create(const char* name, soinfo* needed_by,
1106 std::unordered_map<const soinfo*, ElfReader>* readers_map) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001107 LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001108 return new (ptr) LoadTask(name, needed_by, readers_map);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001109 }
1110
1111 const char* get_name() const {
1112 return name_;
1113 }
1114
1115 soinfo* get_needed_by() const {
1116 return needed_by_;
1117 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001118
1119 soinfo* get_soinfo() const {
1120 return si_;
1121 }
1122
1123 void set_soinfo(soinfo* si) {
1124 si_ = si;
1125 }
1126
1127 off64_t get_file_offset() const {
1128 return file_offset_;
1129 }
1130
1131 void set_file_offset(off64_t offset) {
1132 file_offset_ = offset;
1133 }
1134
1135 int get_fd() const {
1136 return fd_;
1137 }
1138
1139 void set_fd(int fd, bool assume_ownership) {
1140 fd_ = fd;
1141 close_fd_ = assume_ownership;
1142 }
1143
1144 const android_dlextinfo* get_extinfo() const {
1145 return extinfo_;
1146 }
1147
1148 void set_extinfo(const android_dlextinfo* extinfo) {
1149 extinfo_ = extinfo;
1150 }
1151
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001152 bool is_dt_needed() const {
1153 return is_dt_needed_;
1154 }
1155
1156 void set_dt_needed(bool is_dt_needed) {
1157 is_dt_needed_ = is_dt_needed;
1158 }
1159
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001160 const ElfReader& get_elf_reader() const {
1161 CHECK(si_ != nullptr);
1162 return (*elf_readers_map_)[si_];
1163 }
1164
1165 ElfReader& get_elf_reader() {
1166 CHECK(si_ != nullptr);
1167 return (*elf_readers_map_)[si_];
1168 }
1169
1170 std::unordered_map<const soinfo*, ElfReader>* get_readers_map() {
1171 return elf_readers_map_;
1172 }
1173
1174 bool read(const char* realpath, off64_t file_size) {
1175 ElfReader& elf_reader = get_elf_reader();
1176 return elf_reader.Read(realpath, fd_, file_offset_, file_size);
1177 }
1178
1179 bool load() {
1180 ElfReader& elf_reader = get_elf_reader();
1181 if (!elf_reader.Load(extinfo_)) {
1182 return false;
1183 }
1184
1185 si_->base = elf_reader.load_start();
1186 si_->size = elf_reader.load_size();
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -08001187 si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller());
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001188 si_->load_bias = elf_reader.load_bias();
1189 si_->phnum = elf_reader.phdr_count();
1190 si_->phdr = elf_reader.loaded_phdr();
1191
1192 return true;
1193 }
1194
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001195 private:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001196 LoadTask(const char* name, soinfo* needed_by,
1197 std::unordered_map<const soinfo*, ElfReader>* readers_map)
1198 : name_(name), needed_by_(needed_by), si_(nullptr),
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001199 fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map),
1200 is_dt_needed_(false) {}
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001201
1202 ~LoadTask() {
1203 if (fd_ != -1 && close_fd_) {
1204 close(fd_);
1205 }
1206 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001207
1208 const char* name_;
1209 soinfo* needed_by_;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001210 soinfo* si_;
1211 const android_dlextinfo* extinfo_;
1212 int fd_;
1213 bool close_fd_;
1214 off64_t file_offset_;
1215 std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_;
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001216 // TODO(dimitry): needed by workaround for http://b/26394120 (the grey-list)
1217 bool is_dt_needed_;
1218 // END OF WORKAROUND
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001219
1220 DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
1221};
1222
Ningsheng Jiane93be992014-09-16 15:22:10 +08001223LoadTask::deleter_t LoadTask::deleter;
1224
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001225template <typename T>
1226using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
1227
1228typedef linked_list_t<soinfo> SoinfoLinkedList;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001229typedef linked_list_t<const char> StringLinkedList;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001230typedef std::vector<LoadTask*> LoadTaskList;
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001231
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001232
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001233// This function walks down the tree of soinfo dependencies
1234// in breadth-first order and
1235// * calls action(soinfo* si) for each node, and
1236// * terminates walk if action returns false.
1237//
1238// walk_dependencies_tree returns false if walk was terminated
1239// by the action and true otherwise.
1240template<typename F>
1241static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
Dmitriy Ivanov0cd83eb2014-09-01 16:15:52 -07001242 SoinfoLinkedList visit_list;
1243 SoinfoLinkedList visited;
1244
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001245 for (size_t i = 0; i < root_soinfos_size; ++i) {
1246 visit_list.push_back(root_soinfos[i]);
1247 }
1248
1249 soinfo* si;
1250 while ((si = visit_list.pop_front()) != nullptr) {
1251 if (visited.contains(si)) {
Dmitriy Ivanov042426b2014-08-12 21:02:13 -07001252 continue;
1253 }
1254
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001255 if (!action(si)) {
1256 return false;
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001257 }
1258
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001259 visited.push_back(si);
1260
1261 si->get_children().for_each([&](soinfo* child) {
Dmitriy Ivanovaa0f2bd2014-07-28 17:32:20 -07001262 visit_list.push_back(child);
1263 });
1264 }
1265
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001266 return true;
1267}
1268
1269
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001270static const ElfW(Sym)* dlsym_handle_lookup(soinfo* root, soinfo* skip_until,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001271 soinfo** found, SymbolName& symbol_name,
1272 const version_info* vi) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001273 const ElfW(Sym)* result = nullptr;
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001274 bool skip_lookup = skip_until != nullptr;
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001275
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001276 walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
1277 if (skip_lookup) {
1278 skip_lookup = current_soinfo != skip_until;
1279 return true;
1280 }
1281
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001282 if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001283 result = nullptr;
1284 return false;
1285 }
1286
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07001287 if (result != nullptr) {
1288 *found = current_soinfo;
1289 return false;
1290 }
1291
1292 return true;
1293 });
1294
1295 return result;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001296}
1297
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001298static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
1299 const char* name,
1300 const version_info* vi,
1301 soinfo** found,
1302 soinfo* caller,
1303 void* handle);
1304
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001305// This is used by dlsym(3). It performs symbol lookup only within the
1306// specified soinfo object and its dependencies in breadth first order.
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001307static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found,
1308 const char* name, const version_info* vi) {
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001309 // According to man dlopen(3) and posix docs in the case when si is handle
1310 // of the main executable we need to search not only in the executable and its
1311 // dependencies but also in all libraries loaded with RTLD_GLOBAL.
1312 //
1313 // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
1314 // libraries and they are loaded in breath-first (correct) order we can just execute
1315 // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
1316 if (si == somain) {
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001317 return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT);
Dmitriy Ivanovf439b5a2015-05-30 13:04:39 -07001318 }
1319
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001320 SymbolName symbol_name(name);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001321 return dlsym_handle_lookup(si, nullptr, found, symbol_name, vi);
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001322}
1323
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08001324/* This is used by dlsym(3) to performs a global symbol lookup. If the
1325 start value is null (for RTLD_DEFAULT), the search starts at the
1326 beginning of the global solist. Otherwise the search starts at the
1327 specified soinfo (for RTLD_NEXT).
Iliyan Malchev6ed80c82009-09-28 19:38:04 -07001328 */
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001329static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns,
1330 const char* name,
1331 const version_info* vi,
1332 soinfo** found,
1333 soinfo* caller,
1334 void* handle) {
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001335 SymbolName symbol_name(name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001336
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001337 auto& soinfo_list = ns->soinfo_list();
1338 auto start = soinfo_list.begin();
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001339
1340 if (handle == RTLD_NEXT) {
Dmitriy Ivanovb96ac412015-05-22 12:34:42 -07001341 if (caller == nullptr) {
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001342 return nullptr;
1343 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001344 auto it = soinfo_list.find(caller);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001345 CHECK (it != soinfo_list.end());
1346 start = ++it;
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001347 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001348 }
1349
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001350 const ElfW(Sym)* s = nullptr;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001351 for (auto it = start, end = soinfo_list.end(); it != end; ++it) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001352 soinfo* si = *it;
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001353 // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
1354 // if the library is opened by application with target api level <= 22
1355 // See http://b/21565766
1356 if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 && si->get_target_sdk_version() > 22) {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001357 continue;
1358 }
1359
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001360 if (!si->find_symbol_by_name(symbol_name, vi, &s)) {
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07001361 return nullptr;
1362 }
1363
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001364 if (s != nullptr) {
Elliott Hughescade4c32012-12-20 14:42:14 -08001365 *found = si;
1366 break;
Matt Fischer1698d9e2009-12-31 12:17:56 -06001367 }
Elliott Hughescade4c32012-12-20 14:42:14 -08001368 }
Matt Fischer1698d9e2009-12-31 12:17:56 -06001369
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001370 // If not found - use dlsym_handle_lookup for caller's
1371 // local_group unless it is part of the global group in which
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001372 // case we already did it.
1373 if (s == nullptr && caller != nullptr &&
1374 (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
Dmitriy Ivanov697bd9f2015-05-12 11:12:27 -07001375 return dlsym_handle_lookup(caller->get_local_group_root(),
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08001376 (handle == RTLD_NEXT) ? caller : nullptr, found, symbol_name, vi);
Dmitriy Ivanov76ac1ac2015-04-01 14:45:10 -07001377 }
1378
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001379 if (s != nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07001380 TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
1381 name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
Elliott Hughescade4c32012-12-20 14:42:14 -08001382 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001383
Elliott Hughescade4c32012-12-20 14:42:14 -08001384 return s;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001385}
1386
Kito Chengfa8c05d2013-03-12 14:58:06 +08001387soinfo* find_containing_library(const void* p) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08001388 ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001389 for (soinfo* si = solist; si != nullptr; si = si->next) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001390 if (address >= si->base && address - si->base < si->size) {
1391 return si;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001392 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001393 }
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001394 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001395}
1396
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001397ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
1398 return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
1399}
1400
1401static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
1402 return sym->st_shndx != SHN_UNDEF &&
1403 soaddr >= sym->st_value &&
1404 soaddr < sym->st_value + sym->st_size;
1405}
1406
1407ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001408 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001409
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001410 for (size_t i = 0; i < gnu_nbucket_; ++i) {
1411 uint32_t n = gnu_bucket_[i];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001412
1413 if (n == 0) {
1414 continue;
1415 }
1416
1417 do {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001418 ElfW(Sym)* sym = symtab_ + n;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001419 if (symbol_matches_soaddr(sym, soaddr)) {
1420 return sym;
1421 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07001422 } while ((gnu_chain_[n++] & 1) == 0);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001423 }
1424
1425 return nullptr;
1426}
1427
1428ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
Chris Dearman8e553812013-11-13 17:22:33 -08001429 ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001430
Kito Chengfa8c05d2013-03-12 14:58:06 +08001431 // Search the library's symbol table for any defined symbol which
1432 // contains this address.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08001433 for (size_t i = 0; i < nchain_; ++i) {
1434 ElfW(Sym)* sym = symtab_ + i;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08001435 if (symbol_matches_soaddr(sym, soaddr)) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08001436 return sym;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001437 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08001438 }
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001439
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07001440 return nullptr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -06001441}
1442
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001443class ZipArchiveCache {
1444 public:
1445 ZipArchiveCache() {}
1446 ~ZipArchiveCache();
1447
1448 bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
1449 private:
1450 DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
1451
1452 std::unordered_map<std::string, ZipArchiveHandle> cache_;
1453};
1454
1455bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
1456 std::string key(zip_path);
1457
1458 auto it = cache_.find(key);
1459 if (it != cache_.end()) {
1460 *handle = it->second;
1461 return true;
1462 }
1463
1464 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1465 if (fd == -1) {
1466 return false;
1467 }
1468
1469 if (OpenArchiveFd(fd, "", handle) != 0) {
1470 // invalid zip-file (?)
Yabin Cui722072d2016-03-21 17:10:12 -07001471 CloseArchive(handle);
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001472 close(fd);
1473 return false;
1474 }
1475
1476 cache_[key] = *handle;
1477 return true;
1478}
1479
1480ZipArchiveCache::~ZipArchiveCache() {
Dmitriy Ivanov5dce8942015-10-13 12:14:16 -07001481 for (const auto& it : cache_) {
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001482 CloseArchive(it.second);
1483 }
1484}
1485
1486static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001487 const char* const input_path,
1488 off64_t* file_offset, std::string* realpath) {
1489 std::string normalized_path;
1490 if (!normalize_path(input_path, &normalized_path)) {
1491 return -1;
1492 }
1493
1494 const char* const path = normalized_path.c_str();
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001495 TRACE("Trying zip file open from path \"%s\" -> normalized \"%s\"", input_path, path);
Simon Baldwinaef71952015-01-16 13:22:54 +00001496
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001497 // Treat an '!/' separator inside a path as the separator between the name
Simon Baldwinaef71952015-01-16 13:22:54 +00001498 // of the zip file on disk and the subdirectory to search within it.
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001499 // For example, if path is "foo.zip!/bar/bas/x.so", then we search for
Simon Baldwinaef71952015-01-16 13:22:54 +00001500 // "bar/bas/x.so" within "foo.zip".
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001501 const char* const separator = strstr(path, kZipFileSeparator);
Simon Baldwinaef71952015-01-16 13:22:54 +00001502 if (separator == nullptr) {
1503 return -1;
Elliott Hughes124fae92012-10-31 14:20:03 -07001504 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001505
1506 char buf[512];
1507 if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
1508 PRINT("Warning: ignoring very long library path: %s", path);
1509 return -1;
1510 }
1511
1512 buf[separator - path] = '\0';
1513
1514 const char* zip_path = buf;
Dmitriy Ivanov402a7502015-06-09 13:46:51 -07001515 const char* file_path = &buf[separator - path + 2];
Simon Baldwinaef71952015-01-16 13:22:54 +00001516 int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
1517 if (fd == -1) {
1518 return -1;
1519 }
1520
1521 ZipArchiveHandle handle;
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001522 if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001523 // invalid zip-file (?)
1524 close(fd);
1525 return -1;
1526 }
1527
Simon Baldwinaef71952015-01-16 13:22:54 +00001528 ZipEntry entry;
1529
Yusuke Sato56f40fb2015-06-25 14:56:07 -07001530 if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
Simon Baldwinaef71952015-01-16 13:22:54 +00001531 // Entry was not found.
1532 close(fd);
1533 return -1;
1534 }
1535
1536 // Check if it is properly stored
1537 if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
1538 close(fd);
1539 return -1;
1540 }
1541
1542 *file_offset = entry.offset;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001543
1544 if (realpath_fd(fd, realpath)) {
1545 *realpath += separator;
1546 } else {
1547 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
1548 normalized_path.c_str());
1549 *realpath = normalized_path;
1550 }
1551
Simon Baldwinaef71952015-01-16 13:22:54 +00001552 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001553}
1554
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001555static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
1556 int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
1557 if (n < 0 || n >= static_cast<int>(buf_size)) {
1558 PRINT("Warning: ignoring very long library path: %s/%s", path, name);
1559 return false;
1560 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001561
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001562 return true;
1563}
1564
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001565static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
1566 const char* name, off64_t* file_offset,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001567 const std::vector<std::string>& paths,
1568 std::string* realpath) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001569 for (const auto& path : paths) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001570 char buf[512];
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001571 if (!format_path(buf, sizeof(buf), path.c_str(), name)) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001572 continue;
1573 }
1574
1575 int fd = -1;
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001576 if (strstr(buf, kZipFileSeparator) != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001577 fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
Simon Baldwinaef71952015-01-16 13:22:54 +00001578 }
1579
1580 if (fd == -1) {
1581 fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
1582 if (fd != -1) {
1583 *file_offset = 0;
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001584 if (!realpath_fd(fd, realpath)) {
1585 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
1586 *realpath = buf;
1587 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001588 }
1589 }
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001590
1591 if (fd != -1) {
1592 return fd;
1593 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001594 }
1595
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07001596 return -1;
Simon Baldwinaef71952015-01-16 13:22:54 +00001597}
1598
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001599static int open_library(android_namespace_t* ns,
1600 ZipArchiveCache* zip_archive_cache,
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001601 const char* name, soinfo *needed_by,
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001602 off64_t* file_offset, std::string* realpath) {
Elliott Hughesca0c11b2013-03-12 10:40:45 -07001603 TRACE("[ opening %s ]", name);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001604
Elliott Hughes124fae92012-10-31 14:20:03 -07001605 // 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 -07001606 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001607 int fd = -1;
1608
Dmitriy Ivanov730ed9d2015-07-16 04:52:06 -07001609 if (strstr(name, kZipFileSeparator) != nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001610 fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
1611 }
1612
1613 if (fd == -1) {
1614 fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
Simon Baldwinaef71952015-01-16 13:22:54 +00001615 if (fd != -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001616 *file_offset = 0;
1617 if (!realpath_fd(fd, realpath)) {
1618 PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
1619 *realpath = name;
1620 }
Simon Baldwinaef71952015-01-16 13:22:54 +00001621 }
1622 }
1623
Dmitriy Ivanove44fffd2015-03-17 17:12:18 -07001624 return fd;
Elliott Hughes124fae92012-10-31 14:20:03 -07001625 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001626
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001627 // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path
1628 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 -07001629 if (fd == -1 && needed_by != nullptr) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001630 fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001631 // Check if the library is accessible
1632 if (fd != -1 && !ns->is_accessible(*realpath)) {
1633 fd = -1;
1634 }
Evgenii Stepanov68650822015-06-10 13:38:39 -07001635 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001636
Elliott Hughes124fae92012-10-31 14:20:03 -07001637 if (fd == -1) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001638 fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath);
Elliott Hughes124fae92012-10-31 14:20:03 -07001639 }
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07001640
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001641 // TODO(dimitry): workaround for http://b/26394120 (the grey-list)
1642 if (fd == -1 && ns != &g_default_namespace && is_greylisted(name, needed_by)) {
1643 // try searching for it on default_namespace default_library_path
1644 fd = open_library_on_paths(zip_archive_cache, name, file_offset,
1645 g_default_namespace.get_default_library_paths(), realpath);
1646 }
1647 // END OF WORKAROUND
1648
Elliott Hughes124fae92012-10-31 14:20:03 -07001649 return fd;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001650}
1651
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001652static const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
1653#if !defined(__LP64__)
1654 // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
Dmitriy Ivanov19133522015-06-02 17:36:54 -07001655 if (get_application_target_sdk_version() <= 22) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001656 const char* bname = basename(dt_needed);
1657 if (bname != dt_needed) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001658 DL_WARN("library \"%s\" has invalid DT_NEEDED entry \"%s\"", sopath, dt_needed);
1659 add_dlwarning(sopath, "invalid DT_NEEDED entry", dt_needed);
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001660 }
1661
1662 return bname;
1663 }
1664#endif
1665 return dt_needed;
1666}
1667
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001668template<typename F>
1669static void for_each_dt_needed(const soinfo* si, F action) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001670 for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001671 if (d->d_tag == DT_NEEDED) {
Dmitriy Ivanovd974e882015-05-27 18:29:41 -07001672 action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
Dima Zavin2e855792009-05-20 18:28:09 -07001673 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001674 }
1675}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001676
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001677template<typename F>
1678static void for_each_dt_needed(const ElfReader& elf_reader, F action) {
1679 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1680 if (d->d_tag == DT_NEEDED) {
1681 action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name()));
1682 }
1683 }
1684}
1685
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001686static bool load_library(android_namespace_t* ns,
1687 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001688 LoadTaskList* load_tasks,
1689 int rtld_flags,
1690 const std::string& realpath) {
1691 off64_t file_offset = task->get_file_offset();
1692 const char* name = task->get_name();
1693 const android_dlextinfo* extinfo = task->get_extinfo();
1694
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001695 if ((file_offset % PAGE_SIZE) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001696 DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001697 return false;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001698 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001699 if (file_offset < 0) {
1700 DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001701 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001702 }
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07001703
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001704 struct stat file_stat;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001705 if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) {
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07001706 DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001707 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001708 }
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001709 if (file_offset >= file_stat.st_size) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07001710 DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
1711 name, file_offset, file_stat.st_size);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001712 return false;
Yabin Cui16f7f8d2014-11-04 11:08:05 -08001713 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001714
1715 // Check for symlink and other situations where
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001716 // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
1717 if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001718 auto predicate = [&](soinfo* si) {
1719 return si->get_st_dev() != 0 &&
1720 si->get_st_ino() != 0 &&
1721 si->get_st_dev() == file_stat.st_dev &&
1722 si->get_st_ino() == file_stat.st_ino &&
1723 si->get_file_offset() == file_offset;
1724 };
1725
1726 soinfo* si = ns->soinfo_list().find_if(predicate);
1727
1728 // check public namespace
1729 if (si == nullptr) {
1730 si = g_public_namespace.find_if(predicate);
1731 if (si != nullptr) {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001732 ns->add_soinfo(si);
Dmitriy Ivanov9b821362015-04-02 16:03:56 -07001733 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001734 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001735
1736 if (si != nullptr) {
1737 TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
1738 "will return existing soinfo", name, si->get_realpath());
1739 task->set_soinfo(si);
1740 return true;
1741 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001742 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001743
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07001744 if ((rtld_flags & RTLD_NOLOAD) != 0) {
Dmitriy Ivanova6ac54a2014-09-09 10:21:42 -07001745 DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001746 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001747 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001748
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001749 if (!ns->is_accessible(realpath)) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001750 // TODO(dimitry): workaround for http://b/26394120 - the grey-list
1751 const soinfo* needed_by = task->is_dt_needed() ? task->get_needed_by() : nullptr;
1752 if (is_greylisted(name, needed_by)) {
1753 // print warning only if needed by non-system library
1754 if (needed_by == nullptr || !is_system_library(needed_by->get_realpath())) {
1755 const soinfo* needed_or_dlopened_by = task->get_needed_by();
1756 const char* sopath = needed_or_dlopened_by == nullptr ? "(unknown)" :
1757 needed_or_dlopened_by->get_realpath();
1758 DL_WARN("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the namespace \"%s\""
1759 " - the access is temporarily granted as a workaround for http://b/26394120, note that the access"
1760 " will be removed in future releases of Android.",
1761 name, realpath.c_str(), sopath, ns->get_name());
1762 add_dlwarning(sopath, "unauthorized access to", name);
1763 }
1764 } else {
1765 // do not load libraries if they are not accessible for the specified namespace.
1766 const char* needed_or_dlopened_by = task->get_needed_by() == nullptr ?
1767 "(unknown)" :
1768 task->get_needed_by()->get_realpath();
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001769
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001770 DL_ERR("library \"%s\" needed or dlopened by \"%s\" is not accessible for the namespace \"%s\"",
1771 name, needed_or_dlopened_by, ns->get_name());
Dimitry Ivanovd17a3772016-03-01 13:11:28 -08001772
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001773 PRINT("library \"%s\" (\"%s\") needed or dlopened by \"%s\" is not accessible for the"
1774 " namespace: [name=\"%s\", ld_library_paths=\"%s\", default_library_paths=\"%s\","
1775 " permitted_paths=\"%s\"]",
1776 name, realpath.c_str(),
1777 needed_or_dlopened_by,
1778 ns->get_name(),
1779 android::base::Join(ns->get_ld_library_paths(), ':').c_str(),
1780 android::base::Join(ns->get_default_library_paths(), ':').c_str(),
1781 android::base::Join(ns->get_permitted_paths(), ':').c_str());
1782 return false;
1783 }
Dimitry Ivanov22840aa2015-12-04 18:28:49 -08001784 }
1785
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001786 soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001787 if (si == nullptr) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001788 return false;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001789 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001790
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001791 task->set_soinfo(si);
1792
1793 // Read the ELF header and some of the segments.
1794 if (!task->read(realpath.c_str(), file_stat.st_size)) {
Dmitriy Ivanovfd7a91e2015-11-06 10:44:37 -08001795 soinfo_free(si);
1796 task->set_soinfo(nullptr);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001797 return false;
1798 }
1799
1800 // find and set DT_RUNPATH and dt_soname
1801 // Note that these field values are temporary and are
1802 // going to be overwritten on soinfo::prelink_image
1803 // with values from PT_LOAD segments.
1804 const ElfReader& elf_reader = task->get_elf_reader();
1805 for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) {
1806 if (d->d_tag == DT_RUNPATH) {
1807 si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val));
1808 }
1809 if (d->d_tag == DT_SONAME) {
1810 si->set_soname(elf_reader.get_string(d->d_un.d_val));
1811 }
1812 }
1813
1814 for_each_dt_needed(task->get_elf_reader(), [&](const char* name) {
1815 load_tasks->push_back(LoadTask::create(name, si, task->get_readers_map()));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001816 });
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07001817
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001818 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001819}
1820
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001821static bool load_library(android_namespace_t* ns,
1822 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001823 ZipArchiveCache* zip_archive_cache,
1824 LoadTaskList* load_tasks,
1825 int rtld_flags) {
1826 const char* name = task->get_name();
1827 soinfo* needed_by = task->get_needed_by();
1828 const android_dlextinfo* extinfo = task->get_extinfo();
1829
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001830 off64_t file_offset;
1831 std::string realpath;
Spencer Low0346ad72015-04-22 18:06:51 -07001832 if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001833 file_offset = 0;
Spencer Low0346ad72015-04-22 18:06:51 -07001834 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
1835 file_offset = extinfo->library_fd_offset;
1836 }
Dmitriy Ivanova1feb112015-10-01 18:41:57 -07001837
1838 if (!realpath_fd(extinfo->library_fd, &realpath)) {
1839 PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
1840 "Will use given name.", name);
1841 realpath = name;
1842 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001843
1844 task->set_fd(extinfo->library_fd, false);
1845 task->set_file_offset(file_offset);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001846 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001847 }
1848
1849 // Open the file.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001850 int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001851 if (fd == -1) {
1852 DL_ERR("library \"%s\" not found", name);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001853 return false;
Spencer Low0346ad72015-04-22 18:06:51 -07001854 }
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001855
1856 task->set_fd(fd, true);
1857 task->set_file_offset(file_offset);
1858
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001859 return load_library(ns, task, load_tasks, rtld_flags, realpath);
Spencer Low0346ad72015-04-22 18:06:51 -07001860}
1861
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001862// Returns true if library was found and false in 2 cases
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001863// 1. (for default namespace only) The library was found but loaded under different
1864// target_sdk_version (*candidate != nullptr)
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001865// 2. The library was not found by soname (*candidate is nullptr)
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001866static bool find_loaded_library_by_soname(android_namespace_t* ns,
1867 const char* name, soinfo** candidate) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001868 *candidate = nullptr;
1869
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001870 // Ignore filename with path.
1871 if (strchr(name, '/') != nullptr) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001872 return false;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001873 }
1874
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001875 uint32_t target_sdk_version = get_application_target_sdk_version();
1876
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001877 return !ns->soinfo_list().visit([&](soinfo* si) {
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07001878 const char* soname = si->get_soname();
1879 if (soname != nullptr && (strcmp(name, soname) == 0)) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001880 // If the library was opened under different target sdk version
1881 // skip this step and try to reopen it. The exceptions are
1882 // "libdl.so" and global group. There is no point in skipping
1883 // them because relocation process is going to use them
1884 // in any case.
1885 bool is_libdl = si == solist;
1886 if (is_libdl || (si->get_dt_flags_1() & DF_1_GLOBAL) != 0 ||
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001887 !si->is_linked() || si->get_target_sdk_version() == target_sdk_version ||
1888 ns != &g_default_namespace) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001889 *candidate = si;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001890 return false;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001891 } else if (*candidate == nullptr) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001892 // for the different sdk version in the default namespace
1893 // remember the first library.
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001894 *candidate = si;
1895 }
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001896 }
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001897
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001898 return true;
1899 });
Ard Biesheuvel12c78bb2012-08-14 12:30:09 +02001900}
1901
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001902static bool find_library_internal(android_namespace_t* ns,
1903 LoadTask* task,
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001904 ZipArchiveCache* zip_archive_cache,
1905 LoadTaskList* load_tasks,
1906 int rtld_flags) {
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001907 soinfo* candidate;
1908
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001909 if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001910 task->set_soinfo(candidate);
1911 return true;
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001912 }
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001913
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001914 if (ns != &g_default_namespace) {
1915 // check public namespace
1916 candidate = g_public_namespace.find_if([&](soinfo* si) {
1917 return strcmp(task->get_name(), si->get_soname()) == 0;
1918 });
1919
1920 if (candidate != nullptr) {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07001921 ns->add_soinfo(candidate);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001922 task->set_soinfo(candidate);
1923 return true;
1924 }
1925 }
1926
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07001927 // Library might still be loaded, the accurate detection
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001928 // of this fact is done by load_library.
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07001929 TRACE("[ \"%s\" find_loaded_library_by_soname failed (*candidate=%s@%p). Trying harder...]",
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001930 task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
Dmitriy Ivanova9703332015-06-16 15:38:21 -07001931
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001932 if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags)) {
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001933 return true;
1934 } else {
1935 // In case we were unable to load the library but there
1936 // is a candidate loaded under the same soname but different
1937 // sdk level - return it anyways.
1938 if (candidate != nullptr) {
1939 task->set_soinfo(candidate);
1940 return true;
1941 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07001942 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001943
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001944 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001945}
1946
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001947static void soinfo_unload(soinfo* si);
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07001948static void soinfo_unload(soinfo* soinfos[], size_t count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07001949
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001950// TODO: this is slightly unusual way to construct
1951// the global group for relocation. Not every RTLD_GLOBAL
1952// library is included in this group for backwards-compatibility
1953// reasons.
1954//
1955// This group consists of the main executable, LD_PRELOADs
1956// and libraries with the DF_1_GLOBAL flag set.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001957static soinfo::soinfo_list_t make_global_group(android_namespace_t* ns) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001958 soinfo::soinfo_list_t global_group;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001959 ns->soinfo_list().for_each([&](soinfo* si) {
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001960 if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
1961 global_group.push_back(si);
1962 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07001963 });
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07001964
1965 return global_group;
1966}
1967
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07001968// This function provides a list of libraries to be shared
1969// by the namespace. For the default namespace this is the global
1970// group (see make_global_group). For all others this is a group
1971// of RTLD_GLOBAL libraries (which includes the global group from
1972// the default namespace).
1973static soinfo::soinfo_list_t get_shared_group(android_namespace_t* ns) {
1974 if (ns == &g_default_namespace) {
1975 return make_global_group(ns);
1976 }
1977
1978 soinfo::soinfo_list_t shared_group;
1979 ns->soinfo_list().for_each([&](soinfo* si) {
1980 if ((si->get_rtld_flags() & RTLD_GLOBAL) != 0) {
1981 shared_group.push_back(si);
1982 }
1983 });
1984
1985 return shared_group;
1986}
1987
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07001988static void shuffle(std::vector<LoadTask*>* v) {
1989 for (size_t i = 0, size = v->size(); i < size; ++i) {
1990 size_t n = size - i;
1991 size_t r = arc4random_uniform(n);
1992 std::swap((*v)[n-1], (*v)[r]);
1993 }
1994}
1995
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07001996// add_as_children - add first-level loaded libraries (i.e. library_names[], but
1997// not their transitive dependencies) as children of the start_with library.
1998// This is false when find_libraries is called for dlopen(), when newly loaded
1999// libraries must form a disjoint tree.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002000static bool find_libraries(android_namespace_t* ns,
2001 soinfo* start_with,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002002 const char* const library_names[],
2003 size_t library_names_count, soinfo* soinfos[],
2004 std::vector<soinfo*>* ld_preloads,
2005 size_t ld_preloads_count, int rtld_flags,
2006 const android_dlextinfo* extinfo,
2007 bool add_as_children) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002008 // Step 0: prepare.
2009 LoadTaskList load_tasks;
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002010 std::unordered_map<const soinfo*, ElfReader> readers_map;
2011
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002012 for (size_t i = 0; i < library_names_count; ++i) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002013 const char* name = library_names[i];
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002014 load_tasks.push_back(LoadTask::create(name, start_with, &readers_map));
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002015 }
2016
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002017 // Construct global_group.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002018 soinfo::soinfo_list_t global_group = make_global_group(ns);
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07002019
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002020 // If soinfos array is null allocate one on stack.
2021 // The array is needed in case of failure; for example
2022 // when library_names[] = {libone.so, libtwo.so} and libone.so
2023 // is loaded correctly but libtwo.so failed for some reason.
2024 // In this case libone.so should be unloaded on return.
2025 // See also implementation of failure_guard below.
2026
2027 if (soinfos == nullptr) {
2028 size_t soinfos_size = sizeof(soinfo*)*library_names_count;
2029 soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
2030 memset(soinfos, 0, soinfos_size);
2031 }
2032
2033 // list of libraries to link - see step 2.
2034 size_t soinfos_count = 0;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002035
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002036 auto scope_guard = make_scope_guard([&]() {
2037 for (LoadTask* t : load_tasks) {
2038 LoadTask::deleter(t);
2039 }
2040 });
2041
Dmitriy Ivanovd9ff7222014-09-08 16:22:22 -07002042 auto failure_guard = make_scope_guard([&]() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002043 // Housekeeping
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002044 soinfo_unload(soinfos, soinfos_count);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002045 });
2046
Dmitriy Ivanovb4827502015-09-28 16:38:31 -07002047 ZipArchiveCache zip_archive_cache;
2048
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002049 // Step 1: expand the list of load_tasks to include
2050 // all DT_NEEDED libraries (do not load them just yet)
2051 for (size_t i = 0; i<load_tasks.size(); ++i) {
2052 LoadTask* task = load_tasks[i];
Evgenii Stepanov68650822015-06-10 13:38:39 -07002053 soinfo* needed_by = task->get_needed_by();
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002054
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07002055 bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002056 task->set_extinfo(is_dt_needed ? nullptr : extinfo);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002057 task->set_dt_needed(is_dt_needed);
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07002058
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002059 if(!find_library_internal(ns, task, &zip_archive_cache, &load_tasks, rtld_flags)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002060 return false;
2061 }
2062
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002063 soinfo* si = task->get_soinfo();
2064
Dmitriy Ivanovedfc9f62015-09-02 16:32:02 -07002065 if (is_dt_needed) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002066 needed_by->add_child(si);
2067 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002068
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002069 if (si->is_linked()) {
2070 si->increment_ref_count();
2071 }
2072
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002073 // When ld_preloads is not null, the first
2074 // ld_preloads_count libs are in fact ld_preloads.
2075 if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07002076 ld_preloads->push_back(si);
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002077 }
2078
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002079 if (soinfos_count < library_names_count) {
2080 soinfos[soinfos_count++] = si;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002081 }
2082 }
2083
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07002084 // Step 2: Load libraries in random order (see b/24047022)
2085 LoadTaskList load_list;
2086 for (auto&& task : load_tasks) {
2087 soinfo* si = task->get_soinfo();
2088 auto pred = [&](const LoadTask* t) {
2089 return t->get_soinfo() == si;
2090 };
2091
2092 if (!si->is_linked() &&
2093 std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) {
2094 load_list.push_back(task);
2095 }
2096 }
2097 shuffle(&load_list);
2098
2099 for (auto&& task : load_list) {
2100 if (!task->load()) {
2101 return false;
2102 }
2103 }
2104
2105 // Step 3: pre-link all DT_NEEDED libraries in breadth first order.
2106 for (auto&& task : load_tasks) {
2107 soinfo* si = task->get_soinfo();
2108 if (!si->is_linked() && !si->prelink_image()) {
2109 return false;
2110 }
2111 }
2112
2113 // Step 4: Add LD_PRELOADed libraries to the global group for
2114 // future runs. There is no need to explicitly add them to
2115 // the global group for this run because they are going to
2116 // appear in the local group in the correct order.
2117 if (ld_preloads != nullptr) {
2118 for (auto&& si : *ld_preloads) {
2119 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
2120 }
2121 }
2122
2123
2124 // Step 5: link libraries.
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002125 soinfo::soinfo_list_t local_group;
2126 walk_dependencies_tree(
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002127 (start_with != nullptr && add_as_children) ? &start_with : soinfos,
2128 (start_with != nullptr && add_as_children) ? 1 : soinfos_count,
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002129 [&] (soinfo* si) {
2130 local_group.push_back(si);
2131 return true;
2132 });
2133
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002134 // We need to increment ref_count in case
2135 // the root of the local group was not linked.
2136 bool was_local_group_root_linked = local_group.front()->is_linked();
2137
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002138 bool linked = local_group.visit([&](soinfo* si) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002139 if (!si->is_linked()) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002140 if (!si->link_image(global_group, local_group, extinfo)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002141 return false;
2142 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002143 }
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002144
2145 return true;
2146 });
2147
2148 if (linked) {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002149 local_group.for_each([](soinfo* si) {
2150 if (!si->is_linked()) {
2151 si->set_linked();
2152 }
2153 });
2154
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002155 failure_guard.disable();
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002156 }
2157
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002158 if (!was_local_group_root_linked) {
2159 local_group.front()->increment_ref_count();
2160 }
2161
Dmitriy Ivanovcfa97f12014-10-21 09:23:18 -07002162 return linked;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002163}
2164
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002165static soinfo* find_library(android_namespace_t* ns,
2166 const char* name, int rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002167 const android_dlextinfo* extinfo,
2168 soinfo* needed_by) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002169 soinfo* si;
2170
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002171 if (name == nullptr) {
2172 si = somain;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002173 } else if (!find_libraries(ns, needed_by, &name, 1, &si, nullptr, 0, rtld_flags,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07002174 extinfo, /* add_as_children */ false)) {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002175 return nullptr;
2176 }
2177
Elliott Hughesd23736e2012-11-01 15:16:56 -07002178 return si;
2179}
Elliott Hughesbedfe382012-08-14 14:07:59 -07002180
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002181static void soinfo_unload(soinfo* root) {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002182 if (root->is_linked()) {
2183 root = root->get_local_group_root();
2184 }
2185
2186 if (!root->can_unload()) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002187 TRACE("not unloading \"%s\" - the binary is flagged with NODELETE", root->get_realpath());
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002188 return;
2189 }
2190
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002191 soinfo_unload(&root, 1);
2192}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002193
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002194static void soinfo_unload(soinfo* soinfos[], size_t count) {
2195 // Note that the library can be loaded but not linked;
2196 // in which case there is no root but we still need
2197 // to walk the tree and unload soinfos involved.
2198 //
2199 // This happens on unsuccessful dlopen, when one of
2200 // the DT_NEEDED libraries could not be linked/found.
2201 if (count == 0) {
2202 return;
2203 }
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002204
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002205 soinfo::soinfo_list_t unload_list;
2206 for (size_t i = 0; i < count; ++i) {
2207 soinfo* si = soinfos[i];
Dmitriy Ivanov5ae82cb2014-12-02 17:08:42 -08002208
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002209 if (si->can_unload()) {
2210 size_t ref_count = si->is_linked() ? si->decrement_ref_count() : 0;
2211 if (ref_count == 0) {
2212 unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002213 } else {
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002214 TRACE("not unloading '%s' group, decrementing ref_count to %zd",
2215 si->get_realpath(), ref_count);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002216 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002217 } else {
2218 TRACE("not unloading '%s' - the binary is flagged with NODELETE", si->get_realpath());
2219 return;
2220 }
2221 }
2222
2223 // This is used to identify soinfos outside of the load-group
2224 // note that we cannot have > 1 in the array and have any of them
2225 // linked. This is why we can safely use the first one.
2226 soinfo* root = soinfos[0];
2227
2228 soinfo::soinfo_list_t local_unload_list;
2229 soinfo::soinfo_list_t external_unload_list;
2230 soinfo* si = nullptr;
2231
2232 while ((si = unload_list.pop_front()) != nullptr) {
2233 if (local_unload_list.contains(si)) {
2234 continue;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002235 }
Elliott Hughesd23736e2012-11-01 15:16:56 -07002236
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002237 local_unload_list.push_back(si);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002238
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002239 if (si->has_min_version(0)) {
2240 soinfo* child = nullptr;
2241 while ((child = si->get_children().pop_front()) != nullptr) {
2242 TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
2243 child->get_realpath(), child);
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002244
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002245 if (local_unload_list.contains(child)) {
2246 continue;
2247 } else if (child->is_linked() && child->get_local_group_root() != root) {
2248 external_unload_list.push_back(child);
2249 } else {
2250 unload_list.push_front(child);
2251 }
2252 }
2253 } else {
2254#if !defined(__work_around_b_24465209__)
2255 __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
2256#else
2257 PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
2258 for_each_dt_needed(si, [&] (const char* library_name) {
2259 TRACE("deprecated (old format of soinfo): %s needs to unload %s",
2260 si->get_realpath(), library_name);
2261
2262 soinfo* needed = find_library(si->get_primary_namespace(),
2263 library_name, RTLD_NOLOAD, nullptr, nullptr);
2264
2265 if (needed != nullptr) {
2266 // Not found: for example if symlink was deleted between dlopen and dlclose
2267 // Since we cannot really handle errors at this point - print and continue.
2268 PRINT("warning: couldn't find %s needed by %s on unload.",
2269 library_name, si->get_realpath());
2270 return;
2271 } else if (local_unload_list.contains(needed)) {
2272 // already visited
2273 return;
2274 } else if (needed->is_linked() && needed->get_local_group_root() != root) {
2275 // external group
2276 external_unload_list.push_back(needed);
2277 } else {
2278 // local group
2279 unload_list.push_front(needed);
2280 }
2281 });
2282#endif
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08002283 }
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07002284 }
2285
2286 local_unload_list.for_each([](soinfo* si) {
2287 si->call_destructors();
2288 });
2289
2290 while ((si = local_unload_list.pop_front()) != nullptr) {
2291 notify_gdb_of_unload(si);
2292 soinfo_free(si);
2293 }
2294
2295 while ((si = external_unload_list.pop_front()) != nullptr) {
2296 soinfo_unload(si);
Dmitriy Ivanova2547052014-11-18 12:03:09 -08002297 }
2298}
2299
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002300static std::string symbol_display_name(const char* sym_name, const char* sym_ver) {
2301 if (sym_ver == nullptr) {
2302 return sym_name;
2303 }
2304
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002305 return std::string(sym_name) + ", version " + sym_ver;
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002306}
2307
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002308static android_namespace_t* get_caller_namespace(soinfo* caller) {
2309 return caller != nullptr ? caller->get_primary_namespace() : g_anonymous_namespace;
2310}
2311
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002312void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002313 // Use basic string manipulation calls to avoid snprintf.
2314 // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
2315 // When debug malloc is enabled, this call returns 0. This in turn causes
2316 // snprintf to do nothing, which causes libraries to fail to load.
2317 // See b/17302493 for further details.
2318 // Once the above bug is fixed, this code can be modified to use
2319 // snprintf again.
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002320 size_t required_len = 0;
2321 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2322 required_len += strlen(g_default_ld_paths[i]) + 1;
2323 }
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002324 if (buffer_size < required_len) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002325 __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
2326 "buffer len %zu, required len %zu", buffer_size, required_len);
Christopher Ferris052fa3a2014-08-26 20:48:11 -07002327 }
Evgenii Stepanovd640b222015-07-10 17:54:01 -07002328 char* end = buffer;
2329 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
2330 if (i > 0) *end++ = ':';
2331 end = stpcpy(end, g_default_ld_paths[i]);
2332 }
Elliott Hughesa4aafd12014-01-13 16:37:47 -08002333}
2334
Elliott Hughescade4c32012-12-20 14:42:14 -08002335void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
Nick Kralevich6bb01b62015-03-07 13:37:05 -08002336 parse_LD_LIBRARY_PATH(ld_library_path);
Elliott Hughescade4c32012-12-20 14:42:14 -08002337}
2338
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002339static std::string android_dlextinfo_to_string(const android_dlextinfo* info) {
2340 if (info == nullptr) {
2341 return "(null)";
2342 }
2343
2344 return android::base::StringPrintf("[flags=0x%" PRIx64 ","
2345 " reserved_addr=%p,"
2346 " reserved_size=0x%zx,"
2347 " relro_fd=%d,"
2348 " library_fd=%d,"
2349 " library_fd_offset=0x%" PRIx64 ","
2350 " library_namespace=%s@%p]",
2351 info->flags,
2352 info->reserved_addr,
2353 info->reserved_size,
2354 info->relro_fd,
2355 info->library_fd,
2356 info->library_fd_offset,
2357 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
2358 (info->library_namespace != nullptr ?
2359 info->library_namespace->get_name() : "(null)") : "(n/a)",
2360 (info->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0 ?
2361 info->library_namespace : nullptr);
2362}
2363
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002364void* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo,
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002365 void* caller_addr) {
2366 soinfo* const caller = find_containing_library(caller_addr);
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002367 android_namespace_t* ns = get_caller_namespace(caller);
2368
2369 LD_LOG(kLogDlopen,
2370 "dlopen(name=\"%s\", flags=0x%x, extinfo=%s, caller=\"%s\", caller_ns=%s@%p) ...",
2371 name,
2372 flags,
2373 android_dlextinfo_to_string(extinfo).c_str(),
2374 caller == nullptr ? "(null)" : caller->get_realpath(),
2375 ns == nullptr ? "(null)" : ns->get_name(),
2376 ns);
2377
2378 auto failure_guard = make_scope_guard([&]() {
2379 LD_LOG(kLogDlopen, "... dlopen failed: %s", linker_get_error_buffer());
2380 });
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002381
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07002382 if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
Elliott Hughese66190d2012-12-18 15:57:55 -08002383 DL_ERR("invalid flags to dlopen: %x", flags);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002384 return nullptr;
Elliott Hughese66190d2012-12-18 15:57:55 -08002385 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002386
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002387 if (extinfo != nullptr) {
2388 if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
2389 DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
2390 return nullptr;
2391 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002392
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002393 if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
Dmitriy Ivanova6c12792014-10-21 12:09:18 -07002394 (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002395 DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
2396 "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07002397 return nullptr;
2398 }
Dmitriy Ivanov126af752015-10-07 16:34:20 -07002399
2400 if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 &&
2401 (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) {
2402 DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not "
2403 "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT");
2404 return nullptr;
2405 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002406
2407 if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) {
2408 if (extinfo->library_namespace == nullptr) {
2409 DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null");
2410 return nullptr;
2411 }
2412 ns = extinfo->library_namespace;
2413 }
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00002414 }
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002415
2416 ProtectedDataGuard guard;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002417 soinfo* si = find_library(ns, name, flags, extinfo, caller);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002418 if (si != nullptr) {
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002419 failure_guard.disable();
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002420 si->call_constructors();
Dimitry Ivanovb996d602016-07-11 18:11:39 -07002421 void* handle = si->to_handle();
2422 LD_LOG(kLogDlopen,
2423 "... dlopen successful: realpath=\"%s\", soname=\"%s\", handle=%p",
2424 si->get_realpath(), si->get_soname(), handle);
2425 return handle;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002426 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002427
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002428 return nullptr;
Elliott Hughesd23736e2012-11-01 15:16:56 -07002429}
2430
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002431int do_dladdr(const void* addr, Dl_info* info) {
2432 // Determine if this address can be found in any library currently mapped.
2433 soinfo* si = find_containing_library(addr);
2434 if (si == nullptr) {
2435 return 0;
2436 }
2437
2438 memset(info, 0, sizeof(Dl_info));
2439
2440 info->dli_fname = si->get_realpath();
2441 // Address at which the shared object is loaded.
2442 info->dli_fbase = reinterpret_cast<void*>(si->base);
2443
2444 // Determine if any symbol in the library contains the specified address.
2445 ElfW(Sym)* sym = si->find_symbol_by_address(addr);
2446 if (sym != nullptr) {
2447 info->dli_sname = si->get_string(sym->st_name);
2448 info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym));
2449 }
2450
2451 return 1;
2452}
2453
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002454static soinfo* soinfo_from_handle(void* handle) {
2455 if ((reinterpret_cast<uintptr_t>(handle) & 1) != 0) {
2456 auto it = g_soinfo_handles_map.find(reinterpret_cast<uintptr_t>(handle));
2457 if (it == g_soinfo_handles_map.end()) {
2458 return nullptr;
2459 } else {
2460 return it->second;
2461 }
2462 }
2463
2464 return static_cast<soinfo*>(handle);
2465}
2466
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002467bool do_dlsym(void* handle, const char* sym_name, const char* sym_ver,
2468 void* caller_addr, void** symbol) {
2469#if !defined(__LP64__)
2470 if (handle == nullptr) {
2471 DL_ERR("dlsym failed: library handle is null");
2472 return false;
2473 }
2474#endif
2475
2476 if (sym_name == nullptr) {
2477 DL_ERR("dlsym failed: symbol name is null");
2478 return false;
2479 }
2480
2481 soinfo* found = nullptr;
2482 const ElfW(Sym)* sym = nullptr;
2483 soinfo* caller = find_containing_library(caller_addr);
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07002484 android_namespace_t* ns = get_caller_namespace(caller);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002485
2486 version_info vi_instance;
2487 version_info* vi = nullptr;
2488
2489 if (sym_ver != nullptr) {
Dimitry Ivanov9cf99cb2015-12-11 14:22:24 -08002490 vi_instance.name = sym_ver;
2491 vi_instance.elf_hash = calculate_elf_hash(sym_ver);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002492 vi = &vi_instance;
2493 }
2494
2495 if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) {
2496 sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle);
2497 } else {
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002498 soinfo* si = soinfo_from_handle(handle);
2499 if (si == nullptr) {
2500 DL_ERR("dlsym failed: invalid handle: %p", handle);
2501 return false;
2502 }
2503 sym = dlsym_handle_lookup(si, &found, sym_name, vi);
Dimitry Ivanov4a2c5aa2015-12-10 16:08:14 -08002504 }
2505
2506 if (sym != nullptr) {
2507 uint32_t bind = ELF_ST_BIND(sym->st_info);
2508
2509 if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) {
2510 *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym));
2511 return true;
2512 }
2513
2514 DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str());
2515 return false;
2516 }
2517
2518 DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str());
2519 return false;
2520}
2521
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002522int do_dlclose(void* handle) {
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08002523 ProtectedDataGuard guard;
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002524 soinfo* si = soinfo_from_handle(handle);
2525 if (si == nullptr) {
2526 DL_ERR("invalid handle: %p", handle);
2527 return -1;
2528 }
2529
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -07002530 soinfo_unload(si);
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07002531 return 0;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08002532}
2533
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002534bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002535 if (g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002536 DL_ERR("public namespace has already been initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002537 return false;
2538 }
2539
Dimitry Ivanov54807612016-04-21 14:57:38 -07002540 if (public_ns_sonames == nullptr || public_ns_sonames[0] == '\0') {
2541 DL_ERR("error initializing public namespace: the list of public libraries is empty.");
2542 return false;
2543 }
2544
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002545 std::vector<std::string> sonames = android::base::Split(public_ns_sonames, ":");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002546
2547 ProtectedDataGuard guard;
2548
2549 auto failure_guard = make_scope_guard([&]() {
2550 g_public_namespace.clear();
2551 });
2552
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002553 for (const auto& soname : sonames) {
Dmitriy Ivanov3cc35e22015-11-17 18:36:50 -08002554 soinfo* candidate = nullptr;
2555
2556 find_loaded_library_by_soname(&g_default_namespace, soname.c_str(), &candidate);
2557
2558 if (candidate == nullptr) {
Christopher Ferris523e2a92016-06-17 13:46:36 -07002559 DL_ERR("error initializing public namespace: a library with soname \"%s\""
2560 " was not found in the default namespace", soname.c_str());
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002561 return false;
2562 }
2563
2564 candidate->set_nodelete();
2565 g_public_namespace.push_back(candidate);
2566 }
2567
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002568 g_public_namespace_initialized = true;
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002569
2570 // create anonymous namespace
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002571 // When the caller is nullptr - create_namespace will take global group
2572 // from the anonymous namespace, which is fine because anonymous namespace
2573 // is still pointing to the default one.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002574 android_namespace_t* anon_ns =
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002575 create_namespace(nullptr, "(anonymous)", nullptr, anon_ns_library_path,
Dimitry Ivanov52408632016-05-23 10:31:11 -07002576 ANDROID_NAMESPACE_TYPE_REGULAR, nullptr, &g_default_namespace);
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002577
2578 if (anon_ns == nullptr) {
2579 g_public_namespace_initialized = false;
2580 return false;
2581 }
2582 g_anonymous_namespace = anon_ns;
2583 failure_guard.disable();
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002584 return true;
2585}
2586
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002587android_namespace_t* create_namespace(const void* caller_addr,
2588 const char* name,
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002589 const char* ld_library_path,
2590 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002591 uint64_t type,
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002592 const char* permitted_when_isolated_path,
2593 android_namespace_t* parent_namespace) {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002594 if (!g_public_namespace_initialized) {
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -08002595 DL_ERR("cannot create namespace: public namespace is not initialized.");
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002596 return nullptr;
2597 }
2598
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002599 if (parent_namespace == nullptr) {
Dimitry Ivanov52408632016-05-23 10:31:11 -07002600 // if parent_namespace is nullptr -> set it to the caller namespace
2601 soinfo* caller_soinfo = find_containing_library(caller_addr);
2602
2603 parent_namespace = caller_soinfo != nullptr ?
2604 caller_soinfo->get_primary_namespace() :
2605 g_anonymous_namespace;
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002606 }
2607
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002608 ProtectedDataGuard guard;
2609 std::vector<std::string> ld_library_paths;
2610 std::vector<std::string> default_library_paths;
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002611 std::vector<std::string> permitted_paths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002612
2613 parse_path(ld_library_path, ":", &ld_library_paths);
2614 parse_path(default_library_path, ":", &default_library_paths);
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002615 parse_path(permitted_when_isolated_path, ":", &permitted_paths);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002616
2617 android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t();
2618 ns->set_name(name);
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002619 ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002620 ns->set_ld_library_paths(std::move(ld_library_paths));
2621 ns->set_default_library_paths(std::move(default_library_paths));
Dimitry Ivanov284ae352015-12-08 10:47:13 -08002622 ns->set_permitted_paths(std::move(permitted_paths));
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002623
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002624 if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002625 // If shared - clone the parent namespace
2626 ns->add_soinfos(parent_namespace->soinfo_list());
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002627 } else {
Dimitry Ivanovfc2da532016-05-12 15:20:21 -07002628 // If not shared - copy only the shared group
2629 ns->add_soinfos(get_shared_group(parent_namespace));
Dimitry Ivanov7331fe12015-12-14 14:11:17 -08002630 }
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07002631
2632 return ns;
2633}
2634
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002635static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
2636 typedef ElfW(Addr) (*ifunc_resolver_t)(void);
2637 ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
2638 ElfW(Addr) ifunc_addr = ifunc_resolver();
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002639 TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
2640 ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002641
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002642 return ifunc_addr;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002643}
Brigid Smithc5a13ef2014-07-23 11:22:25 -07002644
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002645const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
2646 if (source_symver < 2 ||
2647 source_symver >= version_infos.size() ||
2648 version_infos[source_symver].name == nullptr) {
2649 return nullptr;
2650 }
2651
2652 return &version_infos[source_symver];
2653}
2654
2655void VersionTracker::add_version_info(size_t source_index,
2656 ElfW(Word) elf_hash,
2657 const char* ver_name,
2658 const soinfo* target_si) {
2659 if (source_index >= version_infos.size()) {
2660 version_infos.resize(source_index+1);
2661 }
2662
2663 version_infos[source_index].elf_hash = elf_hash;
2664 version_infos[source_index].name = ver_name;
2665 version_infos[source_index].target_si = target_si;
2666}
2667
2668bool VersionTracker::init_verneed(const soinfo* si_from) {
2669 uintptr_t verneed_ptr = si_from->get_verneed_ptr();
2670
2671 if (verneed_ptr == 0) {
2672 return true;
2673 }
2674
2675 size_t verneed_cnt = si_from->get_verneed_cnt();
2676
2677 for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
2678 const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
2679 size_t vernaux_offset = offset + verneed->vn_aux;
2680 offset += verneed->vn_next;
2681
2682 if (verneed->vn_version != 1) {
2683 DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
2684 return false;
2685 }
2686
2687 const char* target_soname = si_from->get_string(verneed->vn_file);
2688 // find it in dependencies
2689 soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
Dmitriy Ivanov406d9962015-05-06 11:05:27 -07002690 return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002691 });
2692
2693 if (target_si == nullptr) {
2694 DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002695 target_soname, i, si_from->get_realpath());
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002696 return false;
2697 }
2698
2699 for (size_t j = 0; j<verneed->vn_cnt; ++j) {
2700 const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
2701 vernaux_offset += vernaux->vna_next;
2702
2703 const ElfW(Word) elf_hash = vernaux->vna_hash;
2704 const char* ver_name = si_from->get_string(vernaux->vna_name);
2705 ElfW(Half) source_index = vernaux->vna_other;
2706
2707 add_version_info(source_index, elf_hash, ver_name, target_si);
2708 }
2709 }
2710
2711 return true;
2712}
2713
2714bool VersionTracker::init_verdef(const soinfo* si_from) {
2715 return for_each_verdef(si_from,
2716 [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
2717 add_version_info(verdef->vd_ndx, verdef->vd_hash,
2718 si_from->get_string(verdaux->vda_name), si_from);
2719 return false;
2720 }
2721 );
2722}
2723
2724bool VersionTracker::init(const soinfo* si_from) {
2725 if (!si_from->has_min_version(2)) {
2726 return true;
2727 }
2728
2729 return init_verneed(si_from) && init_verdef(si_from);
2730}
2731
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002732bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
2733 const char* sym_name, const version_info** vi) {
2734 const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
2735 ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
2736
2737 if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
2738 *vi = version_tracker.get_version_info(sym_ver);
2739
2740 if (*vi == nullptr) {
2741 DL_ERR("cannot find verneed/verdef for version index=%d "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002742 "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002743 return false;
2744 }
2745 } else {
2746 // there is no version info
2747 *vi = nullptr;
2748 }
2749
2750 return true;
2751}
2752
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002753#if !defined(__mips__)
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07002754#if defined(USE_RELA)
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002755static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
2756 return rela->r_addend;
2757}
2758#else
2759static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002760 if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
2761 ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002762 return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
2763 }
2764 return 0;
2765}
2766#endif
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002767
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002768template<typename ElfRelIteratorT>
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07002769bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
2770 const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
Dmitriy Ivanovfa26eee2015-02-03 16:06:47 -08002771 for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
2772 const auto rel = rel_iterator.next();
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002773 if (rel == nullptr) {
2774 return false;
2775 }
2776
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002777 ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
2778 ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
2779
2780 ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002781 ElfW(Addr) sym_addr = 0;
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002782 const char* sym_name = nullptr;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002783 ElfW(Addr) addend = get_addend(rel, reloc);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002784
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07002785 DEBUG("Processing \"%s\" relocation at index %zd", get_realpath(), idx);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002786 if (type == R_GENERIC_NONE) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002787 continue;
2788 }
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002789
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002790 const ElfW(Sym)* s = nullptr;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07002791 soinfo* lsi = nullptr;
2792
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002793 if (sym != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002794 sym_name = get_string(symtab_[sym].st_name);
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002795 const version_info* vi = nullptr;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002796
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002797 if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
2798 return false;
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07002799 }
Dmitriy Ivanov31b408d2015-04-30 16:11:48 -07002800
2801 if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
2802 return false;
2803 }
2804
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07002805 if (s == nullptr) {
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002806 // We only allow an undefined symbol if this is a weak reference...
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08002807 s = &symtab_[sym];
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002808 if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07002809 DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002810 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002811 }
2812
2813 /* IHI0044C AAELF 4.5.1.1:
2814
2815 Libraries are not searched to resolve weak references.
2816 It is not an error for a weak reference to remain unsatisfied.
2817
2818 During linking, the value of an undefined weak reference is:
2819 - Zero if the relocation type is absolute
2820 - The address of the place if the relocation is pc-relative
2821 - The address of nominal base address if the relocation
2822 type is base-relative.
2823 */
2824
2825 switch (type) {
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002826 case R_GENERIC_JUMP_SLOT:
2827 case R_GENERIC_GLOB_DAT:
2828 case R_GENERIC_RELATIVE:
2829 case R_GENERIC_IRELATIVE:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002830#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002831 case R_AARCH64_ABS64:
2832 case R_AARCH64_ABS32:
2833 case R_AARCH64_ABS16:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002834#elif defined(__x86_64__)
2835 case R_X86_64_32:
2836 case R_X86_64_64:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002837#elif defined(__arm__)
2838 case R_ARM_ABS32:
2839#elif defined(__i386__)
2840 case R_386_32:
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002841#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002842 /*
2843 * The sym_addr was initialized to be zero above, or the relocation
2844 * code below does not care about value of sym_addr.
2845 * No need to do anything.
2846 */
2847 break;
Dmitriy Ivanov1b694692015-01-13 12:17:31 -08002848#if defined(__x86_64__)
Dimitry Ivanovd338aac2015-01-13 22:31:54 +00002849 case R_X86_64_PC32:
2850 sym_addr = reloc;
2851 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002852#elif defined(__i386__)
2853 case R_386_PC32:
2854 sym_addr = reloc;
2855 break;
2856#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002857 default:
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002858 DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08002859 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002860 }
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002861 } else { // We got a definition.
2862#if !defined(__LP64__)
2863 // When relocating dso with text_relocation .text segment is
2864 // not executable. We need to restore elf flags before resolving
2865 // STT_GNU_IFUNC symbol.
2866 bool protect_segments = has_text_relocations &&
2867 lsi == this &&
2868 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
2869 if (protect_segments) {
2870 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2871 DL_ERR("can't protect segments for \"%s\": %s",
2872 get_realpath(), strerror(errno));
2873 return false;
2874 }
2875 }
2876#endif
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07002877 sym_addr = lsi->resolve_symbol_address(s);
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002878#if !defined(__LP64__)
2879 if (protect_segments) {
2880 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2881 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2882 get_realpath(), strerror(errno));
2883 return false;
2884 }
2885 }
2886#endif
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002887 }
2888 count_relocation(kRelocSymbol);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07002889 }
2890
2891 switch (type) {
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002892 case R_GENERIC_JUMP_SLOT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002893 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002894 MARK(rel->r_offset);
2895 TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
2896 reinterpret_cast<void*>(reloc),
2897 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2898
2899 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002900 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002901 case R_GENERIC_GLOB_DAT:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002902 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002903 MARK(rel->r_offset);
2904 TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
2905 reinterpret_cast<void*>(reloc),
2906 reinterpret_cast<void*>(sym_addr + addend), sym_name);
2907 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002908 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002909 case R_GENERIC_RELATIVE:
2910 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002911 MARK(rel->r_offset);
2912 TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
2913 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002914 reinterpret_cast<void*>(load_bias + addend));
2915 *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002916 break;
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002917 case R_GENERIC_IRELATIVE:
2918 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002919 MARK(rel->r_offset);
2920 TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
2921 reinterpret_cast<void*>(reloc),
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08002922 reinterpret_cast<void*>(load_bias + addend));
Dmitriy Ivanovec83a612015-07-26 07:37:09 -07002923 {
2924#if !defined(__LP64__)
2925 // When relocating dso with text_relocation .text segment is
2926 // not executable. We need to restore elf flags for this
2927 // particular call.
2928 if (has_text_relocations) {
2929 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
2930 DL_ERR("can't protect segments for \"%s\": %s",
2931 get_realpath(), strerror(errno));
2932 return false;
2933 }
2934 }
2935#endif
2936 ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
2937#if !defined(__LP64__)
2938 // Unprotect it afterwards...
2939 if (has_text_relocations) {
2940 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
2941 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
2942 get_realpath(), strerror(errno));
2943 return false;
2944 }
2945 }
2946#endif
2947 *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
2948 }
Dmitriy Ivanovcefef7d2015-01-08 23:30:15 -08002949 break;
2950
2951#if defined(__aarch64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002952 case R_AARCH64_ABS64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002953 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002954 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002955 TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002956 reloc, sym_addr + addend, sym_name);
2957 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002958 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002959 case R_AARCH64_ABS32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002960 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002961 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002962 TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002963 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002964 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002965 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
2966 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002967 if ((min_value <= (sym_addr + addend)) &&
2968 ((sym_addr + addend) <= max_value)) {
2969 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002970 } else {
2971 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002972 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002973 return false;
2974 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002975 }
2976 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002977 case R_AARCH64_ABS16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002978 count_relocation(kRelocAbsolute);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002979 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002980 TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002981 reloc, sym_addr + addend, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002982 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002983 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
2984 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002985 if ((min_value <= (sym_addr + addend)) &&
2986 ((sym_addr + addend) <= max_value)) {
2987 *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002988 } else {
2989 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002990 sym_addr + addend, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07002991 return false;
2992 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002993 }
2994 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07002995 case R_AARCH64_PREL64:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01002996 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08002997 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08002998 TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07002999 reloc, sym_addr + addend, rel->r_offset, sym_name);
3000 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003001 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003002 case R_AARCH64_PREL32:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003003 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003004 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003005 TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003006 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003007 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003008 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
3009 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003010 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
3011 ((sym_addr + addend - rel->r_offset) <= max_value)) {
3012 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003013 } else {
3014 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003015 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003016 return false;
3017 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003018 }
3019 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003020 case R_AARCH64_PREL16:
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003021 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003022 MARK(rel->r_offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08003023 TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003024 reloc, sym_addr + addend, rel->r_offset, sym_name);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003025 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003026 const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
3027 const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003028 if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
3029 ((sym_addr + addend - rel->r_offset) <= max_value)) {
3030 *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003031 } else {
3032 DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
Dmitriy Ivanov77f91c62015-10-15 13:26:03 -07003033 sym_addr + addend - rel->r_offset, min_value, max_value);
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003034 return false;
3035 }
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003036 }
3037 break;
3038
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003039 case R_AARCH64_COPY:
Nick Kralevich76e289c2014-07-03 12:04:31 -07003040 /*
3041 * ET_EXEC is not supported so this should not happen.
3042 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003043 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
Nick Kralevich76e289c2014-07-03 12:04:31 -07003044 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003045 * Section 4.6.11 "Dynamic relocations"
Nick Kralevich76e289c2014-07-03 12:04:31 -07003046 * R_AARCH64_COPY may only appear in executable objects where e_type is
3047 * set to ET_EXEC.
3048 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003049 DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08003050 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003051 case R_AARCH64_TLS_TPREL64:
Elliott Hughes0266ae52014-02-10 17:46:57 -08003052 TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003053 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003054 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003055 case R_AARCH64_TLS_DTPREL32:
Elliott Hughes0266ae52014-02-10 17:46:57 -08003056 TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003057 reloc, (sym_addr + addend), rel->r_offset);
Marcus Oaklande365f9d2013-10-10 15:19:31 +01003058 break;
3059#elif defined(__x86_64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003060 case R_X86_64_32:
3061 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003062 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003063 TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
3064 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09003065 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003066 break;
3067 case R_X86_64_64:
3068 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003069 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003070 TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
3071 static_cast<size_t>(sym_addr), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09003072 *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003073 break;
3074 case R_X86_64_PC32:
3075 count_relocation(kRelocRelative);
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003076 MARK(rel->r_offset);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003077 TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
3078 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
3079 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
Junichi Uekawaff35b1e2015-11-18 10:18:59 +09003080 *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003081 break;
Dmitriy Ivanovbcc04d02015-01-13 12:12:38 -08003082#elif defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003083 case R_ARM_ABS32:
3084 count_relocation(kRelocAbsolute);
3085 MARK(rel->r_offset);
3086 TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
3087 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
3088 break;
3089 case R_ARM_REL32:
3090 count_relocation(kRelocRelative);
3091 MARK(rel->r_offset);
3092 TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
3093 reloc, sym_addr, rel->r_offset, sym_name);
3094 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
3095 break;
3096 case R_ARM_COPY:
3097 /*
3098 * ET_EXEC is not supported so this should not happen.
3099 *
3100 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
3101 *
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003102 * Section 4.6.1.10 "Dynamic relocations"
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003103 * R_ARM_COPY may only appear in executable objects where e_type is
3104 * set to ET_EXEC.
3105 */
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003106 DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08003107 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003108#elif defined(__i386__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003109 case R_386_32:
3110 count_relocation(kRelocRelative);
3111 MARK(rel->r_offset);
3112 TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
3113 *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
3114 break;
3115 case R_386_PC32:
3116 count_relocation(kRelocRelative);
3117 MARK(rel->r_offset);
3118 TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
3119 reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
3120 *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
3121 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003122#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003123 default:
3124 DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003125 return false;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003126 }
3127 }
3128 return true;
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003129}
Dmitriy Ivanov114ff692015-01-14 11:36:38 -08003130#endif // !defined(__mips__)
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07003131
Dimitry Ivanov55437462016-07-20 15:33:07 -07003132static void call_function(const char* function_name __unused,
3133 linker_ctor_function_t function,
3134 const char* realpath __unused) {
3135 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
3136 return;
3137 }
3138
3139 TRACE("[ Calling c-tor %s @ %p for '%s' ]", function_name, function, realpath);
3140 function(g_argc, g_argv, g_envp);
3141 TRACE("[ Done calling c-tor %s @ %p for '%s' ]", function_name, function, realpath);
3142}
3143
3144static void call_function(const char* function_name __unused,
3145 linker_dtor_function_t function,
3146 const char* realpath __unused) {
3147 if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
3148 return;
3149 }
3150
3151 TRACE("[ Calling d-tor %s @ %p for '%s' ]", function_name, function, realpath);
3152 function();
3153 TRACE("[ Done calling d-tor %s @ %p for '%s' ]", function_name, function, realpath);
3154}
3155
3156template <typename F>
3157static void call_array(const char* array_name __unused,
3158 F* functions,
3159 size_t count,
3160 bool reverse,
3161 const char* realpath) {
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07003162 if (functions == nullptr) {
Elliott Hughesd23736e2012-11-01 15:16:56 -07003163 return;
3164 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02003165
Dimitry Ivanov55437462016-07-20 15:33:07 -07003166 TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, realpath);
Elliott Hughesca0c11b2013-03-12 10:40:45 -07003167
3168 int begin = reverse ? (count - 1) : 0;
3169 int end = reverse ? -1 : count;
3170 int step = reverse ? -1 : 1;
3171
3172 for (int i = begin; i != end; i += step) {
3173 TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
Dimitry Ivanov55437462016-07-20 15:33:07 -07003174 call_function("function", functions[i], realpath);
Elliott Hughesd23736e2012-11-01 15:16:56 -07003175 }
David 'Digit' Turner82156792009-05-18 14:37:41 +02003176
Dimitry Ivanov55437462016-07-20 15:33:07 -07003177 TRACE("[ Done calling %s for '%s' ]", array_name, realpath);
Evgeniy Stepanov9181a5d2012-08-13 17:58:37 +04003178}
3179
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003180void soinfo::call_pre_init_constructors() {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003181 // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
3182 // but ignored in a shared library.
Dimitry Ivanov55437462016-07-20 15:33:07 -07003183 call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false, get_realpath());
Elliott Hughesd23736e2012-11-01 15:16:56 -07003184}
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003185
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003186void soinfo::call_constructors() {
Elliott Hughesd23736e2012-11-01 15:16:56 -07003187 if (constructors_called) {
3188 return;
3189 }
Jesse Hallf5d16932012-01-30 15:39:57 -08003190
Elliott Hughesd23736e2012-11-01 15:16:56 -07003191 // We set constructors_called before actually calling the constructors, otherwise it doesn't
3192 // protect against recursive constructor calls. One simple example of constructor recursion
3193 // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
3194 // 1. The program depends on libc, so libc's constructor is called here.
3195 // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
3196 // 3. dlopen() calls the constructors on the newly created
3197 // soinfo for libc_malloc_debug_leak.so.
3198 // 4. The debug .so depends on libc, so CallConstructors is
3199 // called again with the libc soinfo. If it doesn't trigger the early-
3200 // out above, the libc constructor will be called again (recursively!).
3201 constructors_called = true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003202
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003203 if (!is_main_executable() && preinit_array_ != nullptr) {
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003204 // The GNU dynamic linker silently ignores these, but we warn the developer.
Dimitry Ivanov83fcb542016-05-04 17:19:14 -07003205 PRINT("\"%s\": ignoring DT_PREINIT_ARRAY in shared library!", get_realpath());
Elliott Hughesd23736e2012-11-01 15:16:56 -07003206 }
3207
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003208 get_children().for_each([] (soinfo* si) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003209 si->call_constructors();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003210 });
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003211
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003212 TRACE("\"%s\": calling constructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003213
3214 // DT_INIT should be called before DT_INIT_ARRAY if both are present.
Dimitry Ivanov55437462016-07-20 15:33:07 -07003215 call_function("DT_INIT", init_func_, get_realpath());
3216 call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false, get_realpath());
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04003217}
David 'Digit' Turner82156792009-05-18 14:37:41 +02003218
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003219void soinfo::call_destructors() {
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003220 if (!constructors_called) {
3221 return;
3222 }
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003223 TRACE("\"%s\": calling destructors", get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003224
3225 // DT_FINI_ARRAY must be parsed in reverse order.
Dimitry Ivanov55437462016-07-20 15:33:07 -07003226 call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true, get_realpath());
Elliott Hughes8147d3c2013-05-09 14:19:58 -07003227
3228 // DT_FINI should be called after DT_FINI_ARRAY if both are present.
Dimitry Ivanov55437462016-07-20 15:33:07 -07003229 call_function("DT_FINI", fini_func_, get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003230}
3231
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003232void soinfo::add_child(soinfo* child) {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003233 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003234 child->parents_.push_back(this);
3235 this->children_.push_back(child);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003236 }
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003237}
3238
3239void soinfo::remove_all_links() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003240 if (!has_min_version(0)) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003241 return;
3242 }
3243
3244 // 1. Untie connected soinfos from 'this'.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003245 children_.for_each([&] (soinfo* child) {
3246 child->parents_.remove_if([&] (const soinfo* parent) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003247 return parent == this;
3248 });
3249 });
3250
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003251 parents_.for_each([&] (soinfo* parent) {
3252 parent->children_.remove_if([&] (const soinfo* child) {
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003253 return child == this;
3254 });
3255 });
3256
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07003257 // 2. Remove from the primary namespace
3258 primary_namespace_->remove_soinfo(this);
3259 primary_namespace_ = nullptr;
3260
3261 // 3. Remove from secondary namespaces
3262 secondary_namespaces_.for_each([&](android_namespace_t* ns) {
3263 ns->remove_soinfo(this);
3264 });
3265
3266
3267 // 4. Once everything untied - clear local lists.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003268 parents_.clear();
3269 children_.clear();
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07003270 secondary_namespaces_.clear();
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003271}
3272
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003273dev_t soinfo::get_st_dev() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003274 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003275 return st_dev_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003276 }
3277
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003278 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003279};
3280
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003281ino_t soinfo::get_st_ino() const {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003282 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003283 return st_ino_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003284 }
3285
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003286 return 0;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003287}
3288
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003289off64_t soinfo::get_file_offset() const {
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003290 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003291 return file_offset_;
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -07003292 }
3293
3294 return 0;
3295}
3296
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003297uint32_t soinfo::get_rtld_flags() const {
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003298 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003299 return rtld_flags_;
Dmitriy Ivanove8ba50f2014-09-15 17:00:10 -07003300 }
3301
3302 return 0;
3303}
3304
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003305uint32_t soinfo::get_dt_flags_1() const {
3306 if (has_min_version(1)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003307 return dt_flags_1_;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003308 }
3309
3310 return 0;
3311}
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003312
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003313void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
3314 if (has_min_version(1)) {
3315 if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003316 rtld_flags_ |= RTLD_GLOBAL;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003317 }
3318
3319 if ((dt_flags_1 & DF_1_NODELETE) != 0) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003320 rtld_flags_ |= RTLD_NODELETE;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003321 }
3322
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003323 dt_flags_1_ = dt_flags_1;
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003324 }
3325}
3326
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003327void soinfo::set_nodelete() {
3328 rtld_flags_ |= RTLD_NODELETE;
3329}
3330
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003331const char* soinfo::get_realpath() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003332#if defined(__work_around_b_24465209__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003333 if (has_min_version(2)) {
3334 return realpath_.c_str();
3335 } else {
3336 return old_name_;
3337 }
3338#else
3339 return realpath_.c_str();
3340#endif
3341}
3342
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003343void soinfo::set_soname(const char* soname) {
3344#if defined(__work_around_b_24465209__)
3345 if (has_min_version(2)) {
3346 soname_ = soname;
3347 }
3348 strlcpy(old_name_, soname_, sizeof(old_name_));
3349#else
3350 soname_ = soname;
3351#endif
3352}
3353
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003354const char* soinfo::get_soname() const {
Dmitriy Ivanov280d5462015-09-28 10:14:17 -07003355#if defined(__work_around_b_24465209__)
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003356 if (has_min_version(2)) {
3357 return soname_;
3358 } else {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003359 return old_name_;
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003360 }
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003361#else
3362 return soname_;
3363#endif
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003364}
3365
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003366// This is a return on get_children()/get_parents() if
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003367// 'this->flags' does not have FLAG_NEW_SOINFO set.
3368static soinfo::soinfo_list_t g_empty_list;
3369
3370soinfo::soinfo_list_t& soinfo::get_children() {
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003371 if (has_min_version(0)) {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003372 return children_;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003373 }
3374
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07003375 return g_empty_list;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07003376}
3377
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003378const soinfo::soinfo_list_t& soinfo::get_children() const {
3379 if (has_min_version(0)) {
3380 return children_;
3381 }
3382
3383 return g_empty_list;
3384}
3385
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003386soinfo::soinfo_list_t& soinfo::get_parents() {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003387 if (has_min_version(0)) {
3388 return parents_;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003389 }
3390
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003391 return g_empty_list;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003392}
3393
Evgenii Stepanov68650822015-06-10 13:38:39 -07003394static std::vector<std::string> g_empty_runpath;
3395
3396const std::vector<std::string>& soinfo::get_dt_runpath() const {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003397 if (has_min_version(3)) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003398 return dt_runpath_;
3399 }
3400
3401 return g_empty_runpath;
3402}
3403
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07003404android_namespace_t* soinfo::get_primary_namespace() {
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003405 if (has_min_version(3)) {
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07003406 return primary_namespace_;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07003407 }
3408
3409 return &g_default_namespace;
3410}
3411
Dimitry Ivanovaca299a2016-04-11 12:42:58 -07003412void soinfo::add_secondary_namespace(android_namespace_t* secondary_ns) {
3413 CHECK(has_min_version(3));
3414 secondary_namespaces_.push_back(secondary_ns);
3415}
3416
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003417ElfW(Addr) soinfo::resolve_symbol_address(const ElfW(Sym)* s) const {
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07003418 if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
3419 return call_ifunc_resolver(s->st_value + load_bias);
3420 }
3421
3422 return static_cast<ElfW(Addr)>(s->st_value + load_bias);
3423}
3424
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003425const char* soinfo::get_string(ElfW(Word) index) const {
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003426 if (has_min_version(1) && (index >= strtab_size_)) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003427 __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003428 get_realpath(), strtab_size_, index);
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003429 }
3430
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003431 return strtab_ + index;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003432}
3433
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003434bool soinfo::is_gnu_hash() const {
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003435 return (flags_ & FLAG_GNU_HASH) != 0;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003436}
3437
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003438bool soinfo::can_unload() const {
Dimitry Ivanovdd906d72016-04-13 11:46:59 -07003439 return !is_linked() || ((get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003440}
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003441
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003442bool soinfo::is_linked() const {
3443 return (flags_ & FLAG_LINKED) != 0;
3444}
3445
3446bool soinfo::is_main_executable() const {
3447 return (flags_ & FLAG_EXE) != 0;
3448}
3449
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -08003450bool soinfo::is_linker() const {
3451 return (flags_ & FLAG_LINKER) != 0;
3452}
3453
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003454void soinfo::set_linked() {
3455 flags_ |= FLAG_LINKED;
3456}
3457
3458void soinfo::set_linker_flag() {
3459 flags_ |= FLAG_LINKER;
3460}
3461
3462void soinfo::set_main_executable() {
3463 flags_ |= FLAG_EXE;
3464}
3465
3466void soinfo::increment_ref_count() {
3467 local_group_root_->ref_count_++;
3468}
3469
3470size_t soinfo::decrement_ref_count() {
3471 return --local_group_root_->ref_count_;
3472}
3473
3474soinfo* soinfo::get_local_group_root() const {
3475 return local_group_root_;
3476}
3477
Dimitry Ivanovf45b0e92016-01-15 11:13:35 -08003478
3479void soinfo::set_mapped_by_caller(bool mapped_by_caller) {
3480 if (mapped_by_caller) {
3481 flags_ |= FLAG_MAPPED_BY_CALLER;
3482 } else {
3483 flags_ &= ~FLAG_MAPPED_BY_CALLER;
3484 }
3485}
3486
3487bool soinfo::is_mapped_by_caller() const {
3488 return (flags_ & FLAG_MAPPED_BY_CALLER) != 0;
3489}
3490
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003491// This function returns api-level at the time of
3492// dlopen/load. Note that libraries opened by system
3493// will always have 'current' api level.
3494uint32_t soinfo::get_target_sdk_version() const {
3495 if (!has_min_version(2)) {
3496 return __ANDROID_API__;
3497 }
3498
3499 return local_group_root_->target_sdk_version_;
3500}
3501
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07003502uintptr_t soinfo::get_handle() const {
3503 CHECK(has_min_version(3));
3504 CHECK(handle_ != 0);
3505 return handle_;
3506}
3507
3508void* soinfo::to_handle() {
3509 if (get_application_target_sdk_version() <= 23 || !has_min_version(3)) {
3510 return this;
3511 }
3512
3513 return reinterpret_cast<void*>(get_handle());
3514}
3515
3516void soinfo::generate_handle() {
3517 CHECK(has_min_version(3));
3518 CHECK(handle_ == 0); // Make sure this is the first call
3519
3520 // Make sure the handle is unique and does not collide
3521 // with special values which are RTLD_DEFAULT and RTLD_NEXT.
3522 do {
3523 arc4random_buf(&handle_, sizeof(handle_));
3524 // the least significant bit for the handle is always 1
3525 // making it easy to test the type of handle passed to
3526 // dl* functions.
3527 handle_ = handle_ | 1;
3528 } while (handle_ == reinterpret_cast<uintptr_t>(RTLD_DEFAULT) ||
3529 handle_ == reinterpret_cast<uintptr_t>(RTLD_NEXT) ||
3530 g_soinfo_handles_map.find(handle_) != g_soinfo_handles_map.end());
3531
3532 g_soinfo_handles_map[handle_] = this;
3533}
3534
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003535bool soinfo::prelink_image() {
Ningsheng Jiane93be992014-09-16 15:22:10 +08003536 /* Extract dynamic section */
3537 ElfW(Word) dynamic_flags = 0;
3538 phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
Dmitriy Ivanov498eb182014-09-05 14:57:59 -07003539
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003540 /* We can't log anything until the linker is relocated */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003541 bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003542 if (!relocating_linker) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003543 INFO("[ Linking \"%s\" ]", get_realpath());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003544 DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003545 }
3546
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003547 if (dynamic == nullptr) {
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003548 if (!relocating_linker) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003549 DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
David 'Digit' Turnerb52e4382012-06-19 01:24:17 +02003550 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003551 return false;
3552 } else {
3553 if (!relocating_linker) {
3554 DEBUG("dynamic = %p", dynamic);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003555 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003556 }
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003557
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003558#if defined(__arm__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003559 (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
3560 &ARM_exidx, &ARM_exidx_count);
David 'Digit' Turner63f99f42012-06-19 00:08:39 +02003561#endif
3562
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003563 // Extract useful information from dynamic section.
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003564 // Note that: "Except for the DT_NULL element at the end of the array,
3565 // and the relative order of DT_NEEDED elements, entries may appear in any order."
3566 //
3567 // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003568 uint32_t needed_count = 0;
3569 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
3570 DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
3571 d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3572 switch (d->d_tag) {
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003573 case DT_SONAME:
Dmitriy Ivanov618f1a32015-03-17 20:06:36 -07003574 // this is parsed after we have strtab initialized (see below).
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003575 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003576
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003577 case DT_HASH:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003578 nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
3579 nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
3580 bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
3581 chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003582 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003583
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003584 case DT_GNU_HASH:
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003585 gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003586 // skip symndx
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003587 gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
3588 gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003589
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003590 gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003591 gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003592 // amend chain for symndx = header[1]
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003593 gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
3594 reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003595
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003596 if (!powerof2(gnu_maskwords_)) {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003597 DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003598 gnu_maskwords_, get_realpath());
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003599 return false;
3600 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003601 --gnu_maskwords_;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003602
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003603 flags_ |= FLAG_GNU_HASH;
Dmitriy Ivanovec18ce02014-11-09 19:27:20 -08003604 break;
3605
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003606 case DT_STRTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003607 strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003608 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003609
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003610 case DT_STRSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003611 strtab_size_ = d->d_un.d_val;
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003612 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003613
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003614 case DT_SYMTAB:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003615 symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003616 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003617
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003618 case DT_SYMENT:
3619 if (d->d_un.d_val != sizeof(ElfW(Sym))) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003620 DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
3621 static_cast<size_t>(d->d_un.d_val), get_realpath());
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003622 return false;
3623 }
3624 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003625
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003626 case DT_PLTREL:
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003627#if defined(USE_RELA)
3628 if (d->d_un.d_val != DT_RELA) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003629 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003630 return false;
3631 }
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003632#else
3633 if (d->d_un.d_val != DT_REL) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003634 DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003635 return false;
3636 }
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003637#endif
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003638 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003639
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003640 case DT_JMPREL:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003641#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003642 plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003643#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003644 plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003645#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003646 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003647
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003648 case DT_PLTRELSZ:
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003649#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003650 plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003651#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003652 plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003653#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003654 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003655
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003656 case DT_PLTGOT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003657#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003658 // Used by mips and mips64.
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003659 plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003660#endif
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003661 // Ignore for other platforms... (because RTLD_LAZY is not supported)
3662 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003663
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003664 case DT_DEBUG:
3665 // Set the DT_DEBUG entry to the address of _r_debug for GDB
3666 // if the dynamic table is writable
Chris Dearman99186652014-02-06 20:36:51 -08003667// FIXME: not working currently for N64
3668// The flags for the LOAD and DYNAMIC program headers do not agree.
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003669// The LOAD section containing the dynamic table has been mapped as
Chris Dearman99186652014-02-06 20:36:51 -08003670// read-only, but the DYNAMIC header claims it is writable.
3671#if !(defined(__mips__) && defined(__LP64__))
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003672 if ((dynamic_flags & PF_W) != 0) {
3673 d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
3674 }
Chris Dearman99186652014-02-06 20:36:51 -08003675#endif
Dmitriy Ivanovc6292ea2015-02-13 16:29:50 -08003676 break;
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07003677#if defined(USE_RELA)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003678 case DT_RELA:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003679 rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003680 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003681
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003682 case DT_RELASZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003683 rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003684 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003685
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003686 case DT_ANDROID_RELA:
3687 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3688 break;
3689
3690 case DT_ANDROID_RELASZ:
3691 android_relocs_size_ = d->d_un.d_val;
3692 break;
3693
3694 case DT_ANDROID_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003695 DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003696 return false;
3697
3698 case DT_ANDROID_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003699 DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003700 return false;
3701
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003702 case DT_RELAENT:
3703 if (d->d_un.d_val != sizeof(ElfW(Rela))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003704 DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003705 return false;
3706 }
3707 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003708
3709 // ignored (see DT_RELCOUNT comments for details)
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003710 case DT_RELACOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003711 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003712
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003713 case DT_REL:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003714 DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003715 return false;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003716
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003717 case DT_RELSZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003718 DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003719 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003720
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003721#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003722 case DT_REL:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003723 rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003724 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003725
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003726 case DT_RELSZ:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003727 rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003728 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003729
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003730 case DT_RELENT:
3731 if (d->d_un.d_val != sizeof(ElfW(Rel))) {
Dmitriy Ivanovf240aa82014-09-16 23:34:20 -07003732 DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003733 return false;
3734 }
3735 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003736
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003737 case DT_ANDROID_REL:
3738 android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
3739 break;
3740
3741 case DT_ANDROID_RELSZ:
3742 android_relocs_size_ = d->d_un.d_val;
3743 break;
3744
3745 case DT_ANDROID_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003746 DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003747 return false;
3748
3749 case DT_ANDROID_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003750 DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003751 return false;
3752
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003753 // "Indicates that all RELATIVE relocations have been concatenated together,
3754 // and specifies the RELATIVE relocation count."
3755 //
3756 // TODO: Spec also mentions that this can be used to optimize relocation process;
3757 // Not currently used by bionic linker - ignored.
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003758 case DT_RELCOUNT:
Dmitriy Ivanov4a6e9a82014-09-16 15:51:25 -07003759 break;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003760
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003761 case DT_RELA:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003762 DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003763 return false;
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003764
3765 case DT_RELASZ:
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003766 DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003767 return false;
3768
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07003769#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003770 case DT_INIT:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003771 init_func_ = reinterpret_cast<linker_ctor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003772 DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003773 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003774
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003775 case DT_FINI:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003776 fini_func_ = reinterpret_cast<linker_dtor_function_t>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003777 DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003778 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003779
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003780 case DT_INIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003781 init_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003782 DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003783 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003784
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003785 case DT_INIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003786 init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003787 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003788
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003789 case DT_FINI_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003790 fini_array_ = reinterpret_cast<linker_dtor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003791 DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003792 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003793
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003794 case DT_FINI_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003795 fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003796 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003797
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003798 case DT_PREINIT_ARRAY:
Dimitry Ivanov55437462016-07-20 15:33:07 -07003799 preinit_array_ = reinterpret_cast<linker_ctor_function_t*>(load_bias + d->d_un.d_ptr);
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003800 DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003801 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003802
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003803 case DT_PREINIT_ARRAYSZ:
Dmitriy Ivanov1649e7e2015-01-22 16:04:25 -08003804 preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003805 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003806
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003807 case DT_TEXTREL:
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003808#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003809 DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003810 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003811#else
3812 has_text_relocations = true;
3813 break;
3814#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003815
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003816 case DT_SYMBOLIC:
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003817 has_DT_SYMBOLIC = true;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003818 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003819
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003820 case DT_NEEDED:
3821 ++needed_count;
3822 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003823
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003824 case DT_FLAGS:
3825 if (d->d_un.d_val & DF_TEXTREL) {
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003826#if defined(__LP64__)
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003827 DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003828 return false;
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003829#else
3830 has_text_relocations = true;
3831#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003832 }
Dmitriy Ivanov96bc37f2014-09-29 12:10:36 -07003833 if (d->d_un.d_val & DF_SYMBOLIC) {
3834 has_DT_SYMBOLIC = true;
3835 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003836 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003837
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003838 case DT_FLAGS_1:
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003839 set_dt_flags_1(d->d_un.d_val);
Dmitriy Ivanov1b20daf2014-05-19 15:06:58 -07003840
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07003841 if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
Dmitriy Ivanov087005f2015-05-28 11:44:31 -07003842 DL_WARN("%s: unsupported flags DT_FLAGS_1=%p", get_realpath(), reinterpret_cast<void*>(d->d_un.d_val));
Dmitriy Ivanov6cdeb522014-09-29 19:14:45 -07003843 }
3844 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003845#if defined(__mips__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003846 case DT_MIPS_RLD_MAP:
3847 // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
3848 {
3849 r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
3850 *dp = &_r_debug;
3851 }
3852 break;
Lazar Trsic83b44a92016-04-06 13:39:17 +02003853 case DT_MIPS_RLD_MAP_REL:
3854 // Set the DT_MIPS_RLD_MAP_REL entry to the address of _r_debug for GDB.
Raghu Gandham68815722014-12-18 19:12:19 -08003855 {
Dmitriy Ivanov20d89cb2015-03-30 18:43:38 -07003856 r_debug** dp = reinterpret_cast<r_debug**>(
3857 reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
Raghu Gandham68815722014-12-18 19:12:19 -08003858 *dp = &_r_debug;
3859 }
3860 break;
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003861
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003862 case DT_MIPS_RLD_VERSION:
3863 case DT_MIPS_FLAGS:
3864 case DT_MIPS_BASE_ADDRESS:
3865 case DT_MIPS_UNREFEXTNO:
3866 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003867
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003868 case DT_MIPS_SYMTABNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003869 mips_symtabno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003870 break;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003871
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003872 case DT_MIPS_LOCAL_GOTNO:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003873 mips_local_gotno_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003874 break;
3875
3876 case DT_MIPS_GOTSYM:
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003877 mips_gotsym_ = d->d_un.d_val;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003878 break;
3879#endif
Dmitriy Ivanovea6eae12014-10-15 14:59:01 -07003880 // Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
3881 case DT_BIND_NOW:
3882 break;
3883
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003884 case DT_VERSYM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003885 versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
3886 break;
3887
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003888 case DT_VERDEF:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003889 verdef_ptr_ = load_bias + d->d_un.d_ptr;
3890 break;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003891 case DT_VERDEFNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003892 verdef_cnt_ = d->d_un.d_val;
3893 break;
3894
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003895 case DT_VERNEED:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003896 verneed_ptr_ = load_bias + d->d_un.d_ptr;
3897 break;
3898
Alexander Ivchenkoe8314332014-12-02 15:32:25 +03003899 case DT_VERNEEDNUM:
Dmitriy Ivanov2a815362015-04-09 13:42:33 -07003900 verneed_cnt_ = d->d_un.d_val;
Dmitriy Ivanov513e29e2014-10-06 11:30:43 -07003901 break;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003902
Evgenii Stepanov68650822015-06-10 13:38:39 -07003903 case DT_RUNPATH:
3904 // this is parsed after we have strtab initialized (see below).
3905 break;
3906
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003907 default:
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003908 if (!relocating_linker) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003909 DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(),
Dmitriy Ivanov8f61d992014-09-16 14:31:06 -07003910 reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
3911 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003912 break;
Brian Carlstromd4ee82d2013-02-28 15:58:45 -08003913 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003914 }
3915
Duane Sandbc425c72015-06-01 16:29:14 -07003916#if defined(__mips__) && !defined(__LP64__)
3917 if (!mips_check_and_adjust_fp_modes()) {
3918 return false;
3919 }
3920#endif
3921
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003922 DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003923 reinterpret_cast<void*>(base), strtab_, symtab_);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003924
3925 // Sanity checks.
3926 if (relocating_linker && needed_count != 0) {
3927 DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
3928 return false;
3929 }
Dmitriy Ivanov3597b802015-03-09 12:02:02 -07003930 if (nbucket_ == 0 && gnu_nbucket_ == 0) {
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07003931 DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003932 "(new hash type from the future?)", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003933 return false;
3934 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003935 if (strtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003936 DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003937 return false;
3938 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08003939 if (symtab_ == 0) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07003940 DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003941 return false;
3942 }
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003943
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003944 // second pass - parse entries relying on strtab
3945 for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
Evgenii Stepanov68650822015-06-10 13:38:39 -07003946 switch (d->d_tag) {
3947 case DT_SONAME:
Dmitriy Ivanov4f7a7ad2015-10-15 12:07:25 -07003948 set_soname(get_string(d->d_un.d_val));
Evgenii Stepanov68650822015-06-10 13:38:39 -07003949 break;
3950 case DT_RUNPATH:
Evgenii Stepanov68650822015-06-10 13:38:39 -07003951 set_dt_runpath(get_string(d->d_un.d_val));
3952 break;
Dmitriy Ivanov624b8f12015-06-08 10:41:33 -07003953 }
3954 }
3955
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003956 // Before M release linker was using basename in place of soname.
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003957 // In the case when dt_soname is absent some apps stop working
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003958 // because they can't find dt_needed library by soname.
3959 // This workaround should keep them working. (applies only
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003960 // for apps targeting sdk version <=22). Make an exception for
3961 // the main executable and linker; they do not need to have dt_soname
3962 if (soname_ == nullptr && this != somain && (flags_ & FLAG_LINKER) == 0 &&
3963 get_application_target_sdk_version() <= 22) {
Dmitriy Ivanov75108f42015-06-02 13:28:06 -07003964 soname_ = basename(realpath_.c_str());
3965 DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
3966 get_realpath(), soname_);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07003967 // 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 -07003968 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07003969 return true;
Dmitriy Ivanov14669a92014-09-05 16:42:53 -07003970}
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003971
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08003972bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
3973 const android_dlextinfo* extinfo) {
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08003974
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08003975 local_group_root_ = local_group.front();
3976 if (local_group_root_ == nullptr) {
3977 local_group_root_ = this;
3978 }
3979
Dmitriy Ivanov19133522015-06-02 17:36:54 -07003980 if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
3981 target_sdk_version_ = get_application_target_sdk_version();
3982 }
3983
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07003984 VersionTracker version_tracker;
3985
3986 if (!version_tracker.init(this)) {
3987 return false;
3988 }
3989
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003990#if !defined(__LP64__)
3991 if (has_text_relocations) {
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003992 // Fail if app is targeting sdk version > 22
Dmitriy Ivanov80687862015-10-09 13:58:46 -07003993 if (get_application_target_sdk_version() > 22) {
Dmitriy Ivanovfae39d22015-10-13 11:07:56 -07003994 PRINT("%s: has text relocations", get_realpath());
Dmitriy Ivanove4ad91f2015-06-12 15:00:31 -07003995 DL_ERR("%s: has text relocations", get_realpath());
3996 return false;
3997 }
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00003998 // Make segments writable to allow text relocations to work properly. We will later call
Dmitriy Ivanov7e039932015-10-01 14:02:19 -07003999 // phdr_table_protect_segments() after all of them are applied.
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00004000 DL_WARN("%s has text relocations. This is wasting memory and prevents "
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004001 "security hardening. Please fix.", get_realpath());
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07004002 add_dlwarning(get_realpath(), "text relocations");
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00004003 if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
4004 DL_ERR("can't unprotect loadable segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004005 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00004006 return false;
4007 }
4008 }
4009#endif
4010
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08004011 if (android_relocs_ != nullptr) {
4012 // check signature
4013 if (android_relocs_size_ > 3 &&
4014 android_relocs_[0] == 'A' &&
4015 android_relocs_[1] == 'P' &&
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07004016 android_relocs_[2] == 'S' &&
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08004017 android_relocs_[3] == '2') {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004018 DEBUG("[ android relocating %s ]", get_realpath());
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08004019
4020 bool relocated = false;
4021 const uint8_t* packed_relocs = android_relocs_ + 4;
4022 const size_t packed_relocs_size = android_relocs_size_ - 4;
4023
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07004024 relocated = relocate(
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07004025 version_tracker,
Dmitriy Ivanov18870d32015-04-22 13:10:04 -07004026 packed_reloc_iterator<sleb128_decoder>(
4027 sleb128_decoder(packed_relocs, packed_relocs_size)),
4028 global_group, local_group);
Dmitriy Ivanov18a69562015-02-04 16:05:30 -08004029
4030 if (!relocated) {
4031 return false;
4032 }
4033 } else {
4034 DL_ERR("bad android relocation header.");
4035 return false;
4036 }
4037 }
4038
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07004039#if defined(USE_RELA)
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004040 if (rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004041 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07004042 if (!relocate(version_tracker,
4043 plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004044 return false;
Elliott Hughesc00f2cb2013-10-04 17:01:33 -07004045 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004046 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004047 if (plt_rela_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004048 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07004049 if (!relocate(version_tracker,
4050 plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004051 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004052 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004053 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07004054#else
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004055 if (rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004056 DEBUG("[ relocating %s ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07004057 if (!relocate(version_tracker,
4058 plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004059 return false;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004060 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004061 }
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004062 if (plt_rel_ != nullptr) {
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004063 DEBUG("[ relocating %s plt ]", get_realpath());
Dmitriy Ivanov7e4bbba2015-04-30 19:49:19 -07004064 if (!relocate(version_tracker,
4065 plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004066 return false;
Brigid Smithc5a13ef2014-07-23 11:22:25 -07004067 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004068 }
Dmitriy Ivanov9aea1642014-09-11 15:16:03 -07004069#endif
Brigid Smithc5a13ef2014-07-23 11:22:25 -07004070
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07004071#if defined(__mips__)
Dmitriy Ivanovf39cb632015-04-30 20:17:03 -07004072 if (!mips_relocate_got(version_tracker, global_group, local_group)) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004073 return false;
4074 }
Raghu Gandhamd7daacb2012-07-31 12:07:22 -07004075#endif
4076
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004077 DEBUG("[ finished linking %s ]", get_realpath());
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004078
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00004079#if !defined(__LP64__)
4080 if (has_text_relocations) {
4081 // All relocations are done, we can protect our segments back to read-only.
4082 if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
4083 DL_ERR("can't protect segments for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004084 get_realpath(), strerror(errno));
Dimitry Ivanov56be6ed2015-04-01 21:18:48 +00004085 return false;
4086 }
4087 }
4088#endif
4089
Mingwei Shibe910522015-11-12 07:02:14 +00004090 // We can also turn on GNU RELRO protection if we're not linking the dynamic linker
4091 // itself --- it can't make system calls yet, and will have to call protect_relro later.
4092 if (!is_linker() && !protect_relro()) {
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004093 return false;
4094 }
Nick Kralevich9ec0f032012-02-28 10:40:00 -08004095
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004096 /* Handle serializing/sharing the RELRO segment */
4097 if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
4098 if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
4099 extinfo->relro_fd) < 0) {
4100 DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004101 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004102 return false;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00004103 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004104 } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
4105 if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
4106 extinfo->relro_fd) < 0) {
4107 DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004108 get_realpath(), strerror(errno));
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004109 return false;
4110 }
4111 }
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +00004112
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004113 notify_gdb_of_load(this);
4114 return true;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004115}
4116
Mingwei Shibe910522015-11-12 07:02:14 +00004117bool soinfo::protect_relro() {
4118 if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
4119 DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
4120 get_realpath(), strerror(errno));
4121 return false;
4122 }
4123 return true;
4124}
4125
Nick Kralevich468319c2011-11-11 15:53:17 -08004126/*
Sergey Melnikovc45087b2013-01-25 16:40:13 +04004127 * This function add vdso to internal dso list.
4128 * It helps to stack unwinding through signal handlers.
4129 * Also, it makes bionic more like glibc.
4130 */
Kito Cheng812fd422014-03-25 22:53:56 +08004131static void add_vdso(KernelArgumentBlock& args __unused) {
Elliott Hughes4eeb1f12013-10-25 17:38:02 -07004132#if defined(AT_SYSINFO_EHDR)
Elliott Hughes0266ae52014-02-10 17:46:57 -08004133 ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07004134 if (ehdr_vdso == nullptr) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08004135 return;
4136 }
Sergey Melnikovc45087b2013-01-25 16:40:13 +04004137
Dmitriy Ivanovd9b08a02015-11-16 13:17:27 -08004138 soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04004139
Elliott Hughes0266ae52014-02-10 17:46:57 -08004140 si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
4141 si->phnum = ehdr_vdso->e_phnum;
4142 si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
4143 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004144 si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
Sergey Melnikovebd506c2013-10-31 18:02:12 +04004145
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004146 si->prelink_image();
4147 si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
Sergey Melnikovc45087b2013-01-25 16:40:13 +04004148#endif
4149}
4150
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004151/* gdb expects the linker to be in the debug shared object list.
4152 * Without this, gdb has trouble locating the linker's ".text"
4153 * and ".plt" sections. Gdb could also potentially use this to
4154 * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
Dimitry Ivanov64001292016-02-17 14:13:06 -08004155 * Note that the linker shouldn't be on the soinfo list.
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004156 */
4157static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08004158 static link_map linker_link_map_for_gdb;
4159#if defined(__LP64__)
4160 static char kLinkerPath[] = "/system/bin/linker64";
4161#else
4162 static char kLinkerPath[] = "/system/bin/linker";
4163#endif
Dmitriy Ivanovaae859c2015-03-31 11:14:03 -07004164
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08004165 linker_link_map_for_gdb.l_addr = linker_base;
4166 linker_link_map_for_gdb.l_name = kLinkerPath;
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004167
4168 /*
4169 * Set the dynamic field in the link map otherwise gdb will complain with
4170 * the following:
4171 * warning: .dynamic section for "/system/bin/linker" is not at the
4172 * expected address (wrong library or version mismatch?)
4173 */
4174 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
4175 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
4176 phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
Dimitry Ivanov8d22dd52016-02-16 13:43:35 -08004177 &linker_link_map_for_gdb.l_ld, nullptr);
4178
4179 insert_link_map_into_debug_map(&linker_link_map_for_gdb);
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004180}
4181
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004182static void init_default_namespace() {
4183 g_default_namespace.set_name("(default)");
4184 g_default_namespace.set_isolated(false);
4185
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004186 const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
4187 somain->load_bias);
4188 const char* bname = basename(interp);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004189 if (bname && (strcmp(bname, "linker_asan") == 0 || strcmp(bname, "linker_asan64") == 0)) {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004190 g_default_ld_paths = kAsanDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004191 } else {
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004192 g_default_ld_paths = kDefaultLdPaths;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004193 }
4194
4195 std::vector<std::string> ld_default_paths;
4196 for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
4197 ld_default_paths.push_back(g_default_ld_paths[i]);
4198 }
4199
4200 g_default_namespace.set_default_library_paths(std::move(ld_default_paths));
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004201};
4202
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07004203extern "C" int __system_properties_init(void);
4204
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07004205static const char* get_executable_path() {
4206 static std::string executable_path;
4207 if (executable_path.empty()) {
4208 char path[PATH_MAX];
4209 ssize_t path_len = readlink("/proc/self/exe", path, sizeof(path));
4210 if (path_len == -1 || path_len >= static_cast<ssize_t>(sizeof(path))) {
4211 __libc_fatal("readlink('/proc/self/exe') failed: %s", strerror(errno));
4212 }
4213 executable_path = std::string(path, path_len);
4214 }
4215
4216 return executable_path.c_str();
4217}
4218
Dmitriy Ivanovd59e5002014-05-09 09:10:14 -07004219/*
Nick Kralevich468319c2011-11-11 15:53:17 -08004220 * This code is called after the linker has linked itself and
4221 * fixed it's own GOT. It is safe to make references to externs
4222 * and other non-local data at this point.
4223 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004224static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04004225#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004226 struct timeval t0, t1;
4227 gettimeofday(&t0, 0);
Evgeniy Stepanov1a78fbb2012-03-22 18:01:53 +04004228#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004229
Elliott Hughes1801db32015-06-08 18:04:00 -07004230 // Sanitize the environment.
4231 __libc_init_AT_SECURE(args);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01004232
Dmitriy Ivanovb4e50672015-04-28 15:49:26 -07004233 // Initialize system properties
4234 __system_properties_init(); // may use 'environ'
4235
Josh Gao3cc387e2016-06-15 18:33:26 -07004236 // Register the debuggerd signal handler.
4237 debuggerd_callbacks_t callbacks = {
4238 .get_abort_message = []() {
4239 return g_abort_message;
4240 },
4241 .post_dump = &notify_gdb_of_libraries,
4242 };
4243 debuggerd_init(&callbacks);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004244
Dimitry Ivanovb996d602016-07-11 18:11:39 -07004245 g_linker_logger.ResetState();
4246
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004247 // Get a few environment variables.
Elliott Hughes1801db32015-06-08 18:04:00 -07004248 const char* LD_DEBUG = getenv("LD_DEBUG");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004249 if (LD_DEBUG != nullptr) {
4250 g_ld_debug_verbosity = atoi(LD_DEBUG);
4251 }
4252
Elliott Hughes116b5692016-01-04 17:45:36 -08004253#if defined(__LP64__)
4254 INFO("[ Android dynamic linker (64-bit) ]");
4255#else
4256 INFO("[ Android dynamic linker (32-bit) ]");
4257#endif
4258
Elliott Hughes1801db32015-06-08 18:04:00 -07004259 // These should have been sanitized by __libc_init_AT_SECURE, but the test
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004260 // doesn't cost us anything.
4261 const char* ldpath_env = nullptr;
4262 const char* ldpreload_env = nullptr;
Elliott Hughes1801db32015-06-08 18:04:00 -07004263 if (!getauxval(AT_SECURE)) {
4264 ldpath_env = getenv("LD_LIBRARY_PATH");
Elliott Hughes116b5692016-01-04 17:45:36 -08004265 if (ldpath_env != nullptr) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07004266 INFO("[ LD_LIBRARY_PATH set to \"%s\" ]", ldpath_env);
Elliott Hughes116b5692016-01-04 17:45:36 -08004267 }
Elliott Hughes1801db32015-06-08 18:04:00 -07004268 ldpreload_env = getenv("LD_PRELOAD");
Elliott Hughes116b5692016-01-04 17:45:36 -08004269 if (ldpreload_env != nullptr) {
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07004270 INFO("[ LD_PRELOAD set to \"%s\" ]", ldpreload_env);
Elliott Hughes116b5692016-01-04 17:45:36 -08004271 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004272 }
4273
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07004274 const char* executable_path = get_executable_path();
4275 struct stat file_stat;
4276 if (TEMP_FAILURE_RETRY(stat(executable_path, &file_stat)) != 0) {
4277 __libc_fatal("unable to stat file for the executable \"%s\": %s", executable_path, strerror(errno));
4278 }
4279
4280 soinfo* si = soinfo_alloc(&g_default_namespace, executable_path, &file_stat, 0, RTLD_GLOBAL);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004281 if (si == nullptr) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004282 __libc_fatal("Couldn't allocate soinfo: out of memory?");
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004283 }
4284
4285 /* bootstrap the link map, the main exe always needs to be first */
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004286 si->set_main_executable();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004287 link_map* map = &(si->link_map_head);
4288
Dimitry Ivanove97d8ed2016-03-01 15:55:56 -08004289 // Register the main executable and the linker upfront to have
4290 // gdb aware of them before loading the rest of the dependency
4291 // tree.
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004292 map->l_addr = 0;
Dimitry Ivanov2ba1cf32016-05-17 13:29:37 -07004293 map->l_name = const_cast<char*>(executable_path);
Dimitry Ivanovf3064e42016-02-17 15:25:25 -08004294 insert_link_map_into_debug_map(map);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004295 init_linker_info_for_gdb(linker_base);
4296
4297 // Extract information passed from the kernel.
4298 si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
4299 si->phnum = args.getauxval(AT_PHNUM);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004300
4301 /* Compute the value of si->base. We can't rely on the fact that
4302 * the first entry is the PHDR because this will not be true
4303 * for certain executables (e.g. some in the NDK unit test suite)
4304 */
4305 si->base = 0;
4306 si->size = phdr_table_get_load_size(si->phdr, si->phnum);
4307 si->load_bias = 0;
4308 for (size_t i = 0; i < si->phnum; ++i) {
4309 if (si->phdr[i].p_type == PT_PHDR) {
4310 si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
4311 si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
4312 break;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07004313 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004314 }
4315 si->dynamic = nullptr;
Nick Kralevich8d3e91d2013-04-25 13:15:24 -07004316
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004317 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
4318 if (elf_hdr->e_type != ET_DYN) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004319 __libc_fatal("\"%s\": error: only position independent executables (PIE) are supported.",
Dimitry Ivanov55437462016-07-20 15:33:07 -07004320 g_argv[0]);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004321 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004322
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004323 // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
4324 parse_LD_LIBRARY_PATH(ldpath_env);
4325 parse_LD_PRELOAD(ldpreload_env);
David 'Digit' Turnerbe575592010-12-16 19:52:02 +01004326
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004327 somain = si;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004328
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004329 init_default_namespace();
Evgenii Stepanovd640b222015-07-10 17:54:01 -07004330
Dmitriy Ivanov67181252015-01-07 15:48:25 -08004331 if (!si->prelink_image()) {
Dimitry Ivanov55437462016-07-20 15:33:07 -07004332 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
Dmitriy Ivanov67181252015-01-07 15:48:25 -08004333 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004334
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004335 // add somain to global group
4336 si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
4337
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004338 // Load ld_preloads and dependencies.
4339 StringLinkedList needed_library_name_list;
4340 size_t needed_libraries_count = 0;
4341 size_t ld_preloads_count = 0;
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004342
4343 for (const auto& ld_preload_name : g_ld_preload_names) {
4344 needed_library_name_list.push_back(ld_preload_name.c_str());
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004345 ++needed_libraries_count;
Dmitriy Ivanovf8093a92015-04-28 18:09:53 -07004346 ++ld_preloads_count;
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004347 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004348
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004349 for_each_dt_needed(si, [&](const char* name) {
4350 needed_library_name_list.push_back(name);
4351 ++needed_libraries_count;
4352 });
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004353
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004354 const char* needed_library_names[needed_libraries_count];
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004355
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004356 memset(needed_library_names, 0, sizeof(needed_library_names));
4357 needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004358
Dmitriy Ivanovd165f562015-03-23 18:43:02 -07004359 if (needed_libraries_count > 0 &&
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004360 !find_libraries(&g_default_namespace, si, needed_library_names, needed_libraries_count,
4361 nullptr, &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr,
Evgenii Stepanov0cdef7e2015-07-06 17:56:31 -07004362 /* add_as_children */ true)) {
Dimitry Ivanov55437462016-07-20 15:33:07 -07004363 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004364 } else if (needed_libraries_count == 0) {
4365 if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
Dimitry Ivanov55437462016-07-20 15:33:07 -07004366 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004367 }
4368 si->increment_ref_count();
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004369 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004370
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004371 add_vdso(args);
Nick Kralevich2aebf542014-05-07 10:32:39 -07004372
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004373 {
4374 ProtectedDataGuard guard;
Matt Fischer4fd42c12009-12-31 12:09:10 -06004375
Dmitriy Ivanov279a22f2015-01-23 12:03:53 -08004376 si->call_pre_init_constructors();
4377
4378 /* After the prelink_image, the si->load_bias is initialized.
4379 * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
4380 * We need to update this value for so exe here. So Unwind_Backtrace
4381 * for some arch like x86 could work correctly within so exe.
4382 */
4383 map->l_addr = si->load_bias;
4384 si->call_constructors();
4385 }
Evgeniy Stepanove83c56d2011-12-21 13:03:54 +04004386
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004387#if TIMING
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004388 gettimeofday(&t1, nullptr);
Dimitry Ivanov55437462016-07-20 15:33:07 -07004389 PRINT("LINKER TIME: %s: %d microseconds", g_argv[0], (int) (
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004390 (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
4391 (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004392#endif
4393#if STATS
Dimitry Ivanov55437462016-07-20 15:33:07 -07004394 PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", g_argv[0],
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004395 linker_stats.count[kRelocAbsolute],
4396 linker_stats.count[kRelocRelative],
4397 linker_stats.count[kRelocCopy],
4398 linker_stats.count[kRelocSymbol]);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004399#endif
4400#if COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004401 {
4402 unsigned n;
4403 unsigned i;
4404 unsigned count = 0;
4405 for (n = 0; n < 4096; n++) {
4406 if (bitmask[n]) {
4407 unsigned x = bitmask[n];
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004408#if defined(__LP64__)
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004409 for (i = 0; i < 32; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004410#else
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004411 for (i = 0; i < 8; i++) {
Marcus Oaklande365f9d2013-10-10 15:19:31 +01004412#endif
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004413 if (x & 1) {
4414 count++;
4415 }
4416 x >>= 1;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004417 }
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004418 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004419 }
Dimitry Ivanov55437462016-07-20 15:33:07 -07004420 PRINT("PAGES MODIFIED: %s: %d (%dKB)", g_argv[0], count, count * 4);
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004421 }
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004422#endif
4423
4424#if TIMING || STATS || COUNT_PAGES
Dmitriy Ivanov6abf6242014-09-12 09:43:13 -07004425 fflush(stdout);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004426#endif
4427
Dimitry Ivanove687d062016-02-16 13:25:29 -08004428 ElfW(Addr) entry = args.getauxval(AT_ENTRY);
Dimitry Ivanov769b33f2016-07-21 11:33:40 -07004429 TRACE("[ Ready to execute \"%s\" @ %p ]", si->get_realpath(), reinterpret_cast<void*>(entry));
Dimitry Ivanove687d062016-02-16 13:25:29 -08004430 return entry;
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08004431}
Nick Kralevich468319c2011-11-11 15:53:17 -08004432
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004433/* Compute the load-bias of an existing executable. This shall only
4434 * be used to compute the load bias of an executable or shared library
4435 * that was loaded by the kernel itself.
4436 *
4437 * Input:
4438 * elf -> address of ELF header, assumed to be at the start of the file.
4439 * Return:
4440 * load bias, i.e. add the value of any p_vaddr in the file to get
4441 * the corresponding address in memory.
4442 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004443static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
4444 ElfW(Addr) offset = elf->e_phoff;
Dmitriy Ivanov3edb9182015-05-07 10:48:00 -07004445 const ElfW(Phdr)* phdr_table =
4446 reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004447 const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004448
Elliott Hughes0266ae52014-02-10 17:46:57 -08004449 for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
Kito Chengfa8c05d2013-03-12 14:58:06 +08004450 if (phdr->p_type == PT_LOAD) {
Elliott Hughes0266ae52014-02-10 17:46:57 -08004451 return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004452 }
Kito Chengfa8c05d2013-03-12 14:58:06 +08004453 }
4454 return 0;
David 'Digit' Turnerbea23e52012-06-18 23:38:46 +02004455}
4456
Dimitry Ivanovb996d602016-07-11 18:11:39 -07004457static void __linker_cannot_link() {
Dimitry Ivanov55437462016-07-20 15:33:07 -07004458 __libc_fatal("CANNOT LINK EXECUTABLE \"%s\": %s", g_argv[0], linker_get_error_buffer());
Mingwei Shibe910522015-11-12 07:02:14 +00004459}
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004460
Nick Kralevich468319c2011-11-11 15:53:17 -08004461/*
4462 * This is the entry point for the linker, called from begin.S. This
4463 * method is responsible for fixing the linker's own relocations, and
4464 * then calling __linker_init_post_relocation().
4465 *
4466 * Because this method is called before the linker has fixed it's own
4467 * relocations, any attempt to reference an extern variable, extern
4468 * function, or other GOT reference will generate a segfault.
4469 */
Elliott Hughes0266ae52014-02-10 17:46:57 -08004470extern "C" ElfW(Addr) __linker_init(void* raw_args) {
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004471 KernelArgumentBlock args(raw_args);
Nick Kralevich468319c2011-11-11 15:53:17 -08004472
Dimitry Ivanov55437462016-07-20 15:33:07 -07004473 g_argc = args.argc;
4474 g_argv = args.argv;
4475 g_envp = args.envp;
Dimitry Ivanovb996d602016-07-11 18:11:39 -07004476
Elliott Hughes0266ae52014-02-10 17:46:57 -08004477 ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004478 ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
Elliott Hughes0266ae52014-02-10 17:46:57 -08004479 ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
Elliott Hughesfaf05ba2014-02-11 16:59:37 -08004480 ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
Nick Kralevich468319c2011-11-11 15:53:17 -08004481
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -07004482 soinfo linker_so(nullptr, nullptr, nullptr, 0, 0);
Nick Kralevich468319c2011-11-11 15:53:17 -08004483
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004484 // If the linker is not acting as PT_INTERP entry_point is equal to
4485 // _start. Which means that the linker is running as an executable and
4486 // already linked by PT_INTERP.
4487 //
4488 // This happens when user tries to run 'adb shell /system/bin/linker'
4489 // see also https://code.google.com/p/android/issues/detail?id=63174
4490 if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004491 __libc_format_fd(STDOUT_FILENO,
4492 "This is %s, the helper program for shared library executables.\n",
Dimitry Ivanov55437462016-07-20 15:33:07 -07004493 g_argv[0]);
Dimitry Ivanov9f0a6952016-02-18 14:37:44 -08004494 exit(0);
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004495 }
4496
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004497 linker_so.base = linker_addr;
4498 linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
4499 linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
Dmitriy Ivanov851135b2014-08-29 12:02:36 -07004500 linker_so.dynamic = nullptr;
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004501 linker_so.phdr = phdr;
4502 linker_so.phnum = elf_hdr->e_phnum;
Dmitriy Ivanovab972b92014-11-29 13:57:41 -08004503 linker_so.set_linker_flag();
Elliott Hughes5419b942012-10-16 15:54:46 -07004504
Mingwei Shibe910522015-11-12 07:02:14 +00004505 // Prelink the linker so we can access linker globals.
Dimitry Ivanovb996d602016-07-11 18:11:39 -07004506 if (!linker_so.prelink_image()) __linker_cannot_link();
Mingwei Shibe910522015-11-12 07:02:14 +00004507
Dmitriy Ivanovd225a5e2014-08-28 14:12:12 -07004508 // This might not be obvious... The reasons why we pass g_empty_list
4509 // in place of local_group here are (1) we do not really need it, because
4510 // linker is built with DT_SYMBOLIC and therefore relocates its symbols against
4511 // itself without having to look into local_group and (2) allocators
4512 // are not yet initialized, and therefore we cannot use linked_list.push_*
4513 // functions at this point.
Dimitry Ivanovb996d602016-07-11 18:11:39 -07004514 if (!linker_so.link_image(g_empty_list, g_empty_list, nullptr)) __linker_cannot_link();
Elliott Hughesd23736e2012-11-01 15:16:56 -07004515
Mingwei Shibe910522015-11-12 07:02:14 +00004516#if defined(__i386__)
4517 // On x86, we can't make system calls before this point.
4518 // We can't move this up because this needs to assign to a global.
4519 // Note that until we call __libc_init_main_thread below we have
4520 // no TLS, so you shouldn't make a system call that can fail, because
4521 // it will SEGV when it tries to set errno.
4522 __libc_init_sysinfo(args);
4523#endif
4524
4525 // Initialize the main thread (including TLS, so system calls really work).
Elliott Hughesd2948632015-07-21 11:57:09 -07004526 __libc_init_main_thread(args);
Dmitriy Ivanov14241402014-08-26 14:16:52 -07004527
Mingwei Shibe910522015-11-12 07:02:14 +00004528 // We didn't protect the linker's RELRO pages in link_image because we
4529 // couldn't make system calls on x86 at that point, but we can now...
Dimitry Ivanovb996d602016-07-11 18:11:39 -07004530 if (!linker_so.protect_relro()) __linker_cannot_link();
Mingwei Shibe910522015-11-12 07:02:14 +00004531
Josh Gao93c0f5e2015-10-06 11:08:13 -07004532 // Initialize the linker's static libc's globals
4533 __libc_init_globals(args);
4534
Dmitriy Ivanovefe13832014-07-28 15:05:51 -07004535 // Initialize the linker's own global variables
Dmitriy Ivanov047b5932014-11-13 09:39:20 -08004536 linker_so.call_constructors();
Dmitriy Ivanov4151ea72014-07-24 15:33:25 -07004537
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004538 // Initialize static variables. Note that in order to
4539 // get correct libdl_info we need to call constructors
4540 // before get_libdl_info().
4541 solist = get_libdl_info();
4542 sonext = get_libdl_info();
Dimitry Ivanovd88e1f32016-03-24 15:30:30 -07004543 g_default_namespace.add_soinfo(get_libdl_info());
Dmitriy Ivanov0d150942014-08-22 12:25:04 -07004544
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004545 // We have successfully fixed our own relocations. It's safe to run
4546 // the main part of the linker now.
Elliott Hughes1728b232014-05-14 10:02:03 -07004547 args.abort_message_ptr = &g_abort_message;
Elliott Hughes0266ae52014-02-10 17:46:57 -08004548 ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004549
Elliott Hughes116b5692016-01-04 17:45:36 -08004550 INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address));
Elliott Hughes611f9562015-01-23 10:43:58 -08004551
Elliott Hughes42b2c6a2013-02-07 10:14:39 -08004552 // Return the address that the calling assembly stub should jump to.
4553 return start_address;
Nick Kralevich468319c2011-11-11 15:53:17 -08004554}