| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1 | /* | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2 |  * Copyright (C) 2008 The Android Open Source Project | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3 |  * 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 Ivanov | 1913352 | 2015-06-02 17:36:54 -0700 | [diff] [blame] | 29 | #include <android/api-level.h> | 
| Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 30 | #include <errno.h> | 
 | 31 | #include <fcntl.h> | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 32 | #include <inttypes.h> | 
| Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 33 | #include <pthread.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 34 | #include <stdio.h> | 
 | 35 | #include <stdlib.h> | 
 | 36 | #include <string.h> | 
| Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 37 | #include <sys/mman.h> | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 38 | #include <sys/param.h> | 
| Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 39 | #include <unistd.h> | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 40 |  | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 41 | #include <new> | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 42 | #include <string> | 
| Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 43 | #include <unordered_map> | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 44 | #include <vector> | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 45 |  | 
| Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 46 | // Private C library headers. | 
| Elliott Hughes | eb847bc | 2013-10-09 15:50:50 -0700 | [diff] [blame] | 47 | #include "private/bionic_tls.h" | 
 | 48 | #include "private/KernelArgumentBlock.h" | 
 | 49 | #include "private/ScopedPthreadMutexLocker.h" | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 50 | #include "private/ScopeGuard.h" | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 51 |  | 
 | 52 | #include "linker.h" | 
| Dmitriy Ivanov | c9ce70d | 2015-03-10 15:30:26 -0700 | [diff] [blame] | 53 | #include "linker_block_allocator.h" | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 54 | #include "linker_debug.h" | 
| Dmitriy Ivanov | 18870d3 | 2015-04-22 13:10:04 -0700 | [diff] [blame] | 55 | #include "linker_sleb128.h" | 
| David 'Digit' Turner | 23363ed | 2012-06-18 18:13:49 +0200 | [diff] [blame] | 56 | #include "linker_phdr.h" | 
| Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 57 | #include "linker_relocs.h" | 
| Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 58 | #include "linker_reloc_iterators.h" | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 59 | #include "linker_utils.h" | 
| tony.ys_liu | b447440 | 2015-07-29 18:00:22 +0800 | [diff] [blame] | 60 |  | 
| Elliott Hughes | 939a7e0 | 2015-12-04 15:27:46 -0800 | [diff] [blame] | 61 | #include "android-base/strings.h" | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 62 | #include "ziparchive/zip_archive.h" | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 63 |  | 
| Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 64 | extern void __libc_init_globals(KernelArgumentBlock&); | 
| Elliott Hughes | 1801db3 | 2015-06-08 18:04:00 -0700 | [diff] [blame] | 65 | extern void __libc_init_AT_SECURE(KernelArgumentBlock&); | 
 | 66 |  | 
 | 67 | // Override macros to use C++ style casts. | 
| Dmitriy Ivanov | 1649e7e | 2015-01-22 16:04:25 -0800 | [diff] [blame] | 68 | #undef ELF_ST_TYPE | 
 | 69 | #define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf) | 
 | 70 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 71 | struct android_namespace_t { | 
 | 72 |  public: | 
 | 73 |   android_namespace_t() : name_(nullptr), is_isolated_(false) {} | 
 | 74 |  | 
 | 75 |   const char* get_name() const { return name_; } | 
 | 76 |   void set_name(const char* name) { name_ = name; } | 
 | 77 |  | 
 | 78 |   bool is_isolated() const { return is_isolated_; } | 
 | 79 |   void set_isolated(bool isolated) { is_isolated_ = isolated; } | 
 | 80 |  | 
 | 81 |   const std::vector<std::string>& get_ld_library_paths() const { | 
 | 82 |     return ld_library_paths_; | 
 | 83 |   } | 
 | 84 |   void set_ld_library_paths(std::vector<std::string>&& library_paths) { | 
 | 85 |     ld_library_paths_ = library_paths; | 
 | 86 |   } | 
 | 87 |  | 
 | 88 |   const std::vector<std::string>& get_default_library_paths() const { | 
 | 89 |     return default_library_paths_; | 
 | 90 |   } | 
 | 91 |   void set_default_library_paths(std::vector<std::string>&& library_paths) { | 
 | 92 |     default_library_paths_ = library_paths; | 
 | 93 |   } | 
 | 94 |  | 
| Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 95 |   void set_permitted_paths(std::vector<std::string>&& permitted_paths) { | 
 | 96 |     permitted_paths_ = permitted_paths; | 
 | 97 |   } | 
 | 98 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 99 |   soinfo::soinfo_list_t& soinfo_list() { return soinfo_list_; } | 
 | 100 |  | 
 | 101 |   // For isolated namespaces - checks if the file is on the search path; | 
 | 102 |   // always returns true for not isolated namespace. | 
 | 103 |   bool is_accessible(const std::string& path); | 
 | 104 |  | 
 | 105 |  private: | 
 | 106 |   const char* name_; | 
 | 107 |   bool is_isolated_; | 
 | 108 |   std::vector<std::string> ld_library_paths_; | 
 | 109 |   std::vector<std::string> default_library_paths_; | 
| Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 110 |   std::vector<std::string> permitted_paths_; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 111 |   soinfo::soinfo_list_t soinfo_list_; | 
 | 112 |  | 
 | 113 |   DISALLOW_COPY_AND_ASSIGN(android_namespace_t); | 
 | 114 | }; | 
 | 115 |  | 
 | 116 | android_namespace_t g_default_namespace; | 
| Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 117 | android_namespace_t* g_anonymous_namespace = &g_default_namespace; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 118 |  | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 119 | static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 120 |  | 
| Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 121 | static LinkerTypeAllocator<soinfo> g_soinfo_allocator; | 
 | 122 | static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator; | 
| Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 123 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 124 | static LinkerTypeAllocator<android_namespace_t> g_namespace_allocator; | 
 | 125 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 126 | static soinfo* solist; | 
 | 127 | static soinfo* sonext; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 128 | static soinfo* somain; // main process, always the one after libdl_info | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 129 |  | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 130 | static const char* const kDefaultLdPaths[] = { | 
| Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 131 | #if defined(__LP64__) | 
| Elliott Hughes | 011bc0b | 2013-10-08 14:27:10 -0700 | [diff] [blame] | 132 |   "/system/lib64", | 
| Dimitry Ivanov | 88f5111 | 2016-02-01 23:00:55 -0800 | [diff] [blame] | 133 |   "/vendor/lib64", | 
| Elliott Hughes | 011bc0b | 2013-10-08 14:27:10 -0700 | [diff] [blame] | 134 | #else | 
| Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 135 |   "/system/lib", | 
| Dimitry Ivanov | 88f5111 | 2016-02-01 23:00:55 -0800 | [diff] [blame] | 136 |   "/vendor/lib", | 
| Elliott Hughes | 011bc0b | 2013-10-08 14:27:10 -0700 | [diff] [blame] | 137 | #endif | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 138 |   nullptr | 
| Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 139 | }; | 
| David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 140 |  | 
| Evgenii Stepanov | d640b22 | 2015-07-10 17:54:01 -0700 | [diff] [blame] | 141 | static const char* const kAsanDefaultLdPaths[] = { | 
 | 142 | #if defined(__LP64__) | 
| Evgenii Stepanov | d640b22 | 2015-07-10 17:54:01 -0700 | [diff] [blame] | 143 |   "/data/lib64", | 
 | 144 |   "/system/lib64", | 
| Dimitry Ivanov | 88f5111 | 2016-02-01 23:00:55 -0800 | [diff] [blame] | 145 |   "/data/vendor/lib64", | 
 | 146 |   "/vendor/lib64", | 
| Evgenii Stepanov | d640b22 | 2015-07-10 17:54:01 -0700 | [diff] [blame] | 147 | #else | 
| Evgenii Stepanov | d640b22 | 2015-07-10 17:54:01 -0700 | [diff] [blame] | 148 |   "/data/lib", | 
 | 149 |   "/system/lib", | 
| Dimitry Ivanov | 88f5111 | 2016-02-01 23:00:55 -0800 | [diff] [blame] | 150 |   "/data/vendor/lib", | 
 | 151 |   "/vendor/lib", | 
| Evgenii Stepanov | d640b22 | 2015-07-10 17:54:01 -0700 | [diff] [blame] | 152 | #endif | 
 | 153 |   nullptr | 
 | 154 | }; | 
 | 155 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 156 | static const ElfW(Versym) kVersymNotNeeded = 0; | 
 | 157 | static const ElfW(Versym) kVersymGlobal = 1; | 
 | 158 |  | 
| Evgenii Stepanov | d640b22 | 2015-07-10 17:54:01 -0700 | [diff] [blame] | 159 | static const char* const* g_default_ld_paths; | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 160 | static std::vector<std::string> g_ld_preload_names; | 
| Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 161 |  | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 162 | static std::vector<soinfo*> g_ld_preloads; | 
| Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 163 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 164 | static bool g_public_namespace_initialized; | 
 | 165 | static soinfo::soinfo_list_t g_public_namespace; | 
 | 166 |  | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 167 | __LIBC_HIDDEN__ int g_ld_debug_verbosity; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 168 |  | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 169 | __LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd. | 
| Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 170 |  | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 171 | static std::string dirname(const char *path) { | 
 | 172 |   const char* last_slash = strrchr(path, '/'); | 
 | 173 |   if (last_slash == path) return "/"; | 
 | 174 |   else if (last_slash == nullptr) return "."; | 
 | 175 |   else | 
 | 176 |     return std::string(path, last_slash - path); | 
 | 177 | } | 
 | 178 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 179 | #if STATS | 
| Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 180 | struct linker_stats_t { | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 181 |   int count[kRelocMax]; | 
| Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 182 | }; | 
 | 183 |  | 
 | 184 | static linker_stats_t linker_stats; | 
 | 185 |  | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 186 | void count_relocation(RelocationKind kind) { | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 187 |   ++linker_stats.count[kind]; | 
| Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 188 | } | 
 | 189 | #else | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 190 | void count_relocation(RelocationKind) { | 
| Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 191 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 192 | #endif | 
 | 193 |  | 
 | 194 | #if COUNT_PAGES | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 195 | uint32_t bitmask[4096]; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 196 | #endif | 
 | 197 |  | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 198 | static char __linker_dl_err_buf[768]; | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 199 |  | 
| Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 200 | char* linker_get_error_buffer() { | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 201 |   return &__linker_dl_err_buf[0]; | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 202 | } | 
 | 203 |  | 
| Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 204 | size_t linker_get_error_buffer_size() { | 
 | 205 |   return sizeof(__linker_dl_err_buf); | 
 | 206 | } | 
 | 207 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 208 | // This function is an empty stub where GDB locates a breakpoint to get notified | 
 | 209 | // about linker activity. | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 210 | extern "C" | 
 | 211 | void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity(); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 212 |  | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 213 | static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER; | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 214 | static r_debug _r_debug = | 
 | 215 |     {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0}; | 
 | 216 |  | 
| Elliott Hughes | 3a9c5d6 | 2014-02-10 13:31:13 -0800 | [diff] [blame] | 217 | static link_map* r_debug_tail = 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 218 |  | 
| Elliott Hughes | 3a9c5d6 | 2014-02-10 13:31:13 -0800 | [diff] [blame] | 219 | static void insert_soinfo_into_debug_map(soinfo* info) { | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 220 |   // Copy the necessary fields into the debug structure. | 
 | 221 |   link_map* map = &(info->link_map_head); | 
 | 222 |   map->l_addr = info->load_bias; | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 223 |   // link_map l_name field is not const. | 
 | 224 |   map->l_name = const_cast<char*>(info->get_realpath()); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 225 |   map->l_ld = info->dynamic; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 226 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 227 |   // Stick the new library at the end of the list. | 
 | 228 |   // gdb tends to care more about libc than it does | 
 | 229 |   // about leaf libraries, and ordering it this way | 
 | 230 |   // reduces the back-and-forth over the wire. | 
 | 231 |   if (r_debug_tail) { | 
 | 232 |     r_debug_tail->l_next = map; | 
 | 233 |     map->l_prev = r_debug_tail; | 
 | 234 |     map->l_next = 0; | 
 | 235 |   } else { | 
 | 236 |     _r_debug.r_map = map; | 
 | 237 |     map->l_prev = 0; | 
 | 238 |     map->l_next = 0; | 
 | 239 |   } | 
 | 240 |   r_debug_tail = map; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 241 | } | 
 | 242 |  | 
| Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 243 | static void remove_soinfo_from_debug_map(soinfo* info) { | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 244 |   link_map* map = &(info->link_map_head); | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 245 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 246 |   if (r_debug_tail == map) { | 
 | 247 |     r_debug_tail = map->l_prev; | 
 | 248 |   } | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 249 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 250 |   if (map->l_prev) { | 
 | 251 |     map->l_prev->l_next = map->l_next; | 
 | 252 |   } | 
 | 253 |   if (map->l_next) { | 
 | 254 |     map->l_next->l_prev = map->l_prev; | 
 | 255 |   } | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 256 | } | 
 | 257 |  | 
| Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 258 | static void notify_gdb_of_load(soinfo* info) { | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 259 |   if (info->is_main_executable()) { | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 260 |     // GDB already knows about the main executable | 
 | 261 |     return; | 
 | 262 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 263 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 264 |   ScopedPthreadMutexLocker locker(&g__r_debug_mutex); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 265 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 266 |   _r_debug.r_state = r_debug::RT_ADD; | 
 | 267 |   rtld_db_dlactivity(); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 268 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 269 |   insert_soinfo_into_debug_map(info); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 270 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 271 |   _r_debug.r_state = r_debug::RT_CONSISTENT; | 
 | 272 |   rtld_db_dlactivity(); | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 273 | } | 
 | 274 |  | 
| Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 275 | static void notify_gdb_of_unload(soinfo* info) { | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 276 |   if (info->is_main_executable()) { | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 277 |     // GDB already knows about the main executable | 
 | 278 |     return; | 
 | 279 |   } | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 280 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 281 |   ScopedPthreadMutexLocker locker(&g__r_debug_mutex); | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 282 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 283 |   _r_debug.r_state = r_debug::RT_DELETE; | 
 | 284 |   rtld_db_dlactivity(); | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 285 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 286 |   remove_soinfo_from_debug_map(info); | 
| Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 287 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 288 |   _r_debug.r_state = r_debug::RT_CONSISTENT; | 
 | 289 |   rtld_db_dlactivity(); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 290 | } | 
 | 291 |  | 
| Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 292 | void notify_gdb_of_libraries() { | 
| Elliott Hughes | 3a9c5d6 | 2014-02-10 13:31:13 -0800 | [diff] [blame] | 293 |   _r_debug.r_state = r_debug::RT_ADD; | 
 | 294 |   rtld_db_dlactivity(); | 
 | 295 |   _r_debug.r_state = r_debug::RT_CONSISTENT; | 
 | 296 |   rtld_db_dlactivity(); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 297 | } | 
 | 298 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 299 | bool android_namespace_t::is_accessible(const std::string& file) { | 
 | 300 |   if (!is_isolated_) { | 
 | 301 |     return true; | 
 | 302 |   } | 
 | 303 |  | 
 | 304 |   for (const auto& dir : ld_library_paths_) { | 
 | 305 |     if (file_is_in_dir(file, dir)) { | 
 | 306 |       return true; | 
 | 307 |     } | 
 | 308 |   } | 
 | 309 |  | 
 | 310 |   for (const auto& dir : default_library_paths_) { | 
 | 311 |     if (file_is_in_dir(file, dir)) { | 
 | 312 |       return true; | 
 | 313 |     } | 
 | 314 |   } | 
 | 315 |  | 
| Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 316 |   for (const auto& dir : permitted_paths_) { | 
 | 317 |     if (file_is_under_dir(file, dir)) { | 
 | 318 |       return true; | 
 | 319 |     } | 
 | 320 |   } | 
 | 321 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 322 |   return false; | 
 | 323 | } | 
 | 324 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 325 | LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() { | 
 | 326 |   return g_soinfo_links_allocator.alloc(); | 
 | 327 | } | 
 | 328 |  | 
 | 329 | void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) { | 
 | 330 |   g_soinfo_links_allocator.free(entry); | 
 | 331 | } | 
 | 332 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 333 | static soinfo* soinfo_alloc(android_namespace_t* ns, const char* name, | 
 | 334 |                             struct stat* file_stat, off64_t file_offset, | 
 | 335 |                             uint32_t rtld_flags) { | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 336 |   if (strlen(name) >= PATH_MAX) { | 
| Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 337 |     DL_ERR("library name \"%s\" too long", name); | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 338 |     return nullptr; | 
| Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 339 |   } | 
 | 340 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 341 |   soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(ns, name, file_stat, | 
 | 342 |                                                        file_offset, rtld_flags); | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 343 |  | 
| Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 344 |   sonext->next = si; | 
 | 345 |   sonext = si; | 
 | 346 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 347 |   ns->soinfo_list().push_back(si); | 
 | 348 |  | 
| Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 349 |   TRACE("name %s: allocated soinfo @ %p", name, si); | 
| Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 350 |   return si; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 351 | } | 
 | 352 |  | 
| Elliott Hughes | faf05ba | 2014-02-11 16:59:37 -0800 | [diff] [blame] | 353 | static void soinfo_free(soinfo* si) { | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 354 |   if (si == nullptr) { | 
 | 355 |     return; | 
 | 356 |   } | 
 | 357 |  | 
 | 358 |   if (si->base != 0 && si->size != 0) { | 
| Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 359 |     if (!si->is_mapped_by_caller()) { | 
 | 360 |       munmap(reinterpret_cast<void*>(si->base), si->size); | 
 | 361 |     } else { | 
 | 362 |       // remap the region as PROT_NONE, MAP_ANONYMOUS | MAP_NORESERVE | 
 | 363 |       mmap(reinterpret_cast<void*>(si->base), si->size, PROT_NONE, | 
 | 364 |            MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS | MAP_NORESERVE, -1, 0); | 
 | 365 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 366 |   } | 
 | 367 |  | 
 | 368 |   soinfo *prev = nullptr, *trav; | 
 | 369 |  | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 370 |   TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 371 |  | 
 | 372 |   for (trav = solist; trav != nullptr; trav = trav->next) { | 
 | 373 |     if (trav == si) { | 
 | 374 |       break; | 
| Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 375 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 376 |     prev = trav; | 
 | 377 |   } | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 378 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 379 |   if (trav == nullptr) { | 
 | 380 |     // si was not in solist | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 381 |     DL_ERR("name \"%s\"@%p is not in solist!", si->get_realpath(), si); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 382 |     return; | 
 | 383 |   } | 
| Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 384 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 385 |   // clear links to/from si | 
 | 386 |   si->remove_all_links(); | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 387 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 388 |   // prev will never be null, because the first entry in solist is | 
 | 389 |   // always the static libdl_info. | 
 | 390 |   prev->next = si->next; | 
 | 391 |   if (si == sonext) { | 
 | 392 |     sonext = prev; | 
 | 393 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 394 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 395 |   // remove from the namespace | 
 | 396 |   si->get_namespace()->soinfo_list().remove_if([&](soinfo* candidate) { | 
 | 397 |     return si == candidate; | 
 | 398 |   }); | 
 | 399 |  | 
| Dmitriy Ivanov | 609f11b | 2015-07-08 15:26:46 -0700 | [diff] [blame] | 400 |   si->~soinfo(); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 401 |   g_soinfo_allocator.free(si); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 402 | } | 
 | 403 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 404 | // For every path element this function checks of it exists, and is a directory, | 
 | 405 | // and normalizes it: | 
 | 406 | // 1. For regular path it converts it to realpath() | 
 | 407 | // 2. For path in a zip file it uses realpath on the zipfile | 
 | 408 | //    normalizes entry name by calling normalize_path function. | 
 | 409 | static void resolve_paths(std::vector<std::string>& paths, | 
 | 410 |                           std::vector<std::string>* resolved_paths) { | 
 | 411 |   resolved_paths->clear(); | 
 | 412 |   for (const auto& path : paths) { | 
 | 413 |     char resolved_path[PATH_MAX]; | 
 | 414 |     const char* original_path = path.c_str(); | 
 | 415 |     if (realpath(original_path, resolved_path) != nullptr) { | 
 | 416 |       struct stat s; | 
 | 417 |       if (stat(resolved_path, &s) == 0) { | 
 | 418 |         if (S_ISDIR(s.st_mode)) { | 
 | 419 |           resolved_paths->push_back(resolved_path); | 
 | 420 |         } else { | 
 | 421 |           DL_WARN("Warning: \"%s\" is not a directory (excluding from path)", resolved_path); | 
 | 422 |           continue; | 
 | 423 |         } | 
 | 424 |       } else { | 
 | 425 |         DL_WARN("Warning: cannot stat file \"%s\": %s", resolved_path, strerror(errno)); | 
 | 426 |         continue; | 
 | 427 |       } | 
 | 428 |     } else { | 
 | 429 |       std::string zip_path; | 
 | 430 |       std::string entry_path; | 
 | 431 |  | 
 | 432 |       std::string normalized_path; | 
 | 433 |  | 
 | 434 |       if (!normalize_path(original_path, &normalized_path)) { | 
 | 435 |         DL_WARN("Warning: unable to normalize \"%s\"", original_path); | 
 | 436 |         continue; | 
 | 437 |       } | 
 | 438 |  | 
 | 439 |       if (parse_zip_path(normalized_path.c_str(), &zip_path, &entry_path)) { | 
 | 440 |         if (realpath(zip_path.c_str(), resolved_path) == nullptr) { | 
 | 441 |           DL_WARN("Warning: unable to resolve \"%s\": %s", zip_path.c_str(), strerror(errno)); | 
 | 442 |           continue; | 
 | 443 |         } | 
 | 444 |  | 
 | 445 |         ZipArchiveHandle handle = nullptr; | 
 | 446 |         if (OpenArchive(resolved_path, &handle) != 0) { | 
 | 447 |           DL_WARN("Warning: unable to open zip archive: %s", resolved_path); | 
 | 448 |           continue; | 
 | 449 |         } | 
 | 450 |  | 
 | 451 |         // Check if zip-file has a dir with entry_path name | 
 | 452 |         void* cookie = nullptr; | 
 | 453 |         std::string prefix_str = entry_path + "/"; | 
 | 454 |         ZipString prefix(prefix_str.c_str()); | 
 | 455 |  | 
 | 456 |         ZipEntry out_data; | 
 | 457 |         ZipString out_name; | 
 | 458 |  | 
 | 459 |         int32_t error_code; | 
 | 460 |  | 
 | 461 |         if ((error_code = StartIteration(handle, &cookie, &prefix, nullptr)) != 0) { | 
 | 462 |           DL_WARN("Unable to iterate over zip-archive entries \"%s\";" | 
 | 463 |                   " error code: %d", zip_path.c_str(), error_code); | 
 | 464 |           continue; | 
 | 465 |         } | 
 | 466 |  | 
 | 467 |         if (Next(cookie, &out_data, &out_name) != 0) { | 
 | 468 |           DL_WARN("Unable to find entries starting with \"%s\" in \"%s\"", | 
 | 469 |                   prefix_str.c_str(), zip_path.c_str()); | 
 | 470 |           continue; | 
 | 471 |         } | 
 | 472 |  | 
 | 473 |         auto zip_guard = make_scope_guard([&]() { | 
 | 474 |           if (cookie != nullptr) { | 
 | 475 |             EndIteration(cookie); | 
 | 476 |           } | 
 | 477 |           CloseArchive(handle); | 
 | 478 |         }); | 
 | 479 |  | 
 | 480 |         resolved_paths->push_back(std::string(resolved_path) + kZipFileSeparator + entry_path); | 
 | 481 |       } | 
 | 482 |     } | 
 | 483 |   } | 
 | 484 | } | 
 | 485 |  | 
 | 486 | static void split_path(const char* path, const char* delimiters, | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 487 |                        std::vector<std::string>* paths) { | 
| Dmitriy Ivanov | fbfba64 | 2015-11-16 14:23:37 -0800 | [diff] [blame] | 488 |   if (path != nullptr && path[0] != 0) { | 
| tony.ys_liu | b447440 | 2015-07-29 18:00:22 +0800 | [diff] [blame] | 489 |     *paths = android::base::Split(path, delimiters); | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 490 |   } | 
 | 491 | } | 
 | 492 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 493 | static void parse_path(const char* path, const char* delimiters, | 
 | 494 |                        std::vector<std::string>* resolved_paths) { | 
 | 495 |   std::vector<std::string> paths; | 
 | 496 |   split_path(path, delimiters, &paths); | 
 | 497 |   resolve_paths(paths, resolved_paths); | 
 | 498 | } | 
 | 499 |  | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 500 | static void parse_LD_LIBRARY_PATH(const char* path) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 501 |   std::vector<std::string> ld_libary_paths; | 
 | 502 |   parse_path(path, ":", &ld_libary_paths); | 
 | 503 |   g_default_namespace.set_ld_library_paths(std::move(ld_libary_paths)); | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 504 | } | 
 | 505 |  | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 506 | void soinfo::set_dt_runpath(const char* path) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 507 |   if (!has_min_version(3)) { | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 508 |     return; | 
 | 509 |   } | 
 | 510 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 511 |   std::vector<std::string> runpaths; | 
 | 512 |  | 
 | 513 |   split_path(path, ":", &runpaths); | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 514 |  | 
 | 515 |   std::string origin = dirname(get_realpath()); | 
 | 516 |   // FIXME: add $LIB and $PLATFORM. | 
 | 517 |   std::pair<std::string, std::string> substs[] = {{"ORIGIN", origin}}; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 518 |   for (auto&& s : runpaths) { | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 519 |     size_t pos = 0; | 
 | 520 |     while (pos < s.size()) { | 
 | 521 |       pos = s.find("$", pos); | 
 | 522 |       if (pos == std::string::npos) break; | 
 | 523 |       for (const auto& subst : substs) { | 
 | 524 |         const std::string& token = subst.first; | 
 | 525 |         const std::string& replacement = subst.second; | 
 | 526 |         if (s.substr(pos + 1, token.size()) == token) { | 
 | 527 |           s.replace(pos, token.size() + 1, replacement); | 
 | 528 |           // -1 to compensate for the ++pos below. | 
 | 529 |           pos += replacement.size() - 1; | 
 | 530 |           break; | 
 | 531 |         } else if (s.substr(pos + 1, token.size() + 2) == "{" + token + "}") { | 
 | 532 |           s.replace(pos, token.size() + 3, replacement); | 
 | 533 |           pos += replacement.size() - 1; | 
 | 534 |           break; | 
 | 535 |         } | 
 | 536 |       } | 
 | 537 |       // Skip $ in case it did not match any of the known substitutions. | 
 | 538 |       ++pos; | 
 | 539 |     } | 
 | 540 |   } | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 541 |  | 
 | 542 |   resolve_paths(runpaths, &dt_runpath_); | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 543 | } | 
 | 544 |  | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 545 | static void parse_LD_PRELOAD(const char* path) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 546 |   g_ld_preload_names.clear(); | 
 | 547 |   if (path != nullptr) { | 
 | 548 |     // We have historically supported ':' as well as ' ' in LD_PRELOAD. | 
 | 549 |     g_ld_preload_names = android::base::Split(path, " :"); | 
 | 550 |   } | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 551 | } | 
 | 552 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 553 | static bool realpath_fd(int fd, std::string* realpath) { | 
 | 554 |   std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX); | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 555 |   __libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd); | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 556 |   if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) { | 
| Dmitriy Ivanov | 087005f | 2015-05-28 11:44:31 -0700 | [diff] [blame] | 557 |     PRINT("readlink('%s') failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd); | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 558 |     return false; | 
 | 559 |   } | 
 | 560 |  | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 561 |   *realpath = &buf[0]; | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 562 |   return true; | 
 | 563 | } | 
 | 564 |  | 
| Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 565 | #if defined(__arm__) | 
| Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 566 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 567 | // For a given PC, find the .so that it belongs to. | 
 | 568 | // Returns the base address of the .ARM.exidx section | 
 | 569 | // for that .so, and the number of 8-byte entries | 
 | 570 | // in that section (via *pcount). | 
 | 571 | // | 
 | 572 | // Intended to be called by libc's __gnu_Unwind_Find_exidx(). | 
 | 573 | // | 
 | 574 | // This function is exposed via dlfcn.cpp and libdl.so. | 
| Elliott Hughes | faf05ba | 2014-02-11 16:59:37 -0800 | [diff] [blame] | 575 | _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) { | 
| Dmitriy Ivanov | 1649e7e | 2015-01-22 16:04:25 -0800 | [diff] [blame] | 576 |   uintptr_t addr = reinterpret_cast<uintptr_t>(pc); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 577 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 578 |   for (soinfo* si = solist; si != 0; si = si->next) { | 
 | 579 |     if ((addr >= si->base) && (addr < (si->base + si->size))) { | 
 | 580 |         *pcount = si->ARM_exidx_count; | 
| Dmitriy Ivanov | 1649e7e | 2015-01-22 16:04:25 -0800 | [diff] [blame] | 581 |         return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 582 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 583 |   } | 
 | 584 |   *pcount = 0; | 
 | 585 |   return nullptr; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 586 | } | 
| Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 587 |  | 
| Christopher Ferris | 24053a4 | 2013-08-19 17:45:09 -0700 | [diff] [blame] | 588 | #endif | 
| Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 589 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 590 | // Here, we only have to provide a callback to iterate across all the | 
 | 591 | // loaded libraries. gcc_eh does the rest. | 
| Dmitriy Ivanov | 7271caf | 2015-06-29 14:48:25 -0700 | [diff] [blame] | 592 | int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) { | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 593 |   int rv = 0; | 
 | 594 |   for (soinfo* si = solist; si != nullptr; si = si->next) { | 
 | 595 |     dl_phdr_info dl_info; | 
 | 596 |     dl_info.dlpi_addr = si->link_map_head.l_addr; | 
 | 597 |     dl_info.dlpi_name = si->link_map_head.l_name; | 
 | 598 |     dl_info.dlpi_phdr = si->phdr; | 
 | 599 |     dl_info.dlpi_phnum = si->phnum; | 
 | 600 |     rv = cb(&dl_info, sizeof(dl_phdr_info), data); | 
 | 601 |     if (rv != 0) { | 
 | 602 |       break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 603 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 604 |   } | 
 | 605 |   return rv; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 606 | } | 
| Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 607 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 608 | const ElfW(Versym)* soinfo::get_versym(size_t n) const { | 
 | 609 |   if (has_min_version(2) && versym_ != nullptr) { | 
 | 610 |     return versym_ + n; | 
 | 611 |   } | 
 | 612 |  | 
 | 613 |   return nullptr; | 
 | 614 | } | 
 | 615 |  | 
 | 616 | ElfW(Addr) soinfo::get_verneed_ptr() const { | 
 | 617 |   if (has_min_version(2)) { | 
 | 618 |     return verneed_ptr_; | 
 | 619 |   } | 
 | 620 |  | 
 | 621 |   return 0; | 
 | 622 | } | 
 | 623 |  | 
 | 624 | size_t soinfo::get_verneed_cnt() const { | 
 | 625 |   if (has_min_version(2)) { | 
 | 626 |     return verneed_cnt_; | 
 | 627 |   } | 
 | 628 |  | 
 | 629 |   return 0; | 
 | 630 | } | 
 | 631 |  | 
 | 632 | ElfW(Addr) soinfo::get_verdef_ptr() const { | 
 | 633 |   if (has_min_version(2)) { | 
 | 634 |     return verdef_ptr_; | 
 | 635 |   } | 
 | 636 |  | 
 | 637 |   return 0; | 
 | 638 | } | 
 | 639 |  | 
 | 640 | size_t soinfo::get_verdef_cnt() const { | 
 | 641 |   if (has_min_version(2)) { | 
 | 642 |     return verdef_cnt_; | 
 | 643 |   } | 
 | 644 |  | 
 | 645 |   return 0; | 
 | 646 | } | 
 | 647 |  | 
 | 648 | template<typename F> | 
 | 649 | static bool for_each_verdef(const soinfo* si, F functor) { | 
 | 650 |   if (!si->has_min_version(2)) { | 
 | 651 |     return true; | 
 | 652 |   } | 
 | 653 |  | 
 | 654 |   uintptr_t verdef_ptr = si->get_verdef_ptr(); | 
 | 655 |   if (verdef_ptr == 0) { | 
 | 656 |     return true; | 
 | 657 |   } | 
 | 658 |  | 
 | 659 |   size_t offset = 0; | 
 | 660 |  | 
 | 661 |   size_t verdef_cnt = si->get_verdef_cnt(); | 
 | 662 |   for (size_t i = 0; i<verdef_cnt; ++i) { | 
 | 663 |     const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset); | 
 | 664 |     size_t verdaux_offset = offset + verdef->vd_aux; | 
 | 665 |     offset += verdef->vd_next; | 
 | 666 |  | 
 | 667 |     if (verdef->vd_version != 1) { | 
| Dmitriy Ivanov | 3d7bea1 | 2015-04-20 17:40:39 -0700 | [diff] [blame] | 668 |       DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 669 |           i, verdef->vd_version, si->get_realpath()); | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 670 |       return false; | 
 | 671 |     } | 
 | 672 |  | 
 | 673 |     if ((verdef->vd_flags & VER_FLG_BASE) != 0) { | 
 | 674 |       // "this is the version of the file itself.  It must not be used for | 
 | 675 |       //  matching a symbol. It can be used to match references." | 
 | 676 |       // | 
 | 677 |       // http://www.akkadia.org/drepper/symbol-versioning | 
 | 678 |       continue; | 
 | 679 |     } | 
 | 680 |  | 
 | 681 |     if (verdef->vd_cnt == 0) { | 
 | 682 |       DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i); | 
 | 683 |       return false; | 
 | 684 |     } | 
 | 685 |  | 
 | 686 |     const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset); | 
 | 687 |  | 
 | 688 |     if (functor(i, verdef, verdaux) == true) { | 
 | 689 |       break; | 
 | 690 |     } | 
 | 691 |   } | 
 | 692 |  | 
 | 693 |   return true; | 
 | 694 | } | 
 | 695 |  | 
 | 696 | bool soinfo::find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const { | 
 | 697 |   if (vi == nullptr) { | 
 | 698 |     *versym = kVersymNotNeeded; | 
 | 699 |     return true; | 
 | 700 |   } | 
 | 701 |  | 
 | 702 |   *versym = kVersymGlobal; | 
 | 703 |  | 
 | 704 |   return for_each_verdef(this, | 
 | 705 |     [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) { | 
 | 706 |       if (verdef->vd_hash == vi->elf_hash && | 
 | 707 |           strcmp(vi->name, get_string(verdaux->vda_name)) == 0) { | 
 | 708 |         *versym = verdef->vd_ndx; | 
 | 709 |         return true; | 
 | 710 |       } | 
 | 711 |  | 
 | 712 |       return false; | 
 | 713 |     } | 
 | 714 |   ); | 
 | 715 | } | 
 | 716 |  | 
 | 717 | bool soinfo::find_symbol_by_name(SymbolName& symbol_name, | 
 | 718 |                                  const version_info* vi, | 
 | 719 |                                  const ElfW(Sym)** symbol) const { | 
 | 720 |   uint32_t symbol_index; | 
 | 721 |   bool success = | 
 | 722 |       is_gnu_hash() ? | 
 | 723 |       gnu_lookup(symbol_name, vi, &symbol_index) : | 
 | 724 |       elf_lookup(symbol_name, vi, &symbol_index); | 
 | 725 |  | 
 | 726 |   if (success) { | 
 | 727 |     *symbol = symbol_index == 0 ? nullptr : symtab_ + symbol_index; | 
 | 728 |   } | 
 | 729 |  | 
 | 730 |   return success; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 731 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 732 |  | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 733 | static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) { | 
 | 734 |   if (ELF_ST_BIND(s->st_info) == STB_GLOBAL || | 
 | 735 |       ELF_ST_BIND(s->st_info) == STB_WEAK) { | 
 | 736 |     return s->st_shndx != SHN_UNDEF; | 
 | 737 |   } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) { | 
 | 738 |     DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 739 |         ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_realpath()); | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 740 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 741 |  | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 742 |   return false; | 
 | 743 | } | 
 | 744 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 745 | static const ElfW(Versym) kVersymHiddenBit = 0x8000; | 
 | 746 |  | 
 | 747 | static inline bool is_versym_hidden(const ElfW(Versym)* versym) { | 
 | 748 |   // the symbol is hidden if bit 15 of versym is set. | 
 | 749 |   return versym != nullptr && (*versym & kVersymHiddenBit) != 0; | 
 | 750 | } | 
 | 751 |  | 
 | 752 | static inline bool check_symbol_version(const ElfW(Versym) verneed, | 
 | 753 |                                         const ElfW(Versym)* verdef) { | 
 | 754 |   return verneed == kVersymNotNeeded || | 
 | 755 |       verdef == nullptr || | 
 | 756 |       verneed == (*verdef & ~kVersymHiddenBit); | 
 | 757 | } | 
 | 758 |  | 
 | 759 | bool soinfo::gnu_lookup(SymbolName& symbol_name, | 
 | 760 |                         const version_info* vi, | 
 | 761 |                         uint32_t* symbol_index) const { | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 762 |   uint32_t hash = symbol_name.gnu_hash(); | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 763 |   uint32_t h2 = hash >> gnu_shift2_; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 764 |  | 
 | 765 |   uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8; | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 766 |   uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_; | 
 | 767 |   ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num]; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 768 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 769 |   *symbol_index = 0; | 
 | 770 |  | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 771 |   TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 772 |       symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base)); | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 773 |  | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 774 |   // test against bloom filter | 
 | 775 |   if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) { | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 776 |     TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 777 |         symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base)); | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 778 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 779 |     return true; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 780 |   } | 
 | 781 |  | 
 | 782 |   // bloom test says "probably yes"... | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 783 |   uint32_t n = gnu_bucket_[hash % gnu_nbucket_]; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 784 |  | 
 | 785 |   if (n == 0) { | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 786 |     TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 787 |         symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base)); | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 788 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 789 |     return true; | 
 | 790 |   } | 
 | 791 |  | 
 | 792 |   // lookup versym for the version definition in this library | 
 | 793 |   // note the difference between "version is not requested" (vi == nullptr) | 
 | 794 |   // and "version not found". In the first case verneed is kVersymNotNeeded | 
 | 795 |   // which implies that the default version can be accepted; the second case results in | 
 | 796 |   // verneed = 1 (kVersymGlobal) and implies that we should ignore versioned symbols | 
 | 797 |   // for this library and consider only *global* ones. | 
 | 798 |   ElfW(Versym) verneed = 0; | 
 | 799 |   if (!find_verdef_version_index(vi, &verneed)) { | 
 | 800 |     return false; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 801 |   } | 
 | 802 |  | 
 | 803 |   do { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 804 |     ElfW(Sym)* s = symtab_ + n; | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 805 |     const ElfW(Versym)* verdef = get_versym(n); | 
 | 806 |     // skip hidden versions when verneed == kVersymNotNeeded (0) | 
 | 807 |     if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) { | 
 | 808 |         continue; | 
 | 809 |     } | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 810 |     if (((gnu_chain_[n] ^ hash) >> 1) == 0 && | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 811 |         check_symbol_version(verneed, verdef) && | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 812 |         strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && | 
 | 813 |         is_symbol_global_and_defined(this, s)) { | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 814 |       TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 815 |           symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value), | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 816 |           static_cast<size_t>(s->st_size)); | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 817 |       *symbol_index = n; | 
 | 818 |       return true; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 819 |     } | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 820 |   } while ((gnu_chain_[n++] & 1) == 0); | 
 | 821 |  | 
 | 822 |   TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 823 |              symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base)); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 824 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 825 |   return true; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 826 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 827 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 828 | bool soinfo::elf_lookup(SymbolName& symbol_name, | 
 | 829 |                         const version_info* vi, | 
 | 830 |                         uint32_t* symbol_index) const { | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 831 |   uint32_t hash = symbol_name.elf_hash(); | 
 | 832 |  | 
 | 833 |   TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 834 |              symbol_name.get_name(), get_realpath(), | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 835 |              reinterpret_cast<void*>(base), hash, hash % nbucket_); | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 836 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 837 |   ElfW(Versym) verneed = 0; | 
 | 838 |   if (!find_verdef_version_index(vi, &verneed)) { | 
 | 839 |     return false; | 
 | 840 |   } | 
 | 841 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 842 |   for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) { | 
 | 843 |     ElfW(Sym)* s = symtab_ + n; | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 844 |     const ElfW(Versym)* verdef = get_versym(n); | 
 | 845 |  | 
 | 846 |     // skip hidden versions when verneed == 0 | 
 | 847 |     if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) { | 
 | 848 |         continue; | 
 | 849 |     } | 
 | 850 |  | 
 | 851 |     if (check_symbol_version(verneed, verdef) && | 
 | 852 |         strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 853 |         is_symbol_global_and_defined(this, s)) { | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 854 |       TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 855 |                  symbol_name.get_name(), get_realpath(), | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 856 |                  reinterpret_cast<void*>(s->st_value), | 
 | 857 |                  static_cast<size_t>(s->st_size)); | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 858 |       *symbol_index = n; | 
 | 859 |       return true; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 860 |     } | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 861 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 862 |  | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 863 |   TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 864 |              symbol_name.get_name(), get_realpath(), | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 865 |              reinterpret_cast<void*>(base), hash, hash % nbucket_); | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 866 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 867 |   *symbol_index = 0; | 
 | 868 |   return true; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 869 | } | 
 | 870 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 871 | soinfo::soinfo(android_namespace_t* ns, const char* realpath, | 
 | 872 |                const struct stat* file_stat, off64_t file_offset, | 
 | 873 |                int rtld_flags) { | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 874 |   memset(this, 0, sizeof(*this)); | 
 | 875 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 876 |   if (realpath != nullptr) { | 
 | 877 |     realpath_ = realpath; | 
 | 878 |   } | 
 | 879 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 880 |   flags_ = FLAG_NEW_SOINFO; | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 881 |   version_ = SOINFO_VERSION; | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 882 |  | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 883 |   if (file_stat != nullptr) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 884 |     this->st_dev_ = file_stat->st_dev; | 
 | 885 |     this->st_ino_ = file_stat->st_ino; | 
 | 886 |     this->file_offset_ = file_offset; | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 887 |   } | 
| Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 888 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 889 |   this->rtld_flags_ = rtld_flags; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 890 |   this->namespace_ = ns; | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 891 | } | 
 | 892 |  | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 893 | static uint32_t calculate_elf_hash(const char* name) { | 
 | 894 |   const uint8_t* name_bytes = reinterpret_cast<const uint8_t*>(name); | 
 | 895 |   uint32_t h = 0, g; | 
 | 896 |  | 
 | 897 |   while (*name_bytes) { | 
 | 898 |     h = (h << 4) + *name_bytes++; | 
 | 899 |     g = h & 0xf0000000; | 
 | 900 |     h ^= g; | 
 | 901 |     h ^= g >> 24; | 
 | 902 |   } | 
 | 903 |  | 
 | 904 |   return h; | 
 | 905 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 906 |  | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 907 | uint32_t SymbolName::elf_hash() { | 
 | 908 |   if (!has_elf_hash_) { | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 909 |     elf_hash_ = calculate_elf_hash(name_); | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 910 |     has_elf_hash_ = true; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 911 |   } | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 912 |  | 
 | 913 |   return elf_hash_; | 
 | 914 | } | 
 | 915 |  | 
 | 916 | uint32_t SymbolName::gnu_hash() { | 
 | 917 |   if (!has_gnu_hash_) { | 
 | 918 |     uint32_t h = 5381; | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 919 |     const uint8_t* name = reinterpret_cast<const uint8_t*>(name_); | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 920 |     while (*name != 0) { | 
 | 921 |       h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c | 
 | 922 |     } | 
 | 923 |  | 
 | 924 |     gnu_hash_ =  h; | 
 | 925 |     has_gnu_hash_ = true; | 
 | 926 |   } | 
 | 927 |  | 
 | 928 |   return gnu_hash_; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 929 | } | 
 | 930 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 931 | bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi, | 
 | 932 |                       soinfo** si_found_in, const soinfo::soinfo_list_t& global_group, | 
 | 933 |                       const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol) { | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 934 |   SymbolName symbol_name(name); | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 935 |   const ElfW(Sym)* s = nullptr; | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 936 |  | 
| Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 937 |   /* "This element's presence in a shared object library alters the dynamic linker's | 
 | 938 |    * symbol resolution algorithm for references within the library. Instead of starting | 
 | 939 |    * a symbol search with the executable file, the dynamic linker starts from the shared | 
 | 940 |    * object itself. If the shared object fails to supply the referenced symbol, the | 
 | 941 |    * dynamic linker then searches the executable file and other shared objects as usual." | 
 | 942 |    * | 
 | 943 |    * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html | 
 | 944 |    * | 
 | 945 |    * Note that this is unlikely since static linker avoids generating | 
 | 946 |    * relocations for -Bsymbolic linked dynamic executables. | 
 | 947 |    */ | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 948 |   if (si_from->has_DT_SYMBOLIC) { | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 949 |     DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name); | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 950 |     if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) { | 
 | 951 |       return false; | 
 | 952 |     } | 
 | 953 |  | 
| Dmitriy Ivanov | 8f61d99 | 2014-09-16 14:31:06 -0700 | [diff] [blame] | 954 |     if (s != nullptr) { | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 955 |       *si_found_in = si_from; | 
| Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 956 |     } | 
 | 957 |   } | 
 | 958 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 959 |   // 1. Look for it in global_group | 
 | 960 |   if (s == nullptr) { | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 961 |     bool error = false; | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 962 |     global_group.visit([&](soinfo* global_si) { | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 963 |       DEBUG("%s: looking up %s in %s (from global group)", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 964 |           si_from->get_realpath(), name, global_si->get_realpath()); | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 965 |       if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) { | 
 | 966 |         error = true; | 
 | 967 |         return false; | 
 | 968 |       } | 
 | 969 |  | 
| Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 970 |       if (s != nullptr) { | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 971 |         *si_found_in = global_si; | 
 | 972 |         return false; | 
| Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 973 |       } | 
| Dmitriy Ivanov | c204894 | 2014-08-29 10:15:25 -0700 | [diff] [blame] | 974 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 975 |       return true; | 
 | 976 |     }); | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 977 |  | 
 | 978 |     if (error) { | 
 | 979 |       return false; | 
 | 980 |     } | 
| Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 981 |   } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 982 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 983 |   // 2. Look for it in the local group | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 984 |   if (s == nullptr) { | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 985 |     bool error = false; | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 986 |     local_group.visit([&](soinfo* local_si) { | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 987 |       if (local_si == si_from && si_from->has_DT_SYMBOLIC) { | 
| Dmitriy Ivanov | e47b3f8 | 2014-10-23 14:19:07 -0700 | [diff] [blame] | 988 |         // we already did this - skip | 
 | 989 |         return true; | 
 | 990 |       } | 
 | 991 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 992 |       DEBUG("%s: looking up %s in %s (from local group)", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 993 |           si_from->get_realpath(), name, local_si->get_realpath()); | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 994 |       if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) { | 
 | 995 |         error = true; | 
 | 996 |         return false; | 
 | 997 |       } | 
 | 998 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 999 |       if (s != nullptr) { | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 1000 |         *si_found_in = local_si; | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1001 |         return false; | 
 | 1002 |       } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 1003 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1004 |       return true; | 
 | 1005 |     }); | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1006 |  | 
 | 1007 |     if (error) { | 
 | 1008 |       return false; | 
 | 1009 |     } | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1010 |   } | 
 | 1011 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 1012 |   if (s != nullptr) { | 
 | 1013 |     TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, " | 
 | 1014 |                "found in %s, base = %p, load bias = %p", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 1015 |                si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value), | 
 | 1016 |                (*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base), | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 1017 |                reinterpret_cast<void*>((*si_found_in)->load_bias)); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 1018 |   } | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 1019 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1020 |   *symbol = s; | 
 | 1021 |   return true; | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 1022 | } | 
 | 1023 |  | 
| Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 1024 | class ProtectedDataGuard { | 
 | 1025 |  public: | 
 | 1026 |   ProtectedDataGuard() { | 
 | 1027 |     if (ref_count_++ == 0) { | 
 | 1028 |       protect_data(PROT_READ | PROT_WRITE); | 
 | 1029 |     } | 
 | 1030 |   } | 
 | 1031 |  | 
 | 1032 |   ~ProtectedDataGuard() { | 
 | 1033 |     if (ref_count_ == 0) { // overflow | 
 | 1034 |       __libc_fatal("Too many nested calls to dlopen()"); | 
 | 1035 |     } | 
 | 1036 |  | 
 | 1037 |     if (--ref_count_ == 0) { | 
 | 1038 |       protect_data(PROT_READ); | 
 | 1039 |     } | 
 | 1040 |   } | 
 | 1041 |  private: | 
 | 1042 |   void protect_data(int protection) { | 
 | 1043 |     g_soinfo_allocator.protect_all(protection); | 
 | 1044 |     g_soinfo_links_allocator.protect_all(protection); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1045 |     g_namespace_allocator.protect_all(protection); | 
| Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 1046 |   } | 
 | 1047 |  | 
 | 1048 |   static size_t ref_count_; | 
 | 1049 | }; | 
 | 1050 |  | 
 | 1051 | size_t ProtectedDataGuard::ref_count_ = 0; | 
 | 1052 |  | 
| Dmitriy Ivanov | 0cd83eb | 2014-09-01 16:15:52 -0700 | [diff] [blame] | 1053 | // Each size has it's own allocator. | 
 | 1054 | template<size_t size> | 
 | 1055 | class SizeBasedAllocator { | 
 | 1056 |  public: | 
 | 1057 |   static void* alloc() { | 
 | 1058 |     return allocator_.alloc(); | 
 | 1059 |   } | 
| Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 1060 |  | 
| Dmitriy Ivanov | 0cd83eb | 2014-09-01 16:15:52 -0700 | [diff] [blame] | 1061 |   static void free(void* ptr) { | 
 | 1062 |     allocator_.free(ptr); | 
 | 1063 |   } | 
| Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 1064 |  | 
| Dmitriy Ivanov | 0cd83eb | 2014-09-01 16:15:52 -0700 | [diff] [blame] | 1065 |  private: | 
 | 1066 |   static LinkerBlockAllocator allocator_; | 
 | 1067 | }; | 
 | 1068 |  | 
 | 1069 | template<size_t size> | 
 | 1070 | LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size); | 
 | 1071 |  | 
 | 1072 | template<typename T> | 
 | 1073 | class TypeBasedAllocator { | 
 | 1074 |  public: | 
 | 1075 |   static T* alloc() { | 
 | 1076 |     return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc()); | 
 | 1077 |   } | 
 | 1078 |  | 
 | 1079 |   static void free(T* ptr) { | 
 | 1080 |     SizeBasedAllocator<sizeof(T)>::free(ptr); | 
 | 1081 |   } | 
 | 1082 | }; | 
 | 1083 |  | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1084 | class LoadTask { | 
 | 1085 |  public: | 
 | 1086 |   struct deleter_t { | 
 | 1087 |     void operator()(LoadTask* t) { | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1088 |       t->~LoadTask(); | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1089 |       TypeBasedAllocator<LoadTask>::free(t); | 
 | 1090 |     } | 
 | 1091 |   }; | 
 | 1092 |  | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1093 |   static deleter_t deleter; | 
 | 1094 |  | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1095 |   static LoadTask* create(const char* name, soinfo* needed_by, | 
 | 1096 |                           std::unordered_map<const soinfo*, ElfReader>* readers_map) { | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1097 |     LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc(); | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1098 |     return new (ptr) LoadTask(name, needed_by, readers_map); | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1099 |   } | 
 | 1100 |  | 
 | 1101 |   const char* get_name() const { | 
 | 1102 |     return name_; | 
 | 1103 |   } | 
 | 1104 |  | 
 | 1105 |   soinfo* get_needed_by() const { | 
 | 1106 |     return needed_by_; | 
 | 1107 |   } | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1108 |  | 
 | 1109 |   soinfo* get_soinfo() const { | 
 | 1110 |     return si_; | 
 | 1111 |   } | 
 | 1112 |  | 
 | 1113 |   void set_soinfo(soinfo* si) { | 
 | 1114 |     si_ = si; | 
 | 1115 |   } | 
 | 1116 |  | 
 | 1117 |   off64_t get_file_offset() const { | 
 | 1118 |     return file_offset_; | 
 | 1119 |   } | 
 | 1120 |  | 
 | 1121 |   void set_file_offset(off64_t offset) { | 
 | 1122 |     file_offset_ = offset; | 
 | 1123 |   } | 
 | 1124 |  | 
 | 1125 |   int get_fd() const { | 
 | 1126 |     return fd_; | 
 | 1127 |   } | 
 | 1128 |  | 
 | 1129 |   void set_fd(int fd, bool assume_ownership) { | 
 | 1130 |     fd_ = fd; | 
 | 1131 |     close_fd_ = assume_ownership; | 
 | 1132 |   } | 
 | 1133 |  | 
 | 1134 |   const android_dlextinfo* get_extinfo() const { | 
 | 1135 |     return extinfo_; | 
 | 1136 |   } | 
 | 1137 |  | 
 | 1138 |   void set_extinfo(const android_dlextinfo* extinfo) { | 
 | 1139 |     extinfo_ = extinfo; | 
 | 1140 |   } | 
 | 1141 |  | 
 | 1142 |   const ElfReader& get_elf_reader() const { | 
 | 1143 |     CHECK(si_ != nullptr); | 
 | 1144 |     return (*elf_readers_map_)[si_]; | 
 | 1145 |   } | 
 | 1146 |  | 
 | 1147 |   ElfReader& get_elf_reader() { | 
 | 1148 |     CHECK(si_ != nullptr); | 
 | 1149 |     return (*elf_readers_map_)[si_]; | 
 | 1150 |   } | 
 | 1151 |  | 
 | 1152 |   std::unordered_map<const soinfo*, ElfReader>* get_readers_map() { | 
 | 1153 |     return elf_readers_map_; | 
 | 1154 |   } | 
 | 1155 |  | 
 | 1156 |   bool read(const char* realpath, off64_t file_size) { | 
 | 1157 |     ElfReader& elf_reader = get_elf_reader(); | 
 | 1158 |     return elf_reader.Read(realpath, fd_, file_offset_, file_size); | 
 | 1159 |   } | 
 | 1160 |  | 
 | 1161 |   bool load() { | 
 | 1162 |     ElfReader& elf_reader = get_elf_reader(); | 
 | 1163 |     if (!elf_reader.Load(extinfo_)) { | 
 | 1164 |       return false; | 
 | 1165 |     } | 
 | 1166 |  | 
 | 1167 |     si_->base = elf_reader.load_start(); | 
 | 1168 |     si_->size = elf_reader.load_size(); | 
| Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 1169 |     si_->set_mapped_by_caller(elf_reader.is_mapped_by_caller()); | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1170 |     si_->load_bias = elf_reader.load_bias(); | 
 | 1171 |     si_->phnum = elf_reader.phdr_count(); | 
 | 1172 |     si_->phdr = elf_reader.loaded_phdr(); | 
 | 1173 |  | 
 | 1174 |     return true; | 
 | 1175 |   } | 
 | 1176 |  | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1177 |  private: | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1178 |   LoadTask(const char* name, soinfo* needed_by, | 
 | 1179 |            std::unordered_map<const soinfo*, ElfReader>* readers_map) | 
 | 1180 |     : name_(name), needed_by_(needed_by), si_(nullptr), | 
 | 1181 |       fd_(-1), close_fd_(false), file_offset_(0), elf_readers_map_(readers_map) {} | 
 | 1182 |  | 
 | 1183 |   ~LoadTask() { | 
 | 1184 |     if (fd_ != -1 && close_fd_) { | 
 | 1185 |       close(fd_); | 
 | 1186 |     } | 
 | 1187 |   } | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1188 |  | 
 | 1189 |   const char* name_; | 
 | 1190 |   soinfo* needed_by_; | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1191 |   soinfo* si_; | 
 | 1192 |   const android_dlextinfo* extinfo_; | 
 | 1193 |   int fd_; | 
 | 1194 |   bool close_fd_; | 
 | 1195 |   off64_t file_offset_; | 
 | 1196 |   std::unordered_map<const soinfo*, ElfReader>* elf_readers_map_; | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1197 |  | 
 | 1198 |   DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask); | 
 | 1199 | }; | 
 | 1200 |  | 
| Ningsheng Jian | e93be99 | 2014-09-16 15:22:10 +0800 | [diff] [blame] | 1201 | LoadTask::deleter_t LoadTask::deleter; | 
 | 1202 |  | 
| Dmitriy Ivanov | 0cd83eb | 2014-09-01 16:15:52 -0700 | [diff] [blame] | 1203 | template <typename T> | 
 | 1204 | using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>; | 
 | 1205 |  | 
 | 1206 | typedef linked_list_t<soinfo> SoinfoLinkedList; | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1207 | typedef linked_list_t<const char> StringLinkedList; | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1208 | typedef std::vector<LoadTask*> LoadTaskList; | 
| Dmitriy Ivanov | 0cd83eb | 2014-09-01 16:15:52 -0700 | [diff] [blame] | 1209 |  | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 1210 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1211 | // This function walks down the tree of soinfo dependencies | 
 | 1212 | // in breadth-first order and | 
 | 1213 | //   * calls action(soinfo* si) for each node, and | 
 | 1214 | //   * terminates walk if action returns false. | 
 | 1215 | // | 
 | 1216 | // walk_dependencies_tree returns false if walk was terminated | 
 | 1217 | // by the action and true otherwise. | 
 | 1218 | template<typename F> | 
 | 1219 | static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) { | 
| Dmitriy Ivanov | 0cd83eb | 2014-09-01 16:15:52 -0700 | [diff] [blame] | 1220 |   SoinfoLinkedList visit_list; | 
 | 1221 |   SoinfoLinkedList visited; | 
 | 1222 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1223 |   for (size_t i = 0; i < root_soinfos_size; ++i) { | 
 | 1224 |     visit_list.push_back(root_soinfos[i]); | 
 | 1225 |   } | 
 | 1226 |  | 
 | 1227 |   soinfo* si; | 
 | 1228 |   while ((si = visit_list.pop_front()) != nullptr) { | 
 | 1229 |     if (visited.contains(si)) { | 
| Dmitriy Ivanov | 042426b | 2014-08-12 21:02:13 -0700 | [diff] [blame] | 1230 |       continue; | 
 | 1231 |     } | 
 | 1232 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1233 |     if (!action(si)) { | 
 | 1234 |       return false; | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 1235 |     } | 
 | 1236 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1237 |     visited.push_back(si); | 
 | 1238 |  | 
 | 1239 |     si->get_children().for_each([&](soinfo* child) { | 
| Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 1240 |       visit_list.push_back(child); | 
 | 1241 |     }); | 
 | 1242 |   } | 
 | 1243 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1244 |   return true; | 
 | 1245 | } | 
 | 1246 |  | 
 | 1247 |  | 
| Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 1248 | static const ElfW(Sym)* dlsym_handle_lookup(soinfo* root, soinfo* skip_until, | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 1249 |                                             soinfo** found, SymbolName& symbol_name, | 
 | 1250 |                                             const version_info* vi) { | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1251 |   const ElfW(Sym)* result = nullptr; | 
| Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 1252 |   bool skip_lookup = skip_until != nullptr; | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1253 |  | 
| Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 1254 |   walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) { | 
 | 1255 |     if (skip_lookup) { | 
 | 1256 |       skip_lookup = current_soinfo != skip_until; | 
 | 1257 |       return true; | 
 | 1258 |     } | 
 | 1259 |  | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 1260 |     if (!current_soinfo->find_symbol_by_name(symbol_name, vi, &result)) { | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1261 |       result = nullptr; | 
 | 1262 |       return false; | 
 | 1263 |     } | 
 | 1264 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1265 |     if (result != nullptr) { | 
 | 1266 |       *found = current_soinfo; | 
 | 1267 |       return false; | 
 | 1268 |     } | 
 | 1269 |  | 
 | 1270 |     return true; | 
 | 1271 |   }); | 
 | 1272 |  | 
 | 1273 |   return result; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1274 | } | 
 | 1275 |  | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 1276 | static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns, | 
 | 1277 |                                             const char* name, | 
 | 1278 |                                             const version_info* vi, | 
 | 1279 |                                             soinfo** found, | 
 | 1280 |                                             soinfo* caller, | 
 | 1281 |                                             void* handle); | 
 | 1282 |  | 
| Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 1283 | // This is used by dlsym(3).  It performs symbol lookup only within the | 
 | 1284 | // specified soinfo object and its dependencies in breadth first order. | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 1285 | static const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, | 
 | 1286 |                                             const char* name, const version_info* vi) { | 
| Dmitriy Ivanov | f439b5a | 2015-05-30 13:04:39 -0700 | [diff] [blame] | 1287 |   // According to man dlopen(3) and posix docs in the case when si is handle | 
 | 1288 |   // of the main executable we need to search not only in the executable and its | 
 | 1289 |   // dependencies but also in all libraries loaded with RTLD_GLOBAL. | 
 | 1290 |   // | 
 | 1291 |   // Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared | 
 | 1292 |   // libraries and they are loaded in breath-first (correct) order we can just execute | 
 | 1293 |   // dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup. | 
 | 1294 |   if (si == somain) { | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 1295 |     return dlsym_linear_lookup(&g_default_namespace, name, vi, found, nullptr, RTLD_DEFAULT); | 
| Dmitriy Ivanov | f439b5a | 2015-05-30 13:04:39 -0700 | [diff] [blame] | 1296 |   } | 
 | 1297 |  | 
| Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 1298 |   SymbolName symbol_name(name); | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 1299 |   return dlsym_handle_lookup(si, nullptr, found, symbol_name, vi); | 
| Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 1300 | } | 
 | 1301 |  | 
| Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1302 | /* This is used by dlsym(3) to performs a global symbol lookup. If the | 
 | 1303 |    start value is null (for RTLD_DEFAULT), the search starts at the | 
 | 1304 |    beginning of the global solist. Otherwise the search starts at the | 
 | 1305 |    specified soinfo (for RTLD_NEXT). | 
| Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 1306 |  */ | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 1307 | static const ElfW(Sym)* dlsym_linear_lookup(android_namespace_t* ns, | 
 | 1308 |                                             const char* name, | 
 | 1309 |                                             const version_info* vi, | 
 | 1310 |                                             soinfo** found, | 
 | 1311 |                                             soinfo* caller, | 
 | 1312 |                                             void* handle) { | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1313 |   SymbolName symbol_name(name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1314 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1315 |   soinfo::soinfo_list_t& soinfo_list = ns->soinfo_list(); | 
 | 1316 |   soinfo::soinfo_list_t::iterator start = soinfo_list.begin(); | 
| Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 1317 |  | 
 | 1318 |   if (handle == RTLD_NEXT) { | 
| Dmitriy Ivanov | b96ac41 | 2015-05-22 12:34:42 -0700 | [diff] [blame] | 1319 |     if (caller == nullptr) { | 
| Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 1320 |       return nullptr; | 
 | 1321 |     } else { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1322 |       soinfo::soinfo_list_t::iterator it = soinfo_list.find(caller); | 
 | 1323 |       CHECK (it != soinfo_list.end()); | 
 | 1324 |       start = ++it; | 
| Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 1325 |     } | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 1326 |   } | 
 | 1327 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1328 |   const ElfW(Sym)* s = nullptr; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1329 |   for (soinfo::soinfo_list_t::iterator it = start, end = soinfo_list.end(); it != end; ++it) { | 
 | 1330 |     soinfo* si = *it; | 
| Dmitriy Ivanov | 1913352 | 2015-06-02 17:36:54 -0700 | [diff] [blame] | 1331 |     // Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...) | 
 | 1332 |     // if the library is opened by application with target api level <= 22 | 
 | 1333 |     // See http://b/21565766 | 
 | 1334 |     if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 && si->get_target_sdk_version() > 22) { | 
| Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 1335 |       continue; | 
 | 1336 |     } | 
 | 1337 |  | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 1338 |     if (!si->find_symbol_by_name(symbol_name, vi, &s)) { | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1339 |       return nullptr; | 
 | 1340 |     } | 
 | 1341 |  | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1342 |     if (s != nullptr) { | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 1343 |       *found = si; | 
 | 1344 |       break; | 
| Matt Fischer | 1698d9e | 2009-12-31 12:17:56 -0600 | [diff] [blame] | 1345 |     } | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 1346 |   } | 
| Matt Fischer | 1698d9e | 2009-12-31 12:17:56 -0600 | [diff] [blame] | 1347 |  | 
| Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 1348 |   // If not found - use dlsym_handle_lookup for caller's | 
 | 1349 |   // local_group unless it is part of the global group in which | 
| Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 1350 |   // case we already did it. | 
 | 1351 |   if (s == nullptr && caller != nullptr && | 
 | 1352 |       (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) { | 
| Dmitriy Ivanov | 697bd9f | 2015-05-12 11:12:27 -0700 | [diff] [blame] | 1353 |     return dlsym_handle_lookup(caller->get_local_group_root(), | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 1354 |         (handle == RTLD_NEXT) ? caller : nullptr, found, symbol_name, vi); | 
| Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 1355 |   } | 
 | 1356 |  | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1357 |   if (s != nullptr) { | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1358 |     TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p", | 
 | 1359 |                name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base)); | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 1360 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1361 |  | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 1362 |   return s; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1363 | } | 
 | 1364 |  | 
| Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1365 | soinfo* find_containing_library(const void* p) { | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 1366 |   ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p); | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1367 |   for (soinfo* si = solist; si != nullptr; si = si->next) { | 
| Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1368 |     if (address >= si->base && address - si->base < si->size) { | 
 | 1369 |       return si; | 
| Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 1370 |     } | 
| Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1371 |   } | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1372 |   return nullptr; | 
| Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 1373 | } | 
 | 1374 |  | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1375 | ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) { | 
 | 1376 |   return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr); | 
 | 1377 | } | 
 | 1378 |  | 
 | 1379 | static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) { | 
 | 1380 |   return sym->st_shndx != SHN_UNDEF && | 
 | 1381 |       soaddr >= sym->st_value && | 
 | 1382 |       soaddr < sym->st_value + sym->st_size; | 
 | 1383 | } | 
 | 1384 |  | 
 | 1385 | ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) { | 
| Chris Dearman | 8e55381 | 2013-11-13 17:22:33 -0800 | [diff] [blame] | 1386 |   ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1387 |  | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 1388 |   for (size_t i = 0; i < gnu_nbucket_; ++i) { | 
 | 1389 |     uint32_t n = gnu_bucket_[i]; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1390 |  | 
 | 1391 |     if (n == 0) { | 
 | 1392 |       continue; | 
 | 1393 |     } | 
 | 1394 |  | 
 | 1395 |     do { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 1396 |       ElfW(Sym)* sym = symtab_ + n; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1397 |       if (symbol_matches_soaddr(sym, soaddr)) { | 
 | 1398 |         return sym; | 
 | 1399 |       } | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 1400 |     } while ((gnu_chain_[n++] & 1) == 0); | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1401 |   } | 
 | 1402 |  | 
 | 1403 |   return nullptr; | 
 | 1404 | } | 
 | 1405 |  | 
 | 1406 | ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) { | 
| Chris Dearman | 8e55381 | 2013-11-13 17:22:33 -0800 | [diff] [blame] | 1407 |   ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias; | 
| Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 1408 |  | 
| Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1409 |   // Search the library's symbol table for any defined symbol which | 
 | 1410 |   // contains this address. | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 1411 |   for (size_t i = 0; i < nchain_; ++i) { | 
 | 1412 |     ElfW(Sym)* sym = symtab_ + i; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1413 |     if (symbol_matches_soaddr(sym, soaddr)) { | 
| Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1414 |       return sym; | 
| Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 1415 |     } | 
| Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1416 |   } | 
| Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 1417 |  | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1418 |   return nullptr; | 
| Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 1419 | } | 
 | 1420 |  | 
| Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 1421 | class ZipArchiveCache { | 
 | 1422 |  public: | 
 | 1423 |   ZipArchiveCache() {} | 
 | 1424 |   ~ZipArchiveCache(); | 
 | 1425 |  | 
 | 1426 |   bool get_or_open(const char* zip_path, ZipArchiveHandle* handle); | 
 | 1427 |  private: | 
 | 1428 |   DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache); | 
 | 1429 |  | 
 | 1430 |   std::unordered_map<std::string, ZipArchiveHandle> cache_; | 
 | 1431 | }; | 
 | 1432 |  | 
 | 1433 | bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) { | 
 | 1434 |   std::string key(zip_path); | 
 | 1435 |  | 
 | 1436 |   auto it = cache_.find(key); | 
 | 1437 |   if (it != cache_.end()) { | 
 | 1438 |     *handle = it->second; | 
 | 1439 |     return true; | 
 | 1440 |   } | 
 | 1441 |  | 
 | 1442 |   int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC)); | 
 | 1443 |   if (fd == -1) { | 
 | 1444 |     return false; | 
 | 1445 |   } | 
 | 1446 |  | 
 | 1447 |   if (OpenArchiveFd(fd, "", handle) != 0) { | 
 | 1448 |     // invalid zip-file (?) | 
 | 1449 |     close(fd); | 
 | 1450 |     return false; | 
 | 1451 |   } | 
 | 1452 |  | 
 | 1453 |   cache_[key] = *handle; | 
 | 1454 |   return true; | 
 | 1455 | } | 
 | 1456 |  | 
 | 1457 | ZipArchiveCache::~ZipArchiveCache() { | 
| Dmitriy Ivanov | 5dce894 | 2015-10-13 12:14:16 -0700 | [diff] [blame] | 1458 |   for (const auto& it : cache_) { | 
| Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 1459 |     CloseArchive(it.second); | 
 | 1460 |   } | 
 | 1461 | } | 
 | 1462 |  | 
 | 1463 | static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache, | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1464 |                                    const char* const input_path, | 
 | 1465 |                                    off64_t* file_offset, std::string* realpath) { | 
 | 1466 |   std::string normalized_path; | 
 | 1467 |   if (!normalize_path(input_path, &normalized_path)) { | 
 | 1468 |     return -1; | 
 | 1469 |   } | 
 | 1470 |  | 
 | 1471 |   const char* const path = normalized_path.c_str(); | 
 | 1472 |   TRACE("Trying zip file open from path '%s' -> normalized '%s'", input_path, path); | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1473 |  | 
| Dmitriy Ivanov | 402a750 | 2015-06-09 13:46:51 -0700 | [diff] [blame] | 1474 |   // Treat an '!/' separator inside a path as the separator between the name | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1475 |   // of the zip file on disk and the subdirectory to search within it. | 
| Dmitriy Ivanov | 402a750 | 2015-06-09 13:46:51 -0700 | [diff] [blame] | 1476 |   // For example, if path is "foo.zip!/bar/bas/x.so", then we search for | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1477 |   // "bar/bas/x.so" within "foo.zip". | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1478 |   const char* const separator = strstr(path, kZipFileSeparator); | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1479 |   if (separator == nullptr) { | 
 | 1480 |     return -1; | 
| Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1481 |   } | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1482 |  | 
 | 1483 |   char buf[512]; | 
 | 1484 |   if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) { | 
 | 1485 |     PRINT("Warning: ignoring very long library path: %s", path); | 
 | 1486 |     return -1; | 
 | 1487 |   } | 
 | 1488 |  | 
 | 1489 |   buf[separator - path] = '\0'; | 
 | 1490 |  | 
 | 1491 |   const char* zip_path = buf; | 
| Dmitriy Ivanov | 402a750 | 2015-06-09 13:46:51 -0700 | [diff] [blame] | 1492 |   const char* file_path = &buf[separator - path + 2]; | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1493 |   int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC)); | 
 | 1494 |   if (fd == -1) { | 
 | 1495 |     return -1; | 
 | 1496 |   } | 
 | 1497 |  | 
 | 1498 |   ZipArchiveHandle handle; | 
| Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 1499 |   if (!zip_archive_cache->get_or_open(zip_path, &handle)) { | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1500 |     // invalid zip-file (?) | 
 | 1501 |     close(fd); | 
 | 1502 |     return -1; | 
 | 1503 |   } | 
 | 1504 |  | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1505 |   ZipEntry entry; | 
 | 1506 |  | 
| Yusuke Sato | 56f40fb | 2015-06-25 14:56:07 -0700 | [diff] [blame] | 1507 |   if (FindEntry(handle, ZipString(file_path), &entry) != 0) { | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1508 |     // Entry was not found. | 
 | 1509 |     close(fd); | 
 | 1510 |     return -1; | 
 | 1511 |   } | 
 | 1512 |  | 
 | 1513 |   // Check if it is properly stored | 
 | 1514 |   if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) { | 
 | 1515 |     close(fd); | 
 | 1516 |     return -1; | 
 | 1517 |   } | 
 | 1518 |  | 
 | 1519 |   *file_offset = entry.offset; | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1520 |  | 
 | 1521 |   if (realpath_fd(fd, realpath)) { | 
 | 1522 |     *realpath += separator; | 
 | 1523 |   } else { | 
 | 1524 |     PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", | 
 | 1525 |           normalized_path.c_str()); | 
 | 1526 |     *realpath = normalized_path; | 
 | 1527 |   } | 
 | 1528 |  | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1529 |   return fd; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1530 | } | 
 | 1531 |  | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1532 | static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) { | 
 | 1533 |   int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name); | 
 | 1534 |   if (n < 0 || n >= static_cast<int>(buf_size)) { | 
 | 1535 |     PRINT("Warning: ignoring very long library path: %s/%s", path, name); | 
 | 1536 |     return false; | 
 | 1537 |   } | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1538 |  | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1539 |   return true; | 
 | 1540 | } | 
 | 1541 |  | 
| Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 1542 | static int open_library_on_paths(ZipArchiveCache* zip_archive_cache, | 
 | 1543 |                                  const char* name, off64_t* file_offset, | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1544 |                                  const std::vector<std::string>& paths, | 
 | 1545 |                                  std::string* realpath) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1546 |   for (const auto& path : paths) { | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1547 |     char buf[512]; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1548 |     if (!format_path(buf, sizeof(buf), path.c_str(), name)) { | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1549 |       continue; | 
 | 1550 |     } | 
 | 1551 |  | 
 | 1552 |     int fd = -1; | 
| Dmitriy Ivanov | 730ed9d | 2015-07-16 04:52:06 -0700 | [diff] [blame] | 1553 |     if (strstr(buf, kZipFileSeparator) != nullptr) { | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1554 |       fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath); | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1555 |     } | 
 | 1556 |  | 
 | 1557 |     if (fd == -1) { | 
 | 1558 |       fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC)); | 
 | 1559 |       if (fd != -1) { | 
 | 1560 |         *file_offset = 0; | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1561 |         if (!realpath_fd(fd, realpath)) { | 
 | 1562 |           PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf); | 
 | 1563 |           *realpath = buf; | 
 | 1564 |         } | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1565 |       } | 
 | 1566 |     } | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1567 |  | 
 | 1568 |     if (fd != -1) { | 
 | 1569 |       return fd; | 
 | 1570 |     } | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1571 |   } | 
 | 1572 |  | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1573 |   return -1; | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1574 | } | 
 | 1575 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1576 | static int open_library(android_namespace_t* ns, | 
 | 1577 |                         ZipArchiveCache* zip_archive_cache, | 
| Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 1578 |                         const char* name, soinfo *needed_by, | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1579 |                         off64_t* file_offset, std::string* realpath) { | 
| Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1580 |   TRACE("[ opening %s ]", name); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1581 |  | 
| Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1582 |   // If the name contains a slash, we should attempt to open it directly and not search the paths. | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1583 |   if (strchr(name, '/') != nullptr) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1584 |     int fd = -1; | 
 | 1585 |  | 
| Dmitriy Ivanov | 730ed9d | 2015-07-16 04:52:06 -0700 | [diff] [blame] | 1586 |     if (strstr(name, kZipFileSeparator) != nullptr) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1587 |       fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath); | 
 | 1588 |     } | 
 | 1589 |  | 
 | 1590 |     if (fd == -1) { | 
 | 1591 |       fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC)); | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1592 |       if (fd != -1) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1593 |         *file_offset = 0; | 
 | 1594 |         if (!realpath_fd(fd, realpath)) { | 
 | 1595 |           PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name); | 
 | 1596 |           *realpath = name; | 
 | 1597 |         } | 
| Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1598 |       } | 
 | 1599 |     } | 
 | 1600 |  | 
| Dmitriy Ivanov | e44fffd | 2015-03-17 17:12:18 -0700 | [diff] [blame] | 1601 |     return fd; | 
| Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1602 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1603 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1604 |   // Otherwise we try LD_LIBRARY_PATH first, and fall back to the default library path | 
 | 1605 |   int fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_ld_library_paths(), realpath); | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1606 |   if (fd == -1 && needed_by != nullptr) { | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1607 |     fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1608 |     // Check if the library is accessible | 
 | 1609 |     if (fd != -1 && !ns->is_accessible(*realpath)) { | 
 | 1610 |       fd = -1; | 
 | 1611 |     } | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 1612 |   } | 
| Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 1613 |  | 
| Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1614 |   if (fd == -1) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1615 |     fd = open_library_on_paths(zip_archive_cache, name, file_offset, ns->get_default_library_paths(), realpath); | 
| Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1616 |   } | 
| Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 1617 |  | 
| Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1618 |   return fd; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1619 | } | 
 | 1620 |  | 
| Dmitriy Ivanov | d974e88 | 2015-05-27 18:29:41 -0700 | [diff] [blame] | 1621 | static const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) { | 
 | 1622 | #if !defined(__LP64__) | 
 | 1623 |   // Work around incorrect DT_NEEDED entries for old apps: http://b/21364029 | 
| Dmitriy Ivanov | 1913352 | 2015-06-02 17:36:54 -0700 | [diff] [blame] | 1624 |   if (get_application_target_sdk_version() <= 22) { | 
| Dmitriy Ivanov | d974e88 | 2015-05-27 18:29:41 -0700 | [diff] [blame] | 1625 |     const char* bname = basename(dt_needed); | 
 | 1626 |     if (bname != dt_needed) { | 
 | 1627 |       DL_WARN("'%s' library has invalid DT_NEEDED entry '%s'", sopath, dt_needed); | 
 | 1628 |     } | 
 | 1629 |  | 
 | 1630 |     return bname; | 
 | 1631 |   } | 
 | 1632 | #endif | 
 | 1633 |   return dt_needed; | 
 | 1634 | } | 
 | 1635 |  | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1636 | template<typename F> | 
 | 1637 | static void for_each_dt_needed(const soinfo* si, F action) { | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1638 |   for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) { | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1639 |     if (d->d_tag == DT_NEEDED) { | 
| Dmitriy Ivanov | d974e88 | 2015-05-27 18:29:41 -0700 | [diff] [blame] | 1640 |       action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath())); | 
| Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1641 |     } | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1642 |   } | 
 | 1643 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1644 |  | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1645 | template<typename F> | 
 | 1646 | static void for_each_dt_needed(const ElfReader& elf_reader, F action) { | 
 | 1647 |   for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) { | 
 | 1648 |     if (d->d_tag == DT_NEEDED) { | 
 | 1649 |       action(fix_dt_needed(elf_reader.get_string(d->d_un.d_val), elf_reader.name())); | 
 | 1650 |     } | 
 | 1651 |   } | 
 | 1652 | } | 
 | 1653 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1654 | static bool load_library(android_namespace_t* ns, | 
 | 1655 |                          LoadTask* task, | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1656 |                          LoadTaskList* load_tasks, | 
 | 1657 |                          int rtld_flags, | 
 | 1658 |                          const std::string& realpath) { | 
 | 1659 |   off64_t file_offset = task->get_file_offset(); | 
 | 1660 |   const char* name = task->get_name(); | 
 | 1661 |   const android_dlextinfo* extinfo = task->get_extinfo(); | 
 | 1662 |  | 
| Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 1663 |   if ((file_offset % PAGE_SIZE) != 0) { | 
| Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 1664 |     DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset); | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1665 |     return false; | 
| Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 1666 |   } | 
| Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 1667 |   if (file_offset < 0) { | 
 | 1668 |     DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset); | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1669 |     return false; | 
| Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 1670 |   } | 
| Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 1671 |  | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1672 |   struct stat file_stat; | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1673 |   if (TEMP_FAILURE_RETRY(fstat(task->get_fd(), &file_stat)) != 0) { | 
| Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 1674 |     DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno)); | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1675 |     return false; | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1676 |   } | 
| Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 1677 |   if (file_offset >= file_stat.st_size) { | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 1678 |     DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64, | 
 | 1679 |         name, file_offset, file_stat.st_size); | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1680 |     return false; | 
| Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 1681 |   } | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1682 |  | 
 | 1683 |   // Check for symlink and other situations where | 
| Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 1684 |   // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set | 
 | 1685 |   if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1686 |     auto predicate = [&](soinfo* si) { | 
 | 1687 |       return si->get_st_dev() != 0 && | 
 | 1688 |              si->get_st_ino() != 0 && | 
 | 1689 |              si->get_st_dev() == file_stat.st_dev && | 
 | 1690 |              si->get_st_ino() == file_stat.st_ino && | 
 | 1691 |              si->get_file_offset() == file_offset; | 
 | 1692 |     }; | 
 | 1693 |  | 
 | 1694 |     soinfo* si = ns->soinfo_list().find_if(predicate); | 
 | 1695 |  | 
 | 1696 |     // check public namespace | 
 | 1697 |     if (si == nullptr) { | 
 | 1698 |       si = g_public_namespace.find_if(predicate); | 
 | 1699 |       if (si != nullptr) { | 
 | 1700 |         ns->soinfo_list().push_back(si); | 
| Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 1701 |       } | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1702 |     } | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1703 |  | 
 | 1704 |     if (si != nullptr) { | 
 | 1705 |       TRACE("library \"%s\" is already loaded under different name/path \"%s\" - " | 
 | 1706 |             "will return existing soinfo", name, si->get_realpath()); | 
 | 1707 |       task->set_soinfo(si); | 
 | 1708 |       return true; | 
 | 1709 |     } | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1710 |   } | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1711 |  | 
| Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 1712 |   if ((rtld_flags & RTLD_NOLOAD) != 0) { | 
| Dmitriy Ivanov | a6ac54a | 2014-09-09 10:21:42 -0700 | [diff] [blame] | 1713 |     DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name); | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1714 |     return false; | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1715 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1716 |  | 
| Dimitry Ivanov | 22840aa | 2015-12-04 18:28:49 -0800 | [diff] [blame] | 1717 |   if (!ns->is_accessible(realpath)) { | 
 | 1718 |     // do not load libraries if they are not accessible for the specified namespace. | 
 | 1719 |     DL_ERR("library \"%s\" is not accessible for the namespace \"%s\"", | 
 | 1720 |            name, ns->get_name()); | 
 | 1721 |     return false; | 
 | 1722 |   } | 
 | 1723 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1724 |   soinfo* si = soinfo_alloc(ns, realpath.c_str(), &file_stat, file_offset, rtld_flags); | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1725 |   if (si == nullptr) { | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1726 |     return false; | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1727 |   } | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1728 |  | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1729 |   task->set_soinfo(si); | 
 | 1730 |  | 
 | 1731 |   // Read the ELF header and some of the segments. | 
 | 1732 |   if (!task->read(realpath.c_str(), file_stat.st_size)) { | 
| Dmitriy Ivanov | fd7a91e | 2015-11-06 10:44:37 -0800 | [diff] [blame] | 1733 |     soinfo_free(si); | 
 | 1734 |     task->set_soinfo(nullptr); | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1735 |     return false; | 
 | 1736 |   } | 
 | 1737 |  | 
 | 1738 |   // find and set DT_RUNPATH and dt_soname | 
 | 1739 |   // Note that these field values are temporary and are | 
 | 1740 |   // going to be overwritten on soinfo::prelink_image | 
 | 1741 |   // with values from PT_LOAD segments. | 
 | 1742 |   const ElfReader& elf_reader = task->get_elf_reader(); | 
 | 1743 |   for (const ElfW(Dyn)* d = elf_reader.dynamic(); d->d_tag != DT_NULL; ++d) { | 
 | 1744 |     if (d->d_tag == DT_RUNPATH) { | 
 | 1745 |       si->set_dt_runpath(elf_reader.get_string(d->d_un.d_val)); | 
 | 1746 |     } | 
 | 1747 |     if (d->d_tag == DT_SONAME) { | 
 | 1748 |       si->set_soname(elf_reader.get_string(d->d_un.d_val)); | 
 | 1749 |     } | 
 | 1750 |   } | 
 | 1751 |  | 
 | 1752 |   for_each_dt_needed(task->get_elf_reader(), [&](const char* name) { | 
 | 1753 |     load_tasks->push_back(LoadTask::create(name, si, task->get_readers_map())); | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1754 |   }); | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1755 |  | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1756 |   return true; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1757 | } | 
 | 1758 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1759 | static bool load_library(android_namespace_t* ns, | 
 | 1760 |                          LoadTask* task, | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1761 |                          ZipArchiveCache* zip_archive_cache, | 
 | 1762 |                          LoadTaskList* load_tasks, | 
 | 1763 |                          int rtld_flags) { | 
 | 1764 |   const char* name = task->get_name(); | 
 | 1765 |   soinfo* needed_by = task->get_needed_by(); | 
 | 1766 |   const android_dlextinfo* extinfo = task->get_extinfo(); | 
 | 1767 |  | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1768 |   off64_t file_offset; | 
 | 1769 |   std::string realpath; | 
| Spencer Low | 0346ad7 | 2015-04-22 18:06:51 -0700 | [diff] [blame] | 1770 |   if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) { | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1771 |     file_offset = 0; | 
| Spencer Low | 0346ad7 | 2015-04-22 18:06:51 -0700 | [diff] [blame] | 1772 |     if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) { | 
 | 1773 |       file_offset = extinfo->library_fd_offset; | 
 | 1774 |     } | 
| Dmitriy Ivanov | a1feb11 | 2015-10-01 18:41:57 -0700 | [diff] [blame] | 1775 |  | 
 | 1776 |     if (!realpath_fd(extinfo->library_fd, &realpath)) { | 
 | 1777 |       PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. " | 
 | 1778 |             "Will use given name.", name); | 
 | 1779 |       realpath = name; | 
 | 1780 |     } | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1781 |  | 
 | 1782 |     task->set_fd(extinfo->library_fd, false); | 
 | 1783 |     task->set_file_offset(file_offset); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1784 |     return load_library(ns, task, load_tasks, rtld_flags, realpath); | 
| Spencer Low | 0346ad7 | 2015-04-22 18:06:51 -0700 | [diff] [blame] | 1785 |   } | 
 | 1786 |  | 
 | 1787 |   // Open the file. | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1788 |   int fd = open_library(ns, zip_archive_cache, name, needed_by, &file_offset, &realpath); | 
| Spencer Low | 0346ad7 | 2015-04-22 18:06:51 -0700 | [diff] [blame] | 1789 |   if (fd == -1) { | 
 | 1790 |     DL_ERR("library \"%s\" not found", name); | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1791 |     return false; | 
| Spencer Low | 0346ad7 | 2015-04-22 18:06:51 -0700 | [diff] [blame] | 1792 |   } | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1793 |  | 
 | 1794 |   task->set_fd(fd, true); | 
 | 1795 |   task->set_file_offset(file_offset); | 
 | 1796 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1797 |   return load_library(ns, task, load_tasks, rtld_flags, realpath); | 
| Spencer Low | 0346ad7 | 2015-04-22 18:06:51 -0700 | [diff] [blame] | 1798 | } | 
 | 1799 |  | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1800 | // Returns true if library was found and false in 2 cases | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1801 | // 1. (for default namespace only) The library was found but loaded under different | 
 | 1802 | //    target_sdk_version (*candidate != nullptr) | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1803 | // 2. The library was not found by soname (*candidate is nullptr) | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1804 | static bool find_loaded_library_by_soname(android_namespace_t* ns, | 
 | 1805 |                                           const char* name, soinfo** candidate) { | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1806 |   *candidate = nullptr; | 
 | 1807 |  | 
| Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 1808 |   // Ignore filename with path. | 
 | 1809 |   if (strchr(name, '/') != nullptr) { | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1810 |     return false; | 
| Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 1811 |   } | 
 | 1812 |  | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1813 |   uint32_t target_sdk_version = get_application_target_sdk_version(); | 
 | 1814 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1815 |   return !ns->soinfo_list().visit([&](soinfo* si) { | 
| Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 1816 |     const char* soname = si->get_soname(); | 
 | 1817 |     if (soname != nullptr && (strcmp(name, soname) == 0)) { | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1818 |       // If the library was opened under different target sdk version | 
 | 1819 |       // skip this step and try to reopen it. The exceptions are | 
 | 1820 |       // "libdl.so" and global group. There is no point in skipping | 
 | 1821 |       // them because relocation process is going to use them | 
 | 1822 |       // in any case. | 
 | 1823 |       bool is_libdl = si == solist; | 
 | 1824 |       if (is_libdl || (si->get_dt_flags_1() & DF_1_GLOBAL) != 0 || | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1825 |           !si->is_linked() || si->get_target_sdk_version() == target_sdk_version || | 
 | 1826 |           ns != &g_default_namespace) { | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1827 |         *candidate = si; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1828 |         return false; | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1829 |       } else if (*candidate == nullptr) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1830 |         // for the different sdk version in the default namespace | 
 | 1831 |         // remember the first library. | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1832 |         *candidate = si; | 
 | 1833 |       } | 
| Ard Biesheuvel | 12c78bb | 2012-08-14 12:30:09 +0200 | [diff] [blame] | 1834 |     } | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1835 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1836 |     return true; | 
 | 1837 |   }); | 
| Ard Biesheuvel | 12c78bb | 2012-08-14 12:30:09 +0200 | [diff] [blame] | 1838 | } | 
 | 1839 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1840 | static bool find_library_internal(android_namespace_t* ns, | 
 | 1841 |                                   LoadTask* task, | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1842 |                                   ZipArchiveCache* zip_archive_cache, | 
 | 1843 |                                   LoadTaskList* load_tasks, | 
 | 1844 |                                   int rtld_flags) { | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1845 |   soinfo* candidate; | 
 | 1846 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1847 |   if (find_loaded_library_by_soname(ns, task->get_name(), &candidate)) { | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1848 |     task->set_soinfo(candidate); | 
 | 1849 |     return true; | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1850 |   } | 
| Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 1851 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1852 |   if (ns != &g_default_namespace) { | 
 | 1853 |     // check public namespace | 
 | 1854 |     candidate = g_public_namespace.find_if([&](soinfo* si) { | 
 | 1855 |       return strcmp(task->get_name(), si->get_soname()) == 0; | 
 | 1856 |     }); | 
 | 1857 |  | 
 | 1858 |     if (candidate != nullptr) { | 
 | 1859 |       ns->soinfo_list().push_back(candidate); | 
 | 1860 |       task->set_soinfo(candidate); | 
 | 1861 |       return true; | 
 | 1862 |     } | 
 | 1863 |   } | 
 | 1864 |  | 
| Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 1865 |   // Library might still be loaded, the accurate detection | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1866 |   // of this fact is done by load_library. | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1867 |   TRACE("[ '%s' find_loaded_library_by_soname returned false (*candidate=%s@%p). Trying harder...]", | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1868 |       task->get_name(), candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate); | 
| Dmitriy Ivanov | a970333 | 2015-06-16 15:38:21 -0700 | [diff] [blame] | 1869 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1870 |   if (load_library(ns, task, zip_archive_cache, load_tasks, rtld_flags)) { | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1871 |     return true; | 
 | 1872 |   } else { | 
 | 1873 |     // In case we were unable to load the library but there | 
 | 1874 |     // is a candidate loaded under the same soname but different | 
 | 1875 |     // sdk level - return it anyways. | 
 | 1876 |     if (candidate != nullptr) { | 
 | 1877 |       task->set_soinfo(candidate); | 
 | 1878 |       return true; | 
 | 1879 |     } | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1880 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1881 |  | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1882 |   return false; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1883 | } | 
 | 1884 |  | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1885 | static void soinfo_unload(soinfo* si); | 
 | 1886 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 1887 | // TODO: this is slightly unusual way to construct | 
 | 1888 | // the global group for relocation. Not every RTLD_GLOBAL | 
 | 1889 | // library is included in this group for backwards-compatibility | 
 | 1890 | // reasons. | 
 | 1891 | // | 
 | 1892 | // This group consists of the main executable, LD_PRELOADs | 
 | 1893 | // and libraries with the DF_1_GLOBAL flag set. | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1894 | static soinfo::soinfo_list_t make_global_group(android_namespace_t* ns) { | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 1895 |   soinfo::soinfo_list_t global_group; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1896 |   ns->soinfo_list().for_each([&](soinfo* si) { | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 1897 |     if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) { | 
 | 1898 |       global_group.push_back(si); | 
 | 1899 |     } | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1900 |   }); | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 1901 |  | 
 | 1902 |   return global_group; | 
 | 1903 | } | 
 | 1904 |  | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1905 | static void shuffle(std::vector<LoadTask*>* v) { | 
 | 1906 |   for (size_t i = 0, size = v->size(); i < size; ++i) { | 
 | 1907 |     size_t n = size - i; | 
 | 1908 |     size_t r = arc4random_uniform(n); | 
 | 1909 |     std::swap((*v)[n-1], (*v)[r]); | 
 | 1910 |   } | 
 | 1911 | } | 
 | 1912 |  | 
| Evgenii Stepanov | 0cdef7e | 2015-07-06 17:56:31 -0700 | [diff] [blame] | 1913 | // add_as_children - add first-level loaded libraries (i.e. library_names[], but | 
 | 1914 | // not their transitive dependencies) as children of the start_with library. | 
 | 1915 | // This is false when find_libraries is called for dlopen(), when newly loaded | 
 | 1916 | // libraries must form a disjoint tree. | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1917 | static bool find_libraries(android_namespace_t* ns, | 
 | 1918 |                            soinfo* start_with, | 
| Evgenii Stepanov | 0cdef7e | 2015-07-06 17:56:31 -0700 | [diff] [blame] | 1919 |                            const char* const library_names[], | 
 | 1920 |                            size_t library_names_count, soinfo* soinfos[], | 
 | 1921 |                            std::vector<soinfo*>* ld_preloads, | 
 | 1922 |                            size_t ld_preloads_count, int rtld_flags, | 
 | 1923 |                            const android_dlextinfo* extinfo, | 
 | 1924 |                            bool add_as_children) { | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1925 |   // Step 0: prepare. | 
 | 1926 |   LoadTaskList load_tasks; | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1927 |   std::unordered_map<const soinfo*, ElfReader> readers_map; | 
 | 1928 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1929 |   for (size_t i = 0; i < library_names_count; ++i) { | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1930 |     const char* name = library_names[i]; | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1931 |     load_tasks.push_back(LoadTask::create(name, start_with, &readers_map)); | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1932 |   } | 
 | 1933 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 1934 |   // Construct global_group. | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1935 |   soinfo::soinfo_list_t global_group = make_global_group(ns); | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 1936 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1937 |   // If soinfos array is null allocate one on stack. | 
 | 1938 |   // The array is needed in case of failure; for example | 
 | 1939 |   // when library_names[] = {libone.so, libtwo.so} and libone.so | 
 | 1940 |   // is loaded correctly but libtwo.so failed for some reason. | 
 | 1941 |   // In this case libone.so should be unloaded on return. | 
 | 1942 |   // See also implementation of failure_guard below. | 
 | 1943 |  | 
 | 1944 |   if (soinfos == nullptr) { | 
 | 1945 |     size_t soinfos_size = sizeof(soinfo*)*library_names_count; | 
 | 1946 |     soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size)); | 
 | 1947 |     memset(soinfos, 0, soinfos_size); | 
 | 1948 |   } | 
 | 1949 |  | 
 | 1950 |   // list of libraries to link - see step 2. | 
 | 1951 |   size_t soinfos_count = 0; | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1952 |  | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1953 |   auto scope_guard = make_scope_guard([&]() { | 
 | 1954 |     for (LoadTask* t : load_tasks) { | 
 | 1955 |       LoadTask::deleter(t); | 
 | 1956 |     } | 
 | 1957 |   }); | 
 | 1958 |  | 
| Dmitriy Ivanov | d9ff722 | 2014-09-08 16:22:22 -0700 | [diff] [blame] | 1959 |   auto failure_guard = make_scope_guard([&]() { | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1960 |     // Housekeeping | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1961 |     for (size_t i = 0; i<soinfos_count; ++i) { | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1962 |       soinfo_unload(soinfos[i]); | 
 | 1963 |     } | 
 | 1964 |   }); | 
 | 1965 |  | 
| Dmitriy Ivanov | b482750 | 2015-09-28 16:38:31 -0700 | [diff] [blame] | 1966 |   ZipArchiveCache zip_archive_cache; | 
 | 1967 |  | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1968 |   // Step 1: expand the list of load_tasks to include | 
 | 1969 |   // all DT_NEEDED libraries (do not load them just yet) | 
 | 1970 |   for (size_t i = 0; i<load_tasks.size(); ++i) { | 
 | 1971 |     LoadTask* task = load_tasks[i]; | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 1972 |     soinfo* needed_by = task->get_needed_by(); | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1973 |  | 
| Dmitriy Ivanov | edfc9f6 | 2015-09-02 16:32:02 -0700 | [diff] [blame] | 1974 |     bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children); | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1975 |     task->set_extinfo(is_dt_needed ? nullptr : extinfo); | 
| Dmitriy Ivanov | edfc9f6 | 2015-09-02 16:32:02 -0700 | [diff] [blame] | 1976 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 1977 |     if(!find_library_internal(ns, task, &zip_archive_cache, &load_tasks, rtld_flags)) { | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1978 |       return false; | 
 | 1979 |     } | 
 | 1980 |  | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 1981 |     soinfo* si = task->get_soinfo(); | 
 | 1982 |  | 
| Dmitriy Ivanov | edfc9f6 | 2015-09-02 16:32:02 -0700 | [diff] [blame] | 1983 |     if (is_dt_needed) { | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1984 |       needed_by->add_child(si); | 
 | 1985 |     } | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1986 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1987 |     if (si->is_linked()) { | 
 | 1988 |       si->increment_ref_count(); | 
 | 1989 |     } | 
 | 1990 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1991 |     // When ld_preloads is not null, the first | 
 | 1992 |     // ld_preloads_count libs are in fact ld_preloads. | 
 | 1993 |     if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) { | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1994 |       ld_preloads->push_back(si); | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1995 |     } | 
 | 1996 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1997 |     if (soinfos_count < library_names_count) { | 
 | 1998 |       soinfos[soinfos_count++] = si; | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1999 |     } | 
 | 2000 |   } | 
 | 2001 |  | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 2002 |   // Step 2: Load libraries in random order (see b/24047022) | 
 | 2003 |   LoadTaskList load_list; | 
 | 2004 |   for (auto&& task : load_tasks) { | 
 | 2005 |     soinfo* si = task->get_soinfo(); | 
 | 2006 |     auto pred = [&](const LoadTask* t) { | 
 | 2007 |       return t->get_soinfo() == si; | 
 | 2008 |     }; | 
 | 2009 |  | 
 | 2010 |     if (!si->is_linked() && | 
 | 2011 |         std::find_if(load_list.begin(), load_list.end(), pred) == load_list.end() ) { | 
 | 2012 |       load_list.push_back(task); | 
 | 2013 |     } | 
 | 2014 |   } | 
 | 2015 |   shuffle(&load_list); | 
 | 2016 |  | 
 | 2017 |   for (auto&& task : load_list) { | 
 | 2018 |     if (!task->load()) { | 
 | 2019 |       return false; | 
 | 2020 |     } | 
 | 2021 |   } | 
 | 2022 |  | 
 | 2023 |   // Step 3: pre-link all DT_NEEDED libraries in breadth first order. | 
 | 2024 |   for (auto&& task : load_tasks) { | 
 | 2025 |     soinfo* si = task->get_soinfo(); | 
 | 2026 |     if (!si->is_linked() && !si->prelink_image()) { | 
 | 2027 |       return false; | 
 | 2028 |     } | 
 | 2029 |   } | 
 | 2030 |  | 
 | 2031 |   // Step 4: Add LD_PRELOADed libraries to the global group for | 
 | 2032 |   // future runs. There is no need to explicitly add them to | 
 | 2033 |   // the global group for this run because they are going to | 
 | 2034 |   // appear in the local group in the correct order. | 
 | 2035 |   if (ld_preloads != nullptr) { | 
 | 2036 |     for (auto&& si : *ld_preloads) { | 
 | 2037 |       si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL); | 
 | 2038 |     } | 
 | 2039 |   } | 
 | 2040 |  | 
 | 2041 |  | 
 | 2042 |   // Step 5: link libraries. | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 2043 |   soinfo::soinfo_list_t local_group; | 
 | 2044 |   walk_dependencies_tree( | 
| Evgenii Stepanov | 0cdef7e | 2015-07-06 17:56:31 -0700 | [diff] [blame] | 2045 |       (start_with != nullptr && add_as_children) ? &start_with : soinfos, | 
 | 2046 |       (start_with != nullptr && add_as_children) ? 1 : soinfos_count, | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 2047 |       [&] (soinfo* si) { | 
 | 2048 |     local_group.push_back(si); | 
 | 2049 |     return true; | 
 | 2050 |   }); | 
 | 2051 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2052 |   // We need to increment ref_count in case | 
 | 2053 |   // the root of the local group was not linked. | 
 | 2054 |   bool was_local_group_root_linked = local_group.front()->is_linked(); | 
 | 2055 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 2056 |   bool linked = local_group.visit([&](soinfo* si) { | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2057 |     if (!si->is_linked()) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2058 |       if (!si->link_image(global_group, local_group, extinfo)) { | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2059 |         return false; | 
 | 2060 |       } | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2061 |       si->set_linked(); | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2062 |     } | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 2063 |  | 
 | 2064 |     return true; | 
 | 2065 |   }); | 
 | 2066 |  | 
 | 2067 |   if (linked) { | 
 | 2068 |     failure_guard.disable(); | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2069 |   } | 
 | 2070 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2071 |   if (!was_local_group_root_linked) { | 
 | 2072 |     local_group.front()->increment_ref_count(); | 
 | 2073 |   } | 
 | 2074 |  | 
| Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 2075 |   return linked; | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2076 | } | 
 | 2077 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2078 | static soinfo* find_library(android_namespace_t* ns, | 
 | 2079 |                             const char* name, int rtld_flags, | 
| Evgenii Stepanov | 0cdef7e | 2015-07-06 17:56:31 -0700 | [diff] [blame] | 2080 |                             const android_dlextinfo* extinfo, | 
 | 2081 |                             soinfo* needed_by) { | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2082 |   soinfo* si; | 
 | 2083 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2084 |   if (name == nullptr) { | 
 | 2085 |     si = somain; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2086 |   } else if (!find_libraries(ns, needed_by, &name, 1, &si, nullptr, 0, rtld_flags, | 
| Evgenii Stepanov | 0cdef7e | 2015-07-06 17:56:31 -0700 | [diff] [blame] | 2087 |                              extinfo, /* add_as_children */ false)) { | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2088 |     return nullptr; | 
 | 2089 |   } | 
 | 2090 |  | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2091 |   return si; | 
 | 2092 | } | 
| Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 2093 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2094 | static void soinfo_unload(soinfo* root) { | 
 | 2095 |   // Note that the library can be loaded but not linked; | 
 | 2096 |   // in which case there is no root but we still need | 
 | 2097 |   // to walk the tree and unload soinfos involved. | 
 | 2098 |   // | 
 | 2099 |   // This happens on unsuccessful dlopen, when one of | 
 | 2100 |   // the DT_NEEDED libraries could not be linked/found. | 
 | 2101 |   if (root->is_linked()) { | 
 | 2102 |     root = root->get_local_group_root(); | 
 | 2103 |   } | 
 | 2104 |  | 
 | 2105 |   if (!root->can_unload()) { | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2106 |     TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_realpath()); | 
| Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 2107 |     return; | 
 | 2108 |   } | 
 | 2109 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2110 |   size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2111 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2112 |   if (ref_count == 0) { | 
 | 2113 |     soinfo::soinfo_list_t local_unload_list; | 
 | 2114 |     soinfo::soinfo_list_t external_unload_list; | 
 | 2115 |     soinfo::soinfo_list_t depth_first_list; | 
 | 2116 |     depth_first_list.push_back(root); | 
 | 2117 |     soinfo* si = nullptr; | 
 | 2118 |  | 
 | 2119 |     while ((si = depth_first_list.pop_front()) != nullptr) { | 
| Dmitriy Ivanov | 5ae82cb | 2014-12-02 17:08:42 -0800 | [diff] [blame] | 2120 |       if (local_unload_list.contains(si)) { | 
 | 2121 |         continue; | 
 | 2122 |       } | 
 | 2123 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2124 |       local_unload_list.push_back(si); | 
| Dmitriy Ivanov | 5ae82cb | 2014-12-02 17:08:42 -0800 | [diff] [blame] | 2125 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2126 |       if (si->has_min_version(0)) { | 
 | 2127 |         soinfo* child = nullptr; | 
 | 2128 |         while ((child = si->get_children().pop_front()) != nullptr) { | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2129 |           TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si, | 
 | 2130 |               child->get_realpath(), child); | 
 | 2131 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2132 |           if (local_unload_list.contains(child)) { | 
 | 2133 |             continue; | 
| Dmitriy Ivanov | 5ae82cb | 2014-12-02 17:08:42 -0800 | [diff] [blame] | 2134 |           } else if (child->is_linked() && child->get_local_group_root() != root) { | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2135 |             external_unload_list.push_back(child); | 
 | 2136 |           } else { | 
 | 2137 |             depth_first_list.push_front(child); | 
 | 2138 |           } | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2139 |         } | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2140 |       } else { | 
| Dmitriy Ivanov | 280d546 | 2015-09-28 10:14:17 -0700 | [diff] [blame] | 2141 | #if !defined(__work_around_b_24465209__) | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2142 |         __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si); | 
| Dmitriy Ivanov | 5ae82cb | 2014-12-02 17:08:42 -0800 | [diff] [blame] | 2143 | #else | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2144 |         PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2145 |         for_each_dt_needed(si, [&] (const char* library_name) { | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2146 |           TRACE("deprecated (old format of soinfo): %s needs to unload %s", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2147 |               si->get_realpath(), library_name); | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2148 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2149 |           soinfo* needed = find_library(si->get_namespace(), | 
 | 2150 |                                         library_name, RTLD_NOLOAD, nullptr, nullptr); | 
 | 2151 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2152 |           if (needed != nullptr) { | 
 | 2153 |             // Not found: for example if symlink was deleted between dlopen and dlclose | 
 | 2154 |             // Since we cannot really handle errors at this point - print and continue. | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2155 |             PRINT("warning: couldn't find %s needed by %s on unload.", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2156 |                 library_name, si->get_realpath()); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2157 |             return; | 
 | 2158 |           } else if (local_unload_list.contains(needed)) { | 
 | 2159 |             // already visited | 
 | 2160 |             return; | 
| Dmitriy Ivanov | 5ae82cb | 2014-12-02 17:08:42 -0800 | [diff] [blame] | 2161 |           } else if (needed->is_linked() && needed->get_local_group_root() != root) { | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2162 |             // external group | 
 | 2163 |             external_unload_list.push_back(needed); | 
 | 2164 |           } else { | 
 | 2165 |             // local group | 
 | 2166 |             depth_first_list.push_front(needed); | 
 | 2167 |           } | 
 | 2168 |         }); | 
| Dmitriy Ivanov | 5ae82cb | 2014-12-02 17:08:42 -0800 | [diff] [blame] | 2169 | #endif | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2170 |       } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2171 |     } | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2172 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2173 |     local_unload_list.for_each([](soinfo* si) { | 
 | 2174 |       si->call_destructors(); | 
 | 2175 |     }); | 
 | 2176 |  | 
 | 2177 |     while ((si = local_unload_list.pop_front()) != nullptr) { | 
 | 2178 |       notify_gdb_of_unload(si); | 
 | 2179 |       soinfo_free(si); | 
 | 2180 |     } | 
 | 2181 |  | 
 | 2182 |     while ((si = external_unload_list.pop_front()) != nullptr) { | 
 | 2183 |       soinfo_unload(si); | 
 | 2184 |     } | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2185 |   } else { | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2186 |     TRACE("not unloading '%s' group, decrementing ref_count to %zd", | 
 | 2187 |         root->get_realpath(), ref_count); | 
| Dmitriy Ivanov | a254705 | 2014-11-18 12:03:09 -0800 | [diff] [blame] | 2188 |   } | 
 | 2189 | } | 
 | 2190 |  | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 2191 | static std::string symbol_display_name(const char* sym_name, const char* sym_ver) { | 
 | 2192 |   if (sym_ver == nullptr) { | 
 | 2193 |     return sym_name; | 
 | 2194 |   } | 
 | 2195 |  | 
| Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 2196 |   return std::string(sym_name) + ", version " + sym_ver; | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 2197 | } | 
 | 2198 |  | 
| Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 2199 | void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) { | 
| Christopher Ferris | 052fa3a | 2014-08-26 20:48:11 -0700 | [diff] [blame] | 2200 |   // Use basic string manipulation calls to avoid snprintf. | 
 | 2201 |   // snprintf indirectly calls pthread_getspecific to get the size of a buffer. | 
 | 2202 |   // When debug malloc is enabled, this call returns 0. This in turn causes | 
 | 2203 |   // snprintf to do nothing, which causes libraries to fail to load. | 
 | 2204 |   // See b/17302493 for further details. | 
 | 2205 |   // Once the above bug is fixed, this code can be modified to use | 
 | 2206 |   // snprintf again. | 
| Evgenii Stepanov | d640b22 | 2015-07-10 17:54:01 -0700 | [diff] [blame] | 2207 |   size_t required_len = 0; | 
 | 2208 |   for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) { | 
 | 2209 |     required_len += strlen(g_default_ld_paths[i]) + 1; | 
 | 2210 |   } | 
| Christopher Ferris | 052fa3a | 2014-08-26 20:48:11 -0700 | [diff] [blame] | 2211 |   if (buffer_size < required_len) { | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2212 |     __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: " | 
 | 2213 |                  "buffer len %zu, required len %zu", buffer_size, required_len); | 
| Christopher Ferris | 052fa3a | 2014-08-26 20:48:11 -0700 | [diff] [blame] | 2214 |   } | 
| Evgenii Stepanov | d640b22 | 2015-07-10 17:54:01 -0700 | [diff] [blame] | 2215 |   char* end = buffer; | 
 | 2216 |   for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) { | 
 | 2217 |     if (i > 0) *end++ = ':'; | 
 | 2218 |     end = stpcpy(end, g_default_ld_paths[i]); | 
 | 2219 |   } | 
| Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 2220 | } | 
 | 2221 |  | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 2222 | void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) { | 
| Nick Kralevich | 6bb01b6 | 2015-03-07 13:37:05 -0800 | [diff] [blame] | 2223 |   parse_LD_LIBRARY_PATH(ld_library_path); | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 2224 | } | 
 | 2225 |  | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 2226 | soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo, | 
 | 2227 |                   void* caller_addr) { | 
 | 2228 |   soinfo* const caller = find_containing_library(caller_addr); | 
 | 2229 |  | 
| Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 2230 |   if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) { | 
| Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 2231 |     DL_ERR("invalid flags to dlopen: %x", flags); | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 2232 |     return nullptr; | 
| Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 2233 |   } | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2234 |  | 
| Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 2235 |   android_namespace_t* ns = caller != nullptr ? caller->get_namespace() : g_anonymous_namespace; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2236 |  | 
| Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 2237 |   if (extinfo != nullptr) { | 
 | 2238 |     if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) { | 
 | 2239 |       DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags); | 
 | 2240 |       return nullptr; | 
 | 2241 |     } | 
| Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 2242 |  | 
| Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 2243 |     if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 && | 
| Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 2244 |         (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) { | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2245 |       DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without " | 
 | 2246 |           "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags); | 
| Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 2247 |       return nullptr; | 
 | 2248 |     } | 
| Dmitriy Ivanov | 126af75 | 2015-10-07 16:34:20 -0700 | [diff] [blame] | 2249 |  | 
 | 2250 |     if ((extinfo->flags & ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS) != 0 && | 
 | 2251 |         (extinfo->flags & (ANDROID_DLEXT_RESERVED_ADDRESS | ANDROID_DLEXT_RESERVED_ADDRESS_HINT)) != 0) { | 
 | 2252 |       DL_ERR("invalid extended flag combination: ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS is not " | 
 | 2253 |              "compatible with ANDROID_DLEXT_RESERVED_ADDRESS/ANDROID_DLEXT_RESERVED_ADDRESS_HINT"); | 
 | 2254 |       return nullptr; | 
 | 2255 |     } | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2256 |  | 
 | 2257 |     if ((extinfo->flags & ANDROID_DLEXT_USE_NAMESPACE) != 0) { | 
 | 2258 |       if (extinfo->library_namespace == nullptr) { | 
 | 2259 |         DL_ERR("ANDROID_DLEXT_USE_NAMESPACE is set but extinfo->library_namespace is null"); | 
 | 2260 |         return nullptr; | 
 | 2261 |       } | 
 | 2262 |       ns = extinfo->library_namespace; | 
 | 2263 |     } | 
| Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 2264 |   } | 
| Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 2265 |  | 
 | 2266 |   ProtectedDataGuard guard; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2267 |   soinfo* si = find_library(ns, name, flags, extinfo, caller); | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 2268 |   if (si != nullptr) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2269 |     si->call_constructors(); | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2270 |   } | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2271 |  | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2272 |   return si; | 
 | 2273 | } | 
 | 2274 |  | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 2275 | int do_dladdr(const void* addr, Dl_info* info) { | 
 | 2276 |   // Determine if this address can be found in any library currently mapped. | 
 | 2277 |   soinfo* si = find_containing_library(addr); | 
 | 2278 |   if (si == nullptr) { | 
 | 2279 |     return 0; | 
 | 2280 |   } | 
 | 2281 |  | 
 | 2282 |   memset(info, 0, sizeof(Dl_info)); | 
 | 2283 |  | 
 | 2284 |   info->dli_fname = si->get_realpath(); | 
 | 2285 |   // Address at which the shared object is loaded. | 
 | 2286 |   info->dli_fbase = reinterpret_cast<void*>(si->base); | 
 | 2287 |  | 
 | 2288 |   // Determine if any symbol in the library contains the specified address. | 
 | 2289 |   ElfW(Sym)* sym = si->find_symbol_by_address(addr); | 
 | 2290 |   if (sym != nullptr) { | 
 | 2291 |     info->dli_sname = si->get_string(sym->st_name); | 
 | 2292 |     info->dli_saddr = reinterpret_cast<void*>(si->resolve_symbol_address(sym)); | 
 | 2293 |   } | 
 | 2294 |  | 
 | 2295 |   return 1; | 
 | 2296 | } | 
 | 2297 |  | 
 | 2298 | bool do_dlsym(void* handle, const char* sym_name, const char* sym_ver, | 
 | 2299 |               void* caller_addr, void** symbol) { | 
 | 2300 | #if !defined(__LP64__) | 
 | 2301 |   if (handle == nullptr) { | 
 | 2302 |     DL_ERR("dlsym failed: library handle is null"); | 
 | 2303 |     return false; | 
 | 2304 |   } | 
 | 2305 | #endif | 
 | 2306 |  | 
 | 2307 |   if (sym_name == nullptr) { | 
 | 2308 |     DL_ERR("dlsym failed: symbol name is null"); | 
 | 2309 |     return false; | 
 | 2310 |   } | 
 | 2311 |  | 
 | 2312 |   soinfo* found = nullptr; | 
 | 2313 |   const ElfW(Sym)* sym = nullptr; | 
 | 2314 |   soinfo* caller = find_containing_library(caller_addr); | 
 | 2315 |   android_namespace_t* ns = caller != nullptr ? caller->get_namespace() : g_anonymous_namespace; | 
 | 2316 |  | 
 | 2317 |   version_info vi_instance; | 
 | 2318 |   version_info* vi = nullptr; | 
 | 2319 |  | 
 | 2320 |   if (sym_ver != nullptr) { | 
| Dimitry Ivanov | 9cf99cb | 2015-12-11 14:22:24 -0800 | [diff] [blame] | 2321 |     vi_instance.name = sym_ver; | 
 | 2322 |     vi_instance.elf_hash = calculate_elf_hash(sym_ver); | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 2323 |     vi = &vi_instance; | 
 | 2324 |   } | 
 | 2325 |  | 
 | 2326 |   if (handle == RTLD_DEFAULT || handle == RTLD_NEXT) { | 
 | 2327 |     sym = dlsym_linear_lookup(ns, sym_name, vi, &found, caller, handle); | 
 | 2328 |   } else { | 
 | 2329 |     sym = dlsym_handle_lookup(reinterpret_cast<soinfo*>(handle), &found, sym_name, vi); | 
 | 2330 |   } | 
 | 2331 |  | 
 | 2332 |   if (sym != nullptr) { | 
 | 2333 |     uint32_t bind = ELF_ST_BIND(sym->st_info); | 
 | 2334 |  | 
 | 2335 |     if ((bind == STB_GLOBAL || bind == STB_WEAK) && sym->st_shndx != 0) { | 
 | 2336 |       *symbol = reinterpret_cast<void*>(found->resolve_symbol_address(sym)); | 
 | 2337 |       return true; | 
 | 2338 |     } | 
 | 2339 |  | 
 | 2340 |     DL_ERR("symbol \"%s\" found but not global", symbol_display_name(sym_name, sym_ver).c_str()); | 
 | 2341 |     return false; | 
 | 2342 |   } | 
 | 2343 |  | 
 | 2344 |   DL_ERR("undefined symbol: %s", symbol_display_name(sym_name, sym_ver).c_str()); | 
 | 2345 |   return false; | 
 | 2346 | } | 
 | 2347 |  | 
| Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 2348 | void do_dlclose(soinfo* si) { | 
| Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 2349 |   ProtectedDataGuard guard; | 
| Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 2350 |   soinfo_unload(si); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2351 | } | 
 | 2352 |  | 
| Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 2353 | bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path) { | 
 | 2354 |   CHECK(public_ns_sonames != nullptr); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2355 |   if (g_public_namespace_initialized) { | 
| Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 2356 |     DL_ERR("public namespace has already been initialized."); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2357 |     return false; | 
 | 2358 |   } | 
 | 2359 |  | 
| Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 2360 |   std::vector<std::string> sonames = android::base::Split(public_ns_sonames, ":"); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2361 |  | 
 | 2362 |   ProtectedDataGuard guard; | 
 | 2363 |  | 
 | 2364 |   auto failure_guard = make_scope_guard([&]() { | 
 | 2365 |     g_public_namespace.clear(); | 
 | 2366 |   }); | 
 | 2367 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2368 |   for (const auto& soname : sonames) { | 
| Dmitriy Ivanov | 3cc35e2 | 2015-11-17 18:36:50 -0800 | [diff] [blame] | 2369 |     soinfo* candidate = nullptr; | 
 | 2370 |  | 
 | 2371 |     find_loaded_library_by_soname(&g_default_namespace, soname.c_str(), &candidate); | 
 | 2372 |  | 
 | 2373 |     if (candidate == nullptr) { | 
| Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 2374 |       DL_ERR("error initializing public namespace: \"%s\" was not found" | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2375 |              " in the default namespace", soname.c_str()); | 
 | 2376 |       return false; | 
 | 2377 |     } | 
 | 2378 |  | 
 | 2379 |     candidate->set_nodelete(); | 
 | 2380 |     g_public_namespace.push_back(candidate); | 
 | 2381 |   } | 
 | 2382 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2383 |   g_public_namespace_initialized = true; | 
| Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 2384 |  | 
 | 2385 |   // create anonymous namespace | 
| Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 2386 |   // When the caller is nullptr - create_namespace will take global group | 
 | 2387 |   // from the anonymous namespace, which is fine because anonymous namespace | 
 | 2388 |   // is still pointing to the default one. | 
| Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 2389 |   android_namespace_t* anon_ns = | 
| Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 2390 |       create_namespace(nullptr, "(anonymous)", nullptr, anon_ns_library_path, | 
 | 2391 |                        ANDROID_NAMESPACE_TYPE_REGULAR, nullptr); | 
| Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 2392 |  | 
 | 2393 |   if (anon_ns == nullptr) { | 
 | 2394 |     g_public_namespace_initialized = false; | 
 | 2395 |     return false; | 
 | 2396 |   } | 
 | 2397 |   g_anonymous_namespace = anon_ns; | 
 | 2398 |   failure_guard.disable(); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2399 |   return true; | 
 | 2400 | } | 
 | 2401 |  | 
| Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 2402 | android_namespace_t* create_namespace(const void* caller_addr, | 
 | 2403 |                                       const char* name, | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2404 |                                       const char* ld_library_path, | 
 | 2405 |                                       const char* default_library_path, | 
| Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 2406 |                                       uint64_t type, | 
| Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 2407 |                                       const char* permitted_when_isolated_path) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2408 |   if (!g_public_namespace_initialized) { | 
| Dmitriy Ivanov | 1ffec1c | 2015-11-23 11:26:35 -0800 | [diff] [blame] | 2409 |     DL_ERR("cannot create namespace: public namespace is not initialized."); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2410 |     return nullptr; | 
 | 2411 |   } | 
 | 2412 |  | 
| Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 2413 |   soinfo* caller_soinfo = find_containing_library(caller_addr); | 
 | 2414 |  | 
 | 2415 |   android_namespace_t* caller_ns = caller_soinfo != nullptr ? | 
 | 2416 |                                    caller_soinfo->get_namespace() : | 
 | 2417 |                                    g_anonymous_namespace; | 
 | 2418 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2419 |   ProtectedDataGuard guard; | 
 | 2420 |   std::vector<std::string> ld_library_paths; | 
 | 2421 |   std::vector<std::string> default_library_paths; | 
| Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 2422 |   std::vector<std::string> permitted_paths; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2423 |  | 
 | 2424 |   parse_path(ld_library_path, ":", &ld_library_paths); | 
 | 2425 |   parse_path(default_library_path, ":", &default_library_paths); | 
| Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 2426 |   parse_path(permitted_when_isolated_path, ":", &permitted_paths); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2427 |  | 
 | 2428 |   android_namespace_t* ns = new (g_namespace_allocator.alloc()) android_namespace_t(); | 
 | 2429 |   ns->set_name(name); | 
| Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 2430 |   ns->set_isolated((type & ANDROID_NAMESPACE_TYPE_ISOLATED) != 0); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2431 |   ns->set_ld_library_paths(std::move(ld_library_paths)); | 
 | 2432 |   ns->set_default_library_paths(std::move(default_library_paths)); | 
| Dimitry Ivanov | 284ae35 | 2015-12-08 10:47:13 -0800 | [diff] [blame] | 2433 |   ns->set_permitted_paths(std::move(permitted_paths)); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2434 |  | 
| Dimitry Ivanov | 7331fe1 | 2015-12-14 14:11:17 -0800 | [diff] [blame] | 2435 |   if ((type & ANDROID_NAMESPACE_TYPE_SHARED) != 0) { | 
 | 2436 |     // If shared - clone the caller namespace | 
 | 2437 |     auto& soinfo_list = caller_ns->soinfo_list(); | 
 | 2438 |     std::copy(soinfo_list.begin(), soinfo_list.end(), std::back_inserter(ns->soinfo_list())); | 
 | 2439 |   } else { | 
 | 2440 |     // If not shared - copy only the global group | 
 | 2441 |     auto global_group = make_global_group(caller_ns); | 
 | 2442 |     std::copy(global_group.begin(), global_group.end(), std::back_inserter(ns->soinfo_list())); | 
 | 2443 |   } | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 2444 |  | 
 | 2445 |   return ns; | 
 | 2446 | } | 
 | 2447 |  | 
| Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 2448 | static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) { | 
 | 2449 |   typedef ElfW(Addr) (*ifunc_resolver_t)(void); | 
 | 2450 |   ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr); | 
 | 2451 |   ElfW(Addr) ifunc_addr = ifunc_resolver(); | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2452 |   TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p", | 
 | 2453 |       ifunc_resolver, reinterpret_cast<void*>(ifunc_addr)); | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 2454 |  | 
| Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 2455 |   return ifunc_addr; | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 2456 | } | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 2457 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2458 | const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const { | 
 | 2459 |   if (source_symver < 2 || | 
 | 2460 |       source_symver >= version_infos.size() || | 
 | 2461 |       version_infos[source_symver].name == nullptr) { | 
 | 2462 |     return nullptr; | 
 | 2463 |   } | 
 | 2464 |  | 
 | 2465 |   return &version_infos[source_symver]; | 
 | 2466 | } | 
 | 2467 |  | 
 | 2468 | void VersionTracker::add_version_info(size_t source_index, | 
 | 2469 |                                       ElfW(Word) elf_hash, | 
 | 2470 |                                       const char* ver_name, | 
 | 2471 |                                       const soinfo* target_si) { | 
 | 2472 |   if (source_index >= version_infos.size()) { | 
 | 2473 |     version_infos.resize(source_index+1); | 
 | 2474 |   } | 
 | 2475 |  | 
 | 2476 |   version_infos[source_index].elf_hash = elf_hash; | 
 | 2477 |   version_infos[source_index].name = ver_name; | 
 | 2478 |   version_infos[source_index].target_si = target_si; | 
 | 2479 | } | 
 | 2480 |  | 
 | 2481 | bool VersionTracker::init_verneed(const soinfo* si_from) { | 
 | 2482 |   uintptr_t verneed_ptr = si_from->get_verneed_ptr(); | 
 | 2483 |  | 
 | 2484 |   if (verneed_ptr == 0) { | 
 | 2485 |     return true; | 
 | 2486 |   } | 
 | 2487 |  | 
 | 2488 |   size_t verneed_cnt = si_from->get_verneed_cnt(); | 
 | 2489 |  | 
 | 2490 |   for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) { | 
 | 2491 |     const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset); | 
 | 2492 |     size_t vernaux_offset = offset + verneed->vn_aux; | 
 | 2493 |     offset += verneed->vn_next; | 
 | 2494 |  | 
 | 2495 |     if (verneed->vn_version != 1) { | 
 | 2496 |       DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version); | 
 | 2497 |       return false; | 
 | 2498 |     } | 
 | 2499 |  | 
 | 2500 |     const char* target_soname = si_from->get_string(verneed->vn_file); | 
 | 2501 |     // find it in dependencies | 
 | 2502 |     soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) { | 
| Dmitriy Ivanov | 406d996 | 2015-05-06 11:05:27 -0700 | [diff] [blame] | 2503 |       return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0; | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2504 |     }); | 
 | 2505 |  | 
 | 2506 |     if (target_si == nullptr) { | 
 | 2507 |       DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2508 |           target_soname, i, si_from->get_realpath()); | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2509 |       return false; | 
 | 2510 |     } | 
 | 2511 |  | 
 | 2512 |     for (size_t j = 0; j<verneed->vn_cnt; ++j) { | 
 | 2513 |       const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset); | 
 | 2514 |       vernaux_offset += vernaux->vna_next; | 
 | 2515 |  | 
 | 2516 |       const ElfW(Word) elf_hash = vernaux->vna_hash; | 
 | 2517 |       const char* ver_name = si_from->get_string(vernaux->vna_name); | 
 | 2518 |       ElfW(Half) source_index = vernaux->vna_other; | 
 | 2519 |  | 
 | 2520 |       add_version_info(source_index, elf_hash, ver_name, target_si); | 
 | 2521 |     } | 
 | 2522 |   } | 
 | 2523 |  | 
 | 2524 |   return true; | 
 | 2525 | } | 
 | 2526 |  | 
 | 2527 | bool VersionTracker::init_verdef(const soinfo* si_from) { | 
 | 2528 |   return for_each_verdef(si_from, | 
 | 2529 |     [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) { | 
 | 2530 |       add_version_info(verdef->vd_ndx, verdef->vd_hash, | 
 | 2531 |           si_from->get_string(verdaux->vda_name), si_from); | 
 | 2532 |       return false; | 
 | 2533 |     } | 
 | 2534 |   ); | 
 | 2535 | } | 
 | 2536 |  | 
 | 2537 | bool VersionTracker::init(const soinfo* si_from) { | 
 | 2538 |   if (!si_from->has_min_version(2)) { | 
 | 2539 |     return true; | 
 | 2540 |   } | 
 | 2541 |  | 
 | 2542 |   return init_verneed(si_from) && init_verdef(si_from); | 
 | 2543 | } | 
 | 2544 |  | 
| Dmitriy Ivanov | 31b408d | 2015-04-30 16:11:48 -0700 | [diff] [blame] | 2545 | bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym, | 
 | 2546 |                                  const char* sym_name, const version_info** vi) { | 
 | 2547 |   const ElfW(Versym)* sym_ver_ptr = get_versym(sym); | 
 | 2548 |   ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr; | 
 | 2549 |  | 
 | 2550 |   if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) { | 
 | 2551 |     *vi = version_tracker.get_version_info(sym_ver); | 
 | 2552 |  | 
 | 2553 |     if (*vi == nullptr) { | 
 | 2554 |       DL_ERR("cannot find verneed/verdef for version index=%d " | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2555 |           "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath()); | 
| Dmitriy Ivanov | 31b408d | 2015-04-30 16:11:48 -0700 | [diff] [blame] | 2556 |       return false; | 
 | 2557 |     } | 
 | 2558 |   } else { | 
 | 2559 |     // there is no version info | 
 | 2560 |     *vi = nullptr; | 
 | 2561 |   } | 
 | 2562 |  | 
 | 2563 |   return true; | 
 | 2564 | } | 
 | 2565 |  | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2566 | #if !defined(__mips__) | 
| Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 2567 | #if defined(USE_RELA) | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2568 | static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) { | 
 | 2569 |   return rela->r_addend; | 
 | 2570 | } | 
 | 2571 | #else | 
 | 2572 | static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) { | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2573 |   if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE || | 
 | 2574 |       ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) { | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2575 |     return *reinterpret_cast<ElfW(Addr)*>(reloc_addr); | 
 | 2576 |   } | 
 | 2577 |   return 0; | 
 | 2578 | } | 
 | 2579 | #endif | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2580 |  | 
| Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 2581 | template<typename ElfRelIteratorT> | 
| Dmitriy Ivanov | 7e4bbba | 2015-04-30 19:49:19 -0700 | [diff] [blame] | 2582 | bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator, | 
 | 2583 |                       const soinfo_list_t& global_group, const soinfo_list_t& local_group) { | 
| Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 2584 |   for (size_t idx = 0; rel_iterator.has_next(); ++idx) { | 
 | 2585 |     const auto rel = rel_iterator.next(); | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2586 |     if (rel == nullptr) { | 
 | 2587 |       return false; | 
 | 2588 |     } | 
 | 2589 |  | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2590 |     ElfW(Word) type = ELFW(R_TYPE)(rel->r_info); | 
 | 2591 |     ElfW(Word) sym = ELFW(R_SYM)(rel->r_info); | 
 | 2592 |  | 
 | 2593 |     ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias); | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2594 |     ElfW(Addr) sym_addr = 0; | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 2595 |     const char* sym_name = nullptr; | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2596 |     ElfW(Addr) addend = get_addend(rel, reloc); | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2597 |  | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2598 |     DEBUG("Processing '%s' relocation at index %zd", get_realpath(), idx); | 
| Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 2599 |     if (type == R_GENERIC_NONE) { | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2600 |       continue; | 
 | 2601 |     } | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2602 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2603 |     const ElfW(Sym)* s = nullptr; | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2604 |     soinfo* lsi = nullptr; | 
 | 2605 |  | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2606 |     if (sym != 0) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2607 |       sym_name = get_string(symtab_[sym].st_name); | 
| Dmitriy Ivanov | 31b408d | 2015-04-30 16:11:48 -0700 | [diff] [blame] | 2608 |       const version_info* vi = nullptr; | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2609 |  | 
| Dmitriy Ivanov | 31b408d | 2015-04-30 16:11:48 -0700 | [diff] [blame] | 2610 |       if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) { | 
 | 2611 |         return false; | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2612 |       } | 
| Dmitriy Ivanov | 31b408d | 2015-04-30 16:11:48 -0700 | [diff] [blame] | 2613 |  | 
 | 2614 |       if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) { | 
 | 2615 |         return false; | 
 | 2616 |       } | 
 | 2617 |  | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 2618 |       if (s == nullptr) { | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2619 |         // We only allow an undefined symbol if this is a weak reference... | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2620 |         s = &symtab_[sym]; | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2621 |         if (ELF_ST_BIND(s->st_info) != STB_WEAK) { | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2622 |           DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath()); | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 2623 |           return false; | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2624 |         } | 
 | 2625 |  | 
 | 2626 |         /* IHI0044C AAELF 4.5.1.1: | 
 | 2627 |  | 
 | 2628 |            Libraries are not searched to resolve weak references. | 
 | 2629 |            It is not an error for a weak reference to remain unsatisfied. | 
 | 2630 |  | 
 | 2631 |            During linking, the value of an undefined weak reference is: | 
 | 2632 |            - Zero if the relocation type is absolute | 
 | 2633 |            - The address of the place if the relocation is pc-relative | 
 | 2634 |            - The address of nominal base address if the relocation | 
 | 2635 |              type is base-relative. | 
 | 2636 |          */ | 
 | 2637 |  | 
 | 2638 |         switch (type) { | 
| Dmitriy Ivanov | 1b69469 | 2015-01-13 12:17:31 -0800 | [diff] [blame] | 2639 |           case R_GENERIC_JUMP_SLOT: | 
 | 2640 |           case R_GENERIC_GLOB_DAT: | 
 | 2641 |           case R_GENERIC_RELATIVE: | 
 | 2642 |           case R_GENERIC_IRELATIVE: | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2643 | #if defined(__aarch64__) | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2644 |           case R_AARCH64_ABS64: | 
 | 2645 |           case R_AARCH64_ABS32: | 
 | 2646 |           case R_AARCH64_ABS16: | 
| Dmitriy Ivanov | 1b69469 | 2015-01-13 12:17:31 -0800 | [diff] [blame] | 2647 | #elif defined(__x86_64__) | 
 | 2648 |           case R_X86_64_32: | 
 | 2649 |           case R_X86_64_64: | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2650 | #elif defined(__arm__) | 
 | 2651 |           case R_ARM_ABS32: | 
 | 2652 | #elif defined(__i386__) | 
 | 2653 |           case R_386_32: | 
| Dmitriy Ivanov | 1b69469 | 2015-01-13 12:17:31 -0800 | [diff] [blame] | 2654 | #endif | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2655 |             /* | 
 | 2656 |              * The sym_addr was initialized to be zero above, or the relocation | 
 | 2657 |              * code below does not care about value of sym_addr. | 
 | 2658 |              * No need to do anything. | 
 | 2659 |              */ | 
 | 2660 |             break; | 
| Dmitriy Ivanov | 1b69469 | 2015-01-13 12:17:31 -0800 | [diff] [blame] | 2661 | #if defined(__x86_64__) | 
| Dimitry Ivanov | d338aac | 2015-01-13 22:31:54 +0000 | [diff] [blame] | 2662 |           case R_X86_64_PC32: | 
 | 2663 |             sym_addr = reloc; | 
 | 2664 |             break; | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2665 | #elif defined(__i386__) | 
 | 2666 |           case R_386_PC32: | 
 | 2667 |             sym_addr = reloc; | 
 | 2668 |             break; | 
 | 2669 | #endif | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2670 |           default: | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2671 |             DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx); | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 2672 |             return false; | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2673 |         } | 
| Dmitriy Ivanov | ec83a61 | 2015-07-26 07:37:09 -0700 | [diff] [blame] | 2674 |       } else { // We got a definition. | 
 | 2675 | #if !defined(__LP64__) | 
 | 2676 |         // When relocating dso with text_relocation .text segment is | 
 | 2677 |         // not executable. We need to restore elf flags before resolving | 
 | 2678 |         // STT_GNU_IFUNC symbol. | 
 | 2679 |         bool protect_segments = has_text_relocations && | 
 | 2680 |                                 lsi == this && | 
 | 2681 |                                 ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC; | 
 | 2682 |         if (protect_segments) { | 
 | 2683 |           if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) { | 
 | 2684 |             DL_ERR("can't protect segments for \"%s\": %s", | 
 | 2685 |                    get_realpath(), strerror(errno)); | 
 | 2686 |             return false; | 
 | 2687 |           } | 
 | 2688 |         } | 
 | 2689 | #endif | 
| Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 2690 |         sym_addr = lsi->resolve_symbol_address(s); | 
| Dmitriy Ivanov | ec83a61 | 2015-07-26 07:37:09 -0700 | [diff] [blame] | 2691 | #if !defined(__LP64__) | 
 | 2692 |         if (protect_segments) { | 
 | 2693 |           if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) { | 
 | 2694 |             DL_ERR("can't unprotect loadable segments for \"%s\": %s", | 
 | 2695 |                    get_realpath(), strerror(errno)); | 
 | 2696 |             return false; | 
 | 2697 |           } | 
 | 2698 |         } | 
 | 2699 | #endif | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2700 |       } | 
 | 2701 |       count_relocation(kRelocSymbol); | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2702 |     } | 
 | 2703 |  | 
 | 2704 |     switch (type) { | 
| Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 2705 |       case R_GENERIC_JUMP_SLOT: | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2706 |         count_relocation(kRelocAbsolute); | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2707 |         MARK(rel->r_offset); | 
 | 2708 |         TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n", | 
 | 2709 |                    reinterpret_cast<void*>(reloc), | 
 | 2710 |                    reinterpret_cast<void*>(sym_addr + addend), sym_name); | 
 | 2711 |  | 
 | 2712 |         *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend); | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2713 |         break; | 
| Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 2714 |       case R_GENERIC_GLOB_DAT: | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2715 |         count_relocation(kRelocAbsolute); | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2716 |         MARK(rel->r_offset); | 
 | 2717 |         TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n", | 
 | 2718 |                    reinterpret_cast<void*>(reloc), | 
 | 2719 |                    reinterpret_cast<void*>(sym_addr + addend), sym_name); | 
 | 2720 |         *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend); | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2721 |         break; | 
| Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 2722 |       case R_GENERIC_RELATIVE: | 
 | 2723 |         count_relocation(kRelocRelative); | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2724 |         MARK(rel->r_offset); | 
 | 2725 |         TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n", | 
 | 2726 |                    reinterpret_cast<void*>(reloc), | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2727 |                    reinterpret_cast<void*>(load_bias + addend)); | 
 | 2728 |         *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend); | 
| Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 2729 |         break; | 
| Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 2730 |       case R_GENERIC_IRELATIVE: | 
 | 2731 |         count_relocation(kRelocRelative); | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2732 |         MARK(rel->r_offset); | 
 | 2733 |         TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n", | 
 | 2734 |                     reinterpret_cast<void*>(reloc), | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2735 |                     reinterpret_cast<void*>(load_bias + addend)); | 
| Dmitriy Ivanov | ec83a61 | 2015-07-26 07:37:09 -0700 | [diff] [blame] | 2736 |         { | 
 | 2737 | #if !defined(__LP64__) | 
 | 2738 |           // When relocating dso with text_relocation .text segment is | 
 | 2739 |           // not executable. We need to restore elf flags for this | 
 | 2740 |           // particular call. | 
 | 2741 |           if (has_text_relocations) { | 
 | 2742 |             if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) { | 
 | 2743 |               DL_ERR("can't protect segments for \"%s\": %s", | 
 | 2744 |                      get_realpath(), strerror(errno)); | 
 | 2745 |               return false; | 
 | 2746 |             } | 
 | 2747 |           } | 
 | 2748 | #endif | 
 | 2749 |           ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend); | 
 | 2750 | #if !defined(__LP64__) | 
 | 2751 |           // Unprotect it afterwards... | 
 | 2752 |           if (has_text_relocations) { | 
 | 2753 |             if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) { | 
 | 2754 |               DL_ERR("can't unprotect loadable segments for \"%s\": %s", | 
 | 2755 |                      get_realpath(), strerror(errno)); | 
 | 2756 |               return false; | 
 | 2757 |             } | 
 | 2758 |           } | 
 | 2759 | #endif | 
 | 2760 |           *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr; | 
 | 2761 |         } | 
| Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 2762 |         break; | 
 | 2763 |  | 
 | 2764 | #if defined(__aarch64__) | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2765 |       case R_AARCH64_ABS64: | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2766 |         count_relocation(kRelocAbsolute); | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2767 |         MARK(rel->r_offset); | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2768 |         TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n", | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2769 |                    reloc, sym_addr + addend, sym_name); | 
 | 2770 |         *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend; | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2771 |         break; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2772 |       case R_AARCH64_ABS32: | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2773 |         count_relocation(kRelocAbsolute); | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2774 |         MARK(rel->r_offset); | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2775 |         TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n", | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2776 |                    reloc, sym_addr + addend, sym_name); | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2777 |         { | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2778 |           const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN); | 
 | 2779 |           const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX); | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2780 |           if ((min_value <= (sym_addr + addend)) && | 
 | 2781 |               ((sym_addr + addend) <= max_value)) { | 
 | 2782 |             *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend; | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2783 |           } else { | 
 | 2784 |             DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2785 |                    sym_addr + addend, min_value, max_value); | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2786 |             return false; | 
 | 2787 |           } | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2788 |         } | 
 | 2789 |         break; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2790 |       case R_AARCH64_ABS16: | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2791 |         count_relocation(kRelocAbsolute); | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2792 |         MARK(rel->r_offset); | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2793 |         TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n", | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2794 |                    reloc, sym_addr + addend, sym_name); | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2795 |         { | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2796 |           const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN); | 
 | 2797 |           const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX); | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2798 |           if ((min_value <= (sym_addr + addend)) && | 
 | 2799 |               ((sym_addr + addend) <= max_value)) { | 
 | 2800 |             *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend); | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2801 |           } else { | 
 | 2802 |             DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2803 |                    sym_addr + addend, min_value, max_value); | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2804 |             return false; | 
 | 2805 |           } | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2806 |         } | 
 | 2807 |         break; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2808 |       case R_AARCH64_PREL64: | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2809 |         count_relocation(kRelocRelative); | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2810 |         MARK(rel->r_offset); | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2811 |         TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n", | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2812 |                    reloc, sym_addr + addend, rel->r_offset, sym_name); | 
 | 2813 |         *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset; | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2814 |         break; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2815 |       case R_AARCH64_PREL32: | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2816 |         count_relocation(kRelocRelative); | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2817 |         MARK(rel->r_offset); | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2818 |         TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n", | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2819 |                    reloc, sym_addr + addend, rel->r_offset, sym_name); | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2820 |         { | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2821 |           const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN); | 
 | 2822 |           const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX); | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2823 |           if ((min_value <= (sym_addr + addend - rel->r_offset)) && | 
 | 2824 |               ((sym_addr + addend - rel->r_offset) <= max_value)) { | 
 | 2825 |             *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset; | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2826 |           } else { | 
 | 2827 |             DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2828 |                    sym_addr + addend - rel->r_offset, min_value, max_value); | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2829 |             return false; | 
 | 2830 |           } | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2831 |         } | 
 | 2832 |         break; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2833 |       case R_AARCH64_PREL16: | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2834 |         count_relocation(kRelocRelative); | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2835 |         MARK(rel->r_offset); | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2836 |         TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n", | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2837 |                    reloc, sym_addr + addend, rel->r_offset, sym_name); | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2838 |         { | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2839 |           const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN); | 
 | 2840 |           const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX); | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2841 |           if ((min_value <= (sym_addr + addend - rel->r_offset)) && | 
 | 2842 |               ((sym_addr + addend - rel->r_offset) <= max_value)) { | 
 | 2843 |             *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset; | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2844 |           } else { | 
 | 2845 |             DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", | 
| Dmitriy Ivanov | 77f91c6 | 2015-10-15 13:26:03 -0700 | [diff] [blame] | 2846 |                    sym_addr + addend - rel->r_offset, min_value, max_value); | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2847 |             return false; | 
 | 2848 |           } | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2849 |         } | 
 | 2850 |         break; | 
 | 2851 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2852 |       case R_AARCH64_COPY: | 
| Nick Kralevich | 76e289c | 2014-07-03 12:04:31 -0700 | [diff] [blame] | 2853 |         /* | 
 | 2854 |          * ET_EXEC is not supported so this should not happen. | 
 | 2855 |          * | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2856 |          * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf | 
| Nick Kralevich | 76e289c | 2014-07-03 12:04:31 -0700 | [diff] [blame] | 2857 |          * | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2858 |          * Section 4.6.11 "Dynamic relocations" | 
| Nick Kralevich | 76e289c | 2014-07-03 12:04:31 -0700 | [diff] [blame] | 2859 |          * R_AARCH64_COPY may only appear in executable objects where e_type is | 
 | 2860 |          * set to ET_EXEC. | 
 | 2861 |          */ | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2862 |         DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath()); | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 2863 |         return false; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2864 |       case R_AARCH64_TLS_TPREL64: | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2865 |         TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n", | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2866 |                    reloc, (sym_addr + addend), rel->r_offset); | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2867 |         break; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2868 |       case R_AARCH64_TLS_DTPREL32: | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2869 |         TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n", | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2870 |                    reloc, (sym_addr + addend), rel->r_offset); | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2871 |         break; | 
 | 2872 | #elif defined(__x86_64__) | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2873 |       case R_X86_64_32: | 
 | 2874 |         count_relocation(kRelocRelative); | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2875 |         MARK(rel->r_offset); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2876 |         TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc), | 
 | 2877 |                    static_cast<size_t>(sym_addr), sym_name); | 
| Junichi Uekawa | ff35b1e | 2015-11-18 10:18:59 +0900 | [diff] [blame] | 2878 |         *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2879 |         break; | 
 | 2880 |       case R_X86_64_64: | 
 | 2881 |         count_relocation(kRelocRelative); | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2882 |         MARK(rel->r_offset); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2883 |         TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc), | 
 | 2884 |                    static_cast<size_t>(sym_addr), sym_name); | 
| Junichi Uekawa | ff35b1e | 2015-11-18 10:18:59 +0900 | [diff] [blame] | 2885 |         *reinterpret_cast<Elf64_Addr*>(reloc) = sym_addr + addend; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2886 |         break; | 
 | 2887 |       case R_X86_64_PC32: | 
 | 2888 |         count_relocation(kRelocRelative); | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2889 |         MARK(rel->r_offset); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2890 |         TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s", | 
 | 2891 |                    static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc), | 
 | 2892 |                    static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name); | 
| Junichi Uekawa | ff35b1e | 2015-11-18 10:18:59 +0900 | [diff] [blame] | 2893 |         *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr + addend - reloc; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2894 |         break; | 
| Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2895 | #elif defined(__arm__) | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2896 |       case R_ARM_ABS32: | 
 | 2897 |         count_relocation(kRelocAbsolute); | 
 | 2898 |         MARK(rel->r_offset); | 
 | 2899 |         TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name); | 
 | 2900 |         *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr; | 
 | 2901 |         break; | 
 | 2902 |       case R_ARM_REL32: | 
 | 2903 |         count_relocation(kRelocRelative); | 
 | 2904 |         MARK(rel->r_offset); | 
 | 2905 |         TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s", | 
 | 2906 |                    reloc, sym_addr, rel->r_offset, sym_name); | 
 | 2907 |         *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset; | 
 | 2908 |         break; | 
 | 2909 |       case R_ARM_COPY: | 
 | 2910 |         /* | 
 | 2911 |          * ET_EXEC is not supported so this should not happen. | 
 | 2912 |          * | 
 | 2913 |          * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf | 
 | 2914 |          * | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2915 |          * Section 4.6.1.10 "Dynamic relocations" | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2916 |          * R_ARM_COPY may only appear in executable objects where e_type is | 
 | 2917 |          * set to ET_EXEC. | 
 | 2918 |          */ | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2919 |         DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath()); | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 2920 |         return false; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2921 | #elif defined(__i386__) | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2922 |       case R_386_32: | 
 | 2923 |         count_relocation(kRelocRelative); | 
 | 2924 |         MARK(rel->r_offset); | 
 | 2925 |         TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name); | 
 | 2926 |         *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr; | 
 | 2927 |         break; | 
 | 2928 |       case R_386_PC32: | 
 | 2929 |         count_relocation(kRelocRelative); | 
 | 2930 |         MARK(rel->r_offset); | 
 | 2931 |         TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s", | 
 | 2932 |                    reloc, (sym_addr - reloc), sym_addr, reloc, sym_name); | 
 | 2933 |         *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc); | 
 | 2934 |         break; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2935 | #endif | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2936 |       default: | 
 | 2937 |         DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2938 |         return false; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2939 |     } | 
 | 2940 |   } | 
 | 2941 |   return true; | 
| Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 2942 | } | 
| Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 2943 | #endif  // !defined(__mips__) | 
| Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 2944 |  | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2945 | void soinfo::call_array(const char* array_name __unused, linker_function_t* functions, | 
 | 2946 |                         size_t count, bool reverse) { | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 2947 |   if (functions == nullptr) { | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2948 |     return; | 
 | 2949 |   } | 
| David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 2950 |  | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2951 |   TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_realpath()); | 
| Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 2952 |  | 
 | 2953 |   int begin = reverse ? (count - 1) : 0; | 
 | 2954 |   int end = reverse ? -1 : count; | 
 | 2955 |   int step = reverse ? -1 : 1; | 
 | 2956 |  | 
 | 2957 |   for (int i = begin; i != end; i += step) { | 
 | 2958 |     TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]); | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2959 |     call_function("function", functions[i]); | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2960 |   } | 
| David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 2961 |  | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2962 |   TRACE("[ Done calling %s for '%s' ]", array_name, get_realpath()); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2963 | } | 
 | 2964 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2965 | void soinfo::call_function(const char* function_name __unused, linker_function_t function) { | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 2966 |   if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) { | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2967 |     return; | 
 | 2968 |   } | 
 | 2969 |  | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2970 |   TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_realpath()); | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2971 |   function(); | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 2972 |   TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_realpath()); | 
| Evgeniy Stepanov | 9181a5d | 2012-08-13 17:58:37 +0400 | [diff] [blame] | 2973 | } | 
 | 2974 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2975 | void soinfo::call_pre_init_constructors() { | 
| Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 2976 |   // DT_PREINIT_ARRAY functions are called before any other constructors for executables, | 
 | 2977 |   // but ignored in a shared library. | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2978 |   call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false); | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2979 | } | 
| Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 2980 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2981 | void soinfo::call_constructors() { | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2982 |   if (constructors_called) { | 
 | 2983 |     return; | 
 | 2984 |   } | 
| Jesse Hall | f5d1693 | 2012-01-30 15:39:57 -0800 | [diff] [blame] | 2985 |  | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2986 |   // We set constructors_called before actually calling the constructors, otherwise it doesn't | 
 | 2987 |   // protect against recursive constructor calls. One simple example of constructor recursion | 
 | 2988 |   // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so: | 
 | 2989 |   // 1. The program depends on libc, so libc's constructor is called here. | 
 | 2990 |   // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so. | 
 | 2991 |   // 3. dlopen() calls the constructors on the newly created | 
 | 2992 |   //    soinfo for libc_malloc_debug_leak.so. | 
 | 2993 |   // 4. The debug .so depends on libc, so CallConstructors is | 
 | 2994 |   //    called again with the libc soinfo. If it doesn't trigger the early- | 
 | 2995 |   //    out above, the libc constructor will be called again (recursively!). | 
 | 2996 |   constructors_called = true; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2997 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2998 |   if (!is_main_executable() && preinit_array_ != nullptr) { | 
| Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 2999 |     // The GNU dynamic linker silently ignores these, but we warn the developer. | 
| Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 3000 |     PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3001 |           get_realpath(), preinit_array_count_); | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 3002 |   } | 
 | 3003 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3004 |   get_children().for_each([] (soinfo* si) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3005 |     si->call_constructors(); | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3006 |   }); | 
| Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 3007 |  | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3008 |   TRACE("\"%s\": calling constructors", get_realpath()); | 
| Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 3009 |  | 
 | 3010 |   // DT_INIT should be called before DT_INIT_ARRAY if both are present. | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3011 |   call_function("DT_INIT", init_func_); | 
 | 3012 |   call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false); | 
| Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 3013 | } | 
| David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 3014 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3015 | void soinfo::call_destructors() { | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 3016 |   if (!constructors_called) { | 
 | 3017 |     return; | 
 | 3018 |   } | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3019 |   TRACE("\"%s\": calling destructors", get_realpath()); | 
| Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 3020 |  | 
 | 3021 |   // DT_FINI_ARRAY must be parsed in reverse order. | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3022 |   call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true); | 
| Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 3023 |  | 
 | 3024 |   // DT_FINI should be called after DT_FINI_ARRAY if both are present. | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3025 |   call_function("DT_FINI", fini_func_); | 
| Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 3026 |  | 
 | 3027 |   // This is needed on second call to dlopen | 
 | 3028 |   // after library has been unloaded with RTLD_NODELETE | 
 | 3029 |   constructors_called = false; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3030 | } | 
 | 3031 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3032 | void soinfo::add_child(soinfo* child) { | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 3033 |   if (has_min_version(0)) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3034 |     child->parents_.push_back(this); | 
 | 3035 |     this->children_.push_back(child); | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3036 |   } | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3037 | } | 
 | 3038 |  | 
 | 3039 | void soinfo::remove_all_links() { | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 3040 |   if (!has_min_version(0)) { | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3041 |     return; | 
 | 3042 |   } | 
 | 3043 |  | 
 | 3044 |   // 1. Untie connected soinfos from 'this'. | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3045 |   children_.for_each([&] (soinfo* child) { | 
 | 3046 |     child->parents_.remove_if([&] (const soinfo* parent) { | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3047 |       return parent == this; | 
 | 3048 |     }); | 
 | 3049 |   }); | 
 | 3050 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3051 |   parents_.for_each([&] (soinfo* parent) { | 
 | 3052 |     parent->children_.remove_if([&] (const soinfo* child) { | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3053 |       return child == this; | 
 | 3054 |     }); | 
 | 3055 |   }); | 
 | 3056 |  | 
 | 3057 |   // 2. Once everything untied - clear local lists. | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3058 |   parents_.clear(); | 
 | 3059 |   children_.clear(); | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3060 | } | 
 | 3061 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3062 | dev_t soinfo::get_st_dev() const { | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 3063 |   if (has_min_version(0)) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3064 |     return st_dev_; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3065 |   } | 
 | 3066 |  | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 3067 |   return 0; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3068 | }; | 
 | 3069 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3070 | ino_t soinfo::get_st_ino() const { | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 3071 |   if (has_min_version(0)) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3072 |     return st_ino_; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3073 |   } | 
 | 3074 |  | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 3075 |   return 0; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3076 | } | 
 | 3077 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3078 | off64_t soinfo::get_file_offset() const { | 
| Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 3079 |   if (has_min_version(1)) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3080 |     return file_offset_; | 
| Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 3081 |   } | 
 | 3082 |  | 
 | 3083 |   return 0; | 
 | 3084 | } | 
 | 3085 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3086 | uint32_t soinfo::get_rtld_flags() const { | 
| Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 3087 |   if (has_min_version(1)) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3088 |     return rtld_flags_; | 
| Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 3089 |   } | 
 | 3090 |  | 
 | 3091 |   return 0; | 
 | 3092 | } | 
 | 3093 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3094 | uint32_t soinfo::get_dt_flags_1() const { | 
 | 3095 |   if (has_min_version(1)) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3096 |     return dt_flags_1_; | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3097 |   } | 
 | 3098 |  | 
 | 3099 |   return 0; | 
 | 3100 | } | 
| Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 3101 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3102 | void soinfo::set_dt_flags_1(uint32_t dt_flags_1) { | 
 | 3103 |   if (has_min_version(1)) { | 
 | 3104 |     if ((dt_flags_1 & DF_1_GLOBAL) != 0) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3105 |       rtld_flags_ |= RTLD_GLOBAL; | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3106 |     } | 
 | 3107 |  | 
 | 3108 |     if ((dt_flags_1 & DF_1_NODELETE) != 0) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3109 |       rtld_flags_ |= RTLD_NODELETE; | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3110 |     } | 
 | 3111 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3112 |     dt_flags_1_ = dt_flags_1; | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3113 |   } | 
 | 3114 | } | 
 | 3115 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 3116 | void soinfo::set_nodelete() { | 
 | 3117 |   rtld_flags_ |= RTLD_NODELETE; | 
 | 3118 | } | 
 | 3119 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3120 | const char* soinfo::get_realpath() const { | 
| Dmitriy Ivanov | 280d546 | 2015-09-28 10:14:17 -0700 | [diff] [blame] | 3121 | #if defined(__work_around_b_24465209__) | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3122 |   if (has_min_version(2)) { | 
 | 3123 |     return realpath_.c_str(); | 
 | 3124 |   } else { | 
 | 3125 |     return old_name_; | 
 | 3126 |   } | 
 | 3127 | #else | 
 | 3128 |   return realpath_.c_str(); | 
 | 3129 | #endif | 
 | 3130 | } | 
 | 3131 |  | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 3132 | void soinfo::set_soname(const char* soname) { | 
 | 3133 | #if defined(__work_around_b_24465209__) | 
 | 3134 |   if (has_min_version(2)) { | 
 | 3135 |     soname_ = soname; | 
 | 3136 |   } | 
 | 3137 |   strlcpy(old_name_, soname_, sizeof(old_name_)); | 
 | 3138 | #else | 
 | 3139 |   soname_ = soname; | 
 | 3140 | #endif | 
 | 3141 | } | 
 | 3142 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3143 | const char* soinfo::get_soname() const { | 
| Dmitriy Ivanov | 280d546 | 2015-09-28 10:14:17 -0700 | [diff] [blame] | 3144 | #if defined(__work_around_b_24465209__) | 
| Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 3145 |   if (has_min_version(2)) { | 
 | 3146 |     return soname_; | 
 | 3147 |   } else { | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3148 |     return old_name_; | 
| Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 3149 |   } | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3150 | #else | 
 | 3151 |   return soname_; | 
 | 3152 | #endif | 
| Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 3153 | } | 
 | 3154 |  | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 3155 | // This is a return on get_children()/get_parents() if | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3156 | // 'this->flags' does not have FLAG_NEW_SOINFO set. | 
 | 3157 | static soinfo::soinfo_list_t g_empty_list; | 
 | 3158 |  | 
 | 3159 | soinfo::soinfo_list_t& soinfo::get_children() { | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 3160 |   if (has_min_version(0)) { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3161 |     return children_; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3162 |   } | 
 | 3163 |  | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 3164 |   return g_empty_list; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3165 | } | 
 | 3166 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 3167 | const soinfo::soinfo_list_t& soinfo::get_children() const { | 
 | 3168 |   if (has_min_version(0)) { | 
 | 3169 |     return children_; | 
 | 3170 |   } | 
 | 3171 |  | 
 | 3172 |   return g_empty_list; | 
 | 3173 | } | 
 | 3174 |  | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 3175 | soinfo::soinfo_list_t& soinfo::get_parents() { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3176 |   if (has_min_version(0)) { | 
 | 3177 |     return parents_; | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 3178 |   } | 
 | 3179 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3180 |   return g_empty_list; | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 3181 | } | 
 | 3182 |  | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 3183 | static std::vector<std::string> g_empty_runpath; | 
 | 3184 |  | 
 | 3185 | const std::vector<std::string>& soinfo::get_dt_runpath() const { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 3186 |   if (has_min_version(3)) { | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 3187 |     return dt_runpath_; | 
 | 3188 |   } | 
 | 3189 |  | 
 | 3190 |   return g_empty_runpath; | 
 | 3191 | } | 
 | 3192 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 3193 | android_namespace_t* soinfo::get_namespace() { | 
 | 3194 |   if (has_min_version(3)) { | 
 | 3195 |     return namespace_; | 
 | 3196 |   } | 
 | 3197 |  | 
 | 3198 |   return &g_default_namespace; | 
 | 3199 | } | 
 | 3200 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 3201 | ElfW(Addr) soinfo::resolve_symbol_address(const ElfW(Sym)* s) const { | 
| Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 3202 |   if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) { | 
 | 3203 |     return call_ifunc_resolver(s->st_value + load_bias); | 
 | 3204 |   } | 
 | 3205 |  | 
 | 3206 |   return static_cast<ElfW(Addr)>(s->st_value + load_bias); | 
 | 3207 | } | 
 | 3208 |  | 
| Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 3209 | const char* soinfo::get_string(ElfW(Word) index) const { | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3210 |   if (has_min_version(1) && (index >= strtab_size_)) { | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3211 |     __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3212 |         get_realpath(), strtab_size_, index); | 
| Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 3213 |   } | 
 | 3214 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3215 |   return strtab_ + index; | 
| Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 3216 | } | 
 | 3217 |  | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 3218 | bool soinfo::is_gnu_hash() const { | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 3219 |   return (flags_ & FLAG_GNU_HASH) != 0; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 3220 | } | 
 | 3221 |  | 
| Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 3222 | bool soinfo::can_unload() const { | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3223 |   return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0; | 
| Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 3224 | } | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3225 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 3226 | bool soinfo::is_linked() const { | 
 | 3227 |   return (flags_ & FLAG_LINKED) != 0; | 
 | 3228 | } | 
 | 3229 |  | 
 | 3230 | bool soinfo::is_main_executable() const { | 
 | 3231 |   return (flags_ & FLAG_EXE) != 0; | 
 | 3232 | } | 
 | 3233 |  | 
 | 3234 | void soinfo::set_linked() { | 
 | 3235 |   flags_ |= FLAG_LINKED; | 
 | 3236 | } | 
 | 3237 |  | 
 | 3238 | void soinfo::set_linker_flag() { | 
 | 3239 |   flags_ |= FLAG_LINKER; | 
 | 3240 | } | 
 | 3241 |  | 
 | 3242 | void soinfo::set_main_executable() { | 
 | 3243 |   flags_ |= FLAG_EXE; | 
 | 3244 | } | 
 | 3245 |  | 
 | 3246 | void soinfo::increment_ref_count() { | 
 | 3247 |   local_group_root_->ref_count_++; | 
 | 3248 | } | 
 | 3249 |  | 
 | 3250 | size_t soinfo::decrement_ref_count() { | 
 | 3251 |   return --local_group_root_->ref_count_; | 
 | 3252 | } | 
 | 3253 |  | 
 | 3254 | soinfo* soinfo::get_local_group_root() const { | 
 | 3255 |   return local_group_root_; | 
 | 3256 | } | 
 | 3257 |  | 
| Dimitry Ivanov | f45b0e9 | 2016-01-15 11:13:35 -0800 | [diff] [blame] | 3258 |  | 
 | 3259 | void soinfo::set_mapped_by_caller(bool mapped_by_caller) { | 
 | 3260 |   if (mapped_by_caller) { | 
 | 3261 |     flags_ |= FLAG_MAPPED_BY_CALLER; | 
 | 3262 |   } else { | 
 | 3263 |     flags_ &= ~FLAG_MAPPED_BY_CALLER; | 
 | 3264 |   } | 
 | 3265 | } | 
 | 3266 |  | 
 | 3267 | bool soinfo::is_mapped_by_caller() const { | 
 | 3268 |   return (flags_ & FLAG_MAPPED_BY_CALLER) != 0; | 
 | 3269 | } | 
 | 3270 |  | 
| Dmitriy Ivanov | 1913352 | 2015-06-02 17:36:54 -0700 | [diff] [blame] | 3271 | // This function returns api-level at the time of | 
 | 3272 | // dlopen/load. Note that libraries opened by system | 
 | 3273 | // will always have 'current' api level. | 
 | 3274 | uint32_t soinfo::get_target_sdk_version() const { | 
 | 3275 |   if (!has_min_version(2)) { | 
 | 3276 |     return __ANDROID_API__; | 
 | 3277 |   } | 
 | 3278 |  | 
 | 3279 |   return local_group_root_->target_sdk_version_; | 
 | 3280 | } | 
 | 3281 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3282 | bool soinfo::prelink_image() { | 
| Ningsheng Jian | e93be99 | 2014-09-16 15:22:10 +0800 | [diff] [blame] | 3283 |   /* Extract dynamic section */ | 
 | 3284 |   ElfW(Word) dynamic_flags = 0; | 
 | 3285 |   phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags); | 
| Dmitriy Ivanov | 498eb18 | 2014-09-05 14:57:59 -0700 | [diff] [blame] | 3286 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3287 |   /* We can't log anything until the linker is relocated */ | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 3288 |   bool relocating_linker = (flags_ & FLAG_LINKER) != 0; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3289 |   if (!relocating_linker) { | 
| Elliott Hughes | 116b569 | 2016-01-04 17:45:36 -0800 | [diff] [blame] | 3290 |     INFO("[ Linking '%s' ]", get_realpath()); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 3291 |     DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3292 |   } | 
 | 3293 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3294 |   if (dynamic == nullptr) { | 
| David 'Digit' Turner | b52e438 | 2012-06-19 01:24:17 +0200 | [diff] [blame] | 3295 |     if (!relocating_linker) { | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3296 |       DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath()); | 
| David 'Digit' Turner | b52e438 | 2012-06-19 01:24:17 +0200 | [diff] [blame] | 3297 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3298 |     return false; | 
 | 3299 |   } else { | 
 | 3300 |     if (!relocating_linker) { | 
 | 3301 |       DEBUG("dynamic = %p", dynamic); | 
| David 'Digit' Turner | 63f99f4 | 2012-06-19 00:08:39 +0200 | [diff] [blame] | 3302 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3303 |   } | 
| David 'Digit' Turner | 63f99f4 | 2012-06-19 00:08:39 +0200 | [diff] [blame] | 3304 |  | 
| Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 3305 | #if defined(__arm__) | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3306 |   (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias, | 
 | 3307 |                                   &ARM_exidx, &ARM_exidx_count); | 
| David 'Digit' Turner | 63f99f4 | 2012-06-19 00:08:39 +0200 | [diff] [blame] | 3308 | #endif | 
 | 3309 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3310 |   // Extract useful information from dynamic section. | 
| Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 3311 |   // Note that: "Except for the DT_NULL element at the end of the array, | 
 | 3312 |   // and the relative order of DT_NEEDED elements, entries may appear in any order." | 
 | 3313 |   // | 
 | 3314 |   // source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3315 |   uint32_t needed_count = 0; | 
 | 3316 |   for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) { | 
 | 3317 |     DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p", | 
 | 3318 |           d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val)); | 
 | 3319 |     switch (d->d_tag) { | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3320 |       case DT_SONAME: | 
| Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 3321 |         // this is parsed after we have strtab initialized (see below). | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3322 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3323 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3324 |       case DT_HASH: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3325 |         nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0]; | 
 | 3326 |         nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1]; | 
 | 3327 |         bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8); | 
 | 3328 |         chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3329 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3330 |  | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 3331 |       case DT_GNU_HASH: | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 3332 |         gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0]; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 3333 |         // skip symndx | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3334 |         gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2]; | 
 | 3335 |         gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3]; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 3336 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3337 |         gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16); | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 3338 |         gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_); | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 3339 |         // amend chain for symndx = header[1] | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 3340 |         gnu_chain_ = gnu_bucket_ + gnu_nbucket_ - | 
 | 3341 |             reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1]; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 3342 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3343 |         if (!powerof2(gnu_maskwords_)) { | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 3344 |           DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two", | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3345 |               gnu_maskwords_, get_realpath()); | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 3346 |           return false; | 
 | 3347 |         } | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3348 |         --gnu_maskwords_; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 3349 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 3350 |         flags_ |= FLAG_GNU_HASH; | 
| Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 3351 |         break; | 
 | 3352 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3353 |       case DT_STRTAB: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3354 |         strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3355 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3356 |  | 
| Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 3357 |       case DT_STRSZ: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3358 |         strtab_size_ = d->d_un.d_val; | 
| Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 3359 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3360 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3361 |       case DT_SYMTAB: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3362 |         symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3363 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3364 |  | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3365 |       case DT_SYMENT: | 
 | 3366 |         if (d->d_un.d_val != sizeof(ElfW(Sym))) { | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3367 |           DL_ERR("invalid DT_SYMENT: %zd in \"%s\"", | 
 | 3368 |               static_cast<size_t>(d->d_un.d_val), get_realpath()); | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3369 |           return false; | 
 | 3370 |         } | 
 | 3371 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3372 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3373 |       case DT_PLTREL: | 
| Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 3374 | #if defined(USE_RELA) | 
 | 3375 |         if (d->d_un.d_val != DT_RELA) { | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3376 |           DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath()); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3377 |           return false; | 
 | 3378 |         } | 
| Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 3379 | #else | 
 | 3380 |         if (d->d_un.d_val != DT_REL) { | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3381 |           DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath()); | 
| Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 3382 |           return false; | 
 | 3383 |         } | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 3384 | #endif | 
| Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 3385 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3386 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3387 |       case DT_JMPREL: | 
| Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 3388 | #if defined(USE_RELA) | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3389 |         plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr); | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 3390 | #else | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3391 |         plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr); | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 3392 | #endif | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3393 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3394 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3395 |       case DT_PLTRELSZ: | 
| Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 3396 | #if defined(USE_RELA) | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3397 |         plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela)); | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 3398 | #else | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3399 |         plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel)); | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 3400 | #endif | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3401 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3402 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3403 |       case DT_PLTGOT: | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3404 | #if defined(__mips__) | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3405 |         // Used by mips and mips64. | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3406 |         plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr); | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 3407 | #endif | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3408 |         // Ignore for other platforms... (because RTLD_LAZY is not supported) | 
 | 3409 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3410 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3411 |       case DT_DEBUG: | 
 | 3412 |         // Set the DT_DEBUG entry to the address of _r_debug for GDB | 
 | 3413 |         // if the dynamic table is writable | 
| Chris Dearman | 9918665 | 2014-02-06 20:36:51 -0800 | [diff] [blame] | 3414 | // FIXME: not working currently for N64 | 
 | 3415 | // The flags for the LOAD and DYNAMIC program headers do not agree. | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 3416 | // The LOAD section containing the dynamic table has been mapped as | 
| Chris Dearman | 9918665 | 2014-02-06 20:36:51 -0800 | [diff] [blame] | 3417 | // read-only, but the DYNAMIC header claims it is writable. | 
 | 3418 | #if !(defined(__mips__) && defined(__LP64__)) | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3419 |         if ((dynamic_flags & PF_W) != 0) { | 
 | 3420 |           d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug); | 
 | 3421 |         } | 
| Chris Dearman | 9918665 | 2014-02-06 20:36:51 -0800 | [diff] [blame] | 3422 | #endif | 
| Dmitriy Ivanov | c6292ea | 2015-02-13 16:29:50 -0800 | [diff] [blame] | 3423 |         break; | 
| Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 3424 | #if defined(USE_RELA) | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3425 |       case DT_RELA: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3426 |         rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3427 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3428 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3429 |       case DT_RELASZ: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3430 |         rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela)); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3431 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3432 |  | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3433 |       case DT_ANDROID_RELA: | 
 | 3434 |         android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr); | 
 | 3435 |         break; | 
 | 3436 |  | 
 | 3437 |       case DT_ANDROID_RELASZ: | 
 | 3438 |         android_relocs_size_ = d->d_un.d_val; | 
 | 3439 |         break; | 
 | 3440 |  | 
 | 3441 |       case DT_ANDROID_REL: | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3442 |         DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath()); | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3443 |         return false; | 
 | 3444 |  | 
 | 3445 |       case DT_ANDROID_RELSZ: | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3446 |         DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath()); | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3447 |         return false; | 
 | 3448 |  | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3449 |       case DT_RELAENT: | 
 | 3450 |         if (d->d_un.d_val != sizeof(ElfW(Rela))) { | 
| Dmitriy Ivanov | f240aa8 | 2014-09-16 23:34:20 -0700 | [diff] [blame] | 3451 |           DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val)); | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3452 |           return false; | 
 | 3453 |         } | 
 | 3454 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3455 |  | 
 | 3456 |       // ignored (see DT_RELCOUNT comments for details) | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3457 |       case DT_RELACOUNT: | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3458 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3459 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3460 |       case DT_REL: | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3461 |         DL_ERR("unsupported DT_REL in \"%s\"", get_realpath()); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3462 |         return false; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3463 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3464 |       case DT_RELSZ: | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3465 |         DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath()); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3466 |         return false; | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3467 |  | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 3468 | #else | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3469 |       case DT_REL: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3470 |         rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3471 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3472 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3473 |       case DT_RELSZ: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3474 |         rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel)); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3475 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3476 |  | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3477 |       case DT_RELENT: | 
 | 3478 |         if (d->d_un.d_val != sizeof(ElfW(Rel))) { | 
| Dmitriy Ivanov | f240aa8 | 2014-09-16 23:34:20 -0700 | [diff] [blame] | 3479 |           DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val)); | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3480 |           return false; | 
 | 3481 |         } | 
 | 3482 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3483 |  | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3484 |       case DT_ANDROID_REL: | 
 | 3485 |         android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr); | 
 | 3486 |         break; | 
 | 3487 |  | 
 | 3488 |       case DT_ANDROID_RELSZ: | 
 | 3489 |         android_relocs_size_ = d->d_un.d_val; | 
 | 3490 |         break; | 
 | 3491 |  | 
 | 3492 |       case DT_ANDROID_RELA: | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3493 |         DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath()); | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3494 |         return false; | 
 | 3495 |  | 
 | 3496 |       case DT_ANDROID_RELASZ: | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3497 |         DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath()); | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3498 |         return false; | 
 | 3499 |  | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3500 |       // "Indicates that all RELATIVE relocations have been concatenated together, | 
 | 3501 |       // and specifies the RELATIVE relocation count." | 
 | 3502 |       // | 
 | 3503 |       // TODO: Spec also mentions that this can be used to optimize relocation process; | 
 | 3504 |       // Not currently used by bionic linker - ignored. | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3505 |       case DT_RELCOUNT: | 
| Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 3506 |         break; | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3507 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3508 |       case DT_RELA: | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3509 |         DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath()); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3510 |         return false; | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3511 |  | 
 | 3512 |       case DT_RELASZ: | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3513 |         DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath()); | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3514 |         return false; | 
 | 3515 |  | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 3516 | #endif | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3517 |       case DT_INIT: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3518 |         init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr); | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3519 |         DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3520 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3521 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3522 |       case DT_FINI: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3523 |         fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr); | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3524 |         DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3525 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3526 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3527 |       case DT_INIT_ARRAY: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3528 |         init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr); | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3529 |         DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3530 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3531 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3532 |       case DT_INIT_ARRAYSZ: | 
| Dmitriy Ivanov | 1649e7e | 2015-01-22 16:04:25 -0800 | [diff] [blame] | 3533 |         init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr)); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3534 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3535 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3536 |       case DT_FINI_ARRAY: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3537 |         fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr); | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3538 |         DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3539 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3540 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3541 |       case DT_FINI_ARRAYSZ: | 
| Dmitriy Ivanov | 1649e7e | 2015-01-22 16:04:25 -0800 | [diff] [blame] | 3542 |         fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr)); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3543 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3544 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3545 |       case DT_PREINIT_ARRAY: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3546 |         preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr); | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3547 |         DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3548 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3549 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3550 |       case DT_PREINIT_ARRAYSZ: | 
| Dmitriy Ivanov | 1649e7e | 2015-01-22 16:04:25 -0800 | [diff] [blame] | 3551 |         preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr)); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3552 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3553 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3554 |       case DT_TEXTREL: | 
| Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 3555 | #if defined(__LP64__) | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3556 |         DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath()); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3557 |         return false; | 
| Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 3558 | #else | 
 | 3559 |         has_text_relocations = true; | 
 | 3560 |         break; | 
 | 3561 | #endif | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3562 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3563 |       case DT_SYMBOLIC: | 
| Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 3564 |         has_DT_SYMBOLIC = true; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3565 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3566 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3567 |       case DT_NEEDED: | 
 | 3568 |         ++needed_count; | 
 | 3569 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3570 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3571 |       case DT_FLAGS: | 
 | 3572 |         if (d->d_un.d_val & DF_TEXTREL) { | 
| Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 3573 | #if defined(__LP64__) | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3574 |           DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath()); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3575 |           return false; | 
| Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 3576 | #else | 
 | 3577 |           has_text_relocations = true; | 
 | 3578 | #endif | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3579 |         } | 
| Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 3580 |         if (d->d_un.d_val & DF_SYMBOLIC) { | 
 | 3581 |           has_DT_SYMBOLIC = true; | 
 | 3582 |         } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3583 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3584 |  | 
| Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 3585 |       case DT_FLAGS_1: | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3586 |         set_dt_flags_1(d->d_un.d_val); | 
| Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 3587 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3588 |         if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) { | 
| Dmitriy Ivanov | 087005f | 2015-05-28 11:44:31 -0700 | [diff] [blame] | 3589 |           DL_WARN("%s: unsupported flags DT_FLAGS_1=%p", get_realpath(), reinterpret_cast<void*>(d->d_un.d_val)); | 
| Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 3590 |         } | 
 | 3591 |         break; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3592 | #if defined(__mips__) | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3593 |       case DT_MIPS_RLD_MAP: | 
 | 3594 |         // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB. | 
 | 3595 |         { | 
 | 3596 |           r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr); | 
 | 3597 |           *dp = &_r_debug; | 
 | 3598 |         } | 
 | 3599 |         break; | 
| Raghu Gandham | 6881572 | 2014-12-18 19:12:19 -0800 | [diff] [blame] | 3600 |       case DT_MIPS_RLD_MAP2: | 
 | 3601 |         // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB. | 
 | 3602 |         { | 
| Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 3603 |           r_debug** dp = reinterpret_cast<r_debug**>( | 
 | 3604 |               reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val); | 
| Raghu Gandham | 6881572 | 2014-12-18 19:12:19 -0800 | [diff] [blame] | 3605 |           *dp = &_r_debug; | 
 | 3606 |         } | 
 | 3607 |         break; | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3608 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3609 |       case DT_MIPS_RLD_VERSION: | 
 | 3610 |       case DT_MIPS_FLAGS: | 
 | 3611 |       case DT_MIPS_BASE_ADDRESS: | 
 | 3612 |       case DT_MIPS_UNREFEXTNO: | 
 | 3613 |         break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3614 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3615 |       case DT_MIPS_SYMTABNO: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3616 |         mips_symtabno_ = d->d_un.d_val; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3617 |         break; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3618 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3619 |       case DT_MIPS_LOCAL_GOTNO: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3620 |         mips_local_gotno_ = d->d_un.d_val; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3621 |         break; | 
 | 3622 |  | 
 | 3623 |       case DT_MIPS_GOTSYM: | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3624 |         mips_gotsym_ = d->d_un.d_val; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3625 |         break; | 
 | 3626 | #endif | 
| Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 3627 |       // Ignored: "Its use has been superseded by the DF_BIND_NOW flag" | 
 | 3628 |       case DT_BIND_NOW: | 
 | 3629 |         break; | 
 | 3630 |  | 
| Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 3631 |       case DT_VERSYM: | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 3632 |         versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr); | 
 | 3633 |         break; | 
 | 3634 |  | 
| Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 3635 |       case DT_VERDEF: | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 3636 |         verdef_ptr_ = load_bias + d->d_un.d_ptr; | 
 | 3637 |         break; | 
| Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 3638 |       case DT_VERDEFNUM: | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 3639 |         verdef_cnt_ = d->d_un.d_val; | 
 | 3640 |         break; | 
 | 3641 |  | 
| Alexander Ivchenko | e831433 | 2014-12-02 15:32:25 +0300 | [diff] [blame] | 3642 |       case DT_VERNEED: | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 3643 |         verneed_ptr_ = load_bias + d->d_un.d_ptr; | 
 | 3644 |         break; | 
 | 3645 |  | 
| Alexander Ivchenko | e831433 | 2014-12-02 15:32:25 +0300 | [diff] [blame] | 3646 |       case DT_VERNEEDNUM: | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 3647 |         verneed_cnt_ = d->d_un.d_val; | 
| Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 3648 |         break; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3649 |  | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 3650 |       case DT_RUNPATH: | 
 | 3651 |         // this is parsed after we have strtab initialized (see below). | 
 | 3652 |         break; | 
 | 3653 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3654 |       default: | 
| Dmitriy Ivanov | 8f61d99 | 2014-09-16 14:31:06 -0700 | [diff] [blame] | 3655 |         if (!relocating_linker) { | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3656 |           DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(), | 
| Dmitriy Ivanov | 8f61d99 | 2014-09-16 14:31:06 -0700 | [diff] [blame] | 3657 |               reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val)); | 
 | 3658 |         } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3659 |         break; | 
| Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 3660 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3661 |   } | 
 | 3662 |  | 
| Duane Sand | bc425c7 | 2015-06-01 16:29:14 -0700 | [diff] [blame] | 3663 | #if defined(__mips__) && !defined(__LP64__) | 
 | 3664 |   if (!mips_check_and_adjust_fp_modes()) { | 
 | 3665 |     return false; | 
 | 3666 |   } | 
 | 3667 | #endif | 
 | 3668 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3669 |   DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p", | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3670 |         reinterpret_cast<void*>(base), strtab_, symtab_); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3671 |  | 
 | 3672 |   // Sanity checks. | 
 | 3673 |   if (relocating_linker && needed_count != 0) { | 
 | 3674 |     DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries"); | 
 | 3675 |     return false; | 
 | 3676 |   } | 
| Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 3677 |   if (nbucket_ == 0 && gnu_nbucket_ == 0) { | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3678 |     DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" " | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3679 |         "(new hash type from the future?)", get_realpath()); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3680 |     return false; | 
 | 3681 |   } | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3682 |   if (strtab_ == 0) { | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3683 |     DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath()); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3684 |     return false; | 
 | 3685 |   } | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3686 |   if (symtab_ == 0) { | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3687 |     DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath()); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3688 |     return false; | 
 | 3689 |   } | 
| Dmitriy Ivanov | 75108f4 | 2015-06-02 13:28:06 -0700 | [diff] [blame] | 3690 |  | 
| Dmitriy Ivanov | 624b8f1 | 2015-06-08 10:41:33 -0700 | [diff] [blame] | 3691 |   // second pass - parse entries relying on strtab | 
 | 3692 |   for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) { | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 3693 |     switch (d->d_tag) { | 
 | 3694 |       case DT_SONAME: | 
| Dmitriy Ivanov | 4f7a7ad | 2015-10-15 12:07:25 -0700 | [diff] [blame] | 3695 |         set_soname(get_string(d->d_un.d_val)); | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 3696 |         break; | 
 | 3697 |       case DT_RUNPATH: | 
| Evgenii Stepanov | 6865082 | 2015-06-10 13:38:39 -0700 | [diff] [blame] | 3698 |         set_dt_runpath(get_string(d->d_un.d_val)); | 
 | 3699 |         break; | 
| Dmitriy Ivanov | 624b8f1 | 2015-06-08 10:41:33 -0700 | [diff] [blame] | 3700 |     } | 
 | 3701 |   } | 
 | 3702 |  | 
| Dmitriy Ivanov | 75108f4 | 2015-06-02 13:28:06 -0700 | [diff] [blame] | 3703 |   // Before M release linker was using basename in place of soname. | 
| Dmitriy Ivanov | 1913352 | 2015-06-02 17:36:54 -0700 | [diff] [blame] | 3704 |   // In the case when dt_soname is absent some apps stop working | 
| Dmitriy Ivanov | 75108f4 | 2015-06-02 13:28:06 -0700 | [diff] [blame] | 3705 |   // because they can't find dt_needed library by soname. | 
 | 3706 |   // This workaround should keep them working. (applies only | 
| Dmitriy Ivanov | 1913352 | 2015-06-02 17:36:54 -0700 | [diff] [blame] | 3707 |   // for apps targeting sdk version <=22). Make an exception for | 
 | 3708 |   // the main executable and linker; they do not need to have dt_soname | 
 | 3709 |   if (soname_ == nullptr && this != somain && (flags_ & FLAG_LINKER) == 0 && | 
 | 3710 |       get_application_target_sdk_version() <= 22) { | 
| Dmitriy Ivanov | 75108f4 | 2015-06-02 13:28:06 -0700 | [diff] [blame] | 3711 |     soname_ = basename(realpath_.c_str()); | 
 | 3712 |     DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"", | 
 | 3713 |         get_realpath(), soname_); | 
 | 3714 |   } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3715 |   return true; | 
| Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 3716 | } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3717 |  | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3718 | bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group, | 
 | 3719 |                         const android_dlextinfo* extinfo) { | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3720 |  | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 3721 |   local_group_root_ = local_group.front(); | 
 | 3722 |   if (local_group_root_ == nullptr) { | 
 | 3723 |     local_group_root_ = this; | 
 | 3724 |   } | 
 | 3725 |  | 
| Dmitriy Ivanov | 1913352 | 2015-06-02 17:36:54 -0700 | [diff] [blame] | 3726 |   if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) { | 
 | 3727 |     target_sdk_version_ = get_application_target_sdk_version(); | 
 | 3728 |   } | 
 | 3729 |  | 
| Dmitriy Ivanov | 7e4bbba | 2015-04-30 19:49:19 -0700 | [diff] [blame] | 3730 |   VersionTracker version_tracker; | 
 | 3731 |  | 
 | 3732 |   if (!version_tracker.init(this)) { | 
 | 3733 |     return false; | 
 | 3734 |   } | 
 | 3735 |  | 
| Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 3736 | #if !defined(__LP64__) | 
 | 3737 |   if (has_text_relocations) { | 
| Dmitriy Ivanov | e4ad91f | 2015-06-12 15:00:31 -0700 | [diff] [blame] | 3738 |     // Fail if app is targeting sdk version > 22 | 
| Dmitriy Ivanov | 8068786 | 2015-10-09 13:58:46 -0700 | [diff] [blame] | 3739 |     if (get_application_target_sdk_version() > 22) { | 
| Dmitriy Ivanov | fae39d2 | 2015-10-13 11:07:56 -0700 | [diff] [blame] | 3740 |       PRINT("%s: has text relocations", get_realpath()); | 
| Dmitriy Ivanov | e4ad91f | 2015-06-12 15:00:31 -0700 | [diff] [blame] | 3741 |       DL_ERR("%s: has text relocations", get_realpath()); | 
 | 3742 |       return false; | 
 | 3743 |     } | 
| Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 3744 |     // Make segments writable to allow text relocations to work properly. We will later call | 
| Dmitriy Ivanov | 7e03993 | 2015-10-01 14:02:19 -0700 | [diff] [blame] | 3745 |     // phdr_table_protect_segments() after all of them are applied. | 
| Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 3746 |     DL_WARN("%s has text relocations. This is wasting memory and prevents " | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3747 |             "security hardening. Please fix.", get_realpath()); | 
| Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 3748 |     if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) { | 
 | 3749 |       DL_ERR("can't unprotect loadable segments for \"%s\": %s", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3750 |              get_realpath(), strerror(errno)); | 
| Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 3751 |       return false; | 
 | 3752 |     } | 
 | 3753 |   } | 
 | 3754 | #endif | 
 | 3755 |  | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3756 |   if (android_relocs_ != nullptr) { | 
 | 3757 |     // check signature | 
 | 3758 |     if (android_relocs_size_ > 3 && | 
 | 3759 |         android_relocs_[0] == 'A' && | 
 | 3760 |         android_relocs_[1] == 'P' && | 
| Dmitriy Ivanov | 18870d3 | 2015-04-22 13:10:04 -0700 | [diff] [blame] | 3761 |         android_relocs_[2] == 'S' && | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3762 |         android_relocs_[3] == '2') { | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3763 |       DEBUG("[ android relocating %s ]", get_realpath()); | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3764 |  | 
 | 3765 |       bool relocated = false; | 
 | 3766 |       const uint8_t* packed_relocs = android_relocs_ + 4; | 
 | 3767 |       const size_t packed_relocs_size = android_relocs_size_ - 4; | 
 | 3768 |  | 
| Dmitriy Ivanov | 18870d3 | 2015-04-22 13:10:04 -0700 | [diff] [blame] | 3769 |       relocated = relocate( | 
| Dmitriy Ivanov | 7e4bbba | 2015-04-30 19:49:19 -0700 | [diff] [blame] | 3770 |           version_tracker, | 
| Dmitriy Ivanov | 18870d3 | 2015-04-22 13:10:04 -0700 | [diff] [blame] | 3771 |           packed_reloc_iterator<sleb128_decoder>( | 
 | 3772 |             sleb128_decoder(packed_relocs, packed_relocs_size)), | 
 | 3773 |           global_group, local_group); | 
| Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 3774 |  | 
 | 3775 |       if (!relocated) { | 
 | 3776 |         return false; | 
 | 3777 |       } | 
 | 3778 |     } else { | 
 | 3779 |       DL_ERR("bad android relocation header."); | 
 | 3780 |       return false; | 
 | 3781 |     } | 
 | 3782 |   } | 
 | 3783 |  | 
| Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 3784 | #if defined(USE_RELA) | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3785 |   if (rela_ != nullptr) { | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3786 |     DEBUG("[ relocating %s ]", get_realpath()); | 
| Dmitriy Ivanov | 7e4bbba | 2015-04-30 19:49:19 -0700 | [diff] [blame] | 3787 |     if (!relocate(version_tracker, | 
 | 3788 |             plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) { | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3789 |       return false; | 
| Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 3790 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3791 |   } | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3792 |   if (plt_rela_ != nullptr) { | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3793 |     DEBUG("[ relocating %s plt ]", get_realpath()); | 
| Dmitriy Ivanov | 7e4bbba | 2015-04-30 19:49:19 -0700 | [diff] [blame] | 3794 |     if (!relocate(version_tracker, | 
 | 3795 |             plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) { | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3796 |       return false; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3797 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3798 |   } | 
| Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 3799 | #else | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3800 |   if (rel_ != nullptr) { | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3801 |     DEBUG("[ relocating %s ]", get_realpath()); | 
| Dmitriy Ivanov | 7e4bbba | 2015-04-30 19:49:19 -0700 | [diff] [blame] | 3802 |     if (!relocate(version_tracker, | 
 | 3803 |             plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) { | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3804 |       return false; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3805 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3806 |   } | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3807 |   if (plt_rel_ != nullptr) { | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3808 |     DEBUG("[ relocating %s plt ]", get_realpath()); | 
| Dmitriy Ivanov | 7e4bbba | 2015-04-30 19:49:19 -0700 | [diff] [blame] | 3809 |     if (!relocate(version_tracker, | 
 | 3810 |             plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) { | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3811 |       return false; | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 3812 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3813 |   } | 
| Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 3814 | #endif | 
| Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 3815 |  | 
| Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 3816 | #if defined(__mips__) | 
| Dmitriy Ivanov | f39cb63 | 2015-04-30 20:17:03 -0700 | [diff] [blame] | 3817 |   if (!mips_relocate_got(version_tracker, global_group, local_group)) { | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3818 |     return false; | 
 | 3819 |   } | 
| Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 3820 | #endif | 
 | 3821 |  | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3822 |   DEBUG("[ finished linking %s ]", get_realpath()); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3823 |  | 
| Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 3824 | #if !defined(__LP64__) | 
 | 3825 |   if (has_text_relocations) { | 
 | 3826 |     // All relocations are done, we can protect our segments back to read-only. | 
 | 3827 |     if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) { | 
 | 3828 |       DL_ERR("can't protect segments for \"%s\": %s", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3829 |              get_realpath(), strerror(errno)); | 
| Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 3830 |       return false; | 
 | 3831 |     } | 
 | 3832 |   } | 
 | 3833 | #endif | 
 | 3834 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3835 |   /* We can also turn on GNU RELRO protection */ | 
 | 3836 |   if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) { | 
 | 3837 |     DL_ERR("can't enable GNU RELRO protection for \"%s\": %s", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3838 |            get_realpath(), strerror(errno)); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3839 |     return false; | 
 | 3840 |   } | 
| Nick Kralevich | 9ec0f03 | 2012-02-28 10:40:00 -0800 | [diff] [blame] | 3841 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3842 |   /* Handle serializing/sharing the RELRO segment */ | 
 | 3843 |   if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) { | 
 | 3844 |     if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias, | 
 | 3845 |                                        extinfo->relro_fd) < 0) { | 
 | 3846 |       DL_ERR("failed serializing GNU RELRO section for \"%s\": %s", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3847 |              get_realpath(), strerror(errno)); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3848 |       return false; | 
| Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 3849 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3850 |   } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) { | 
 | 3851 |     if (phdr_table_map_gnu_relro(phdr, phnum, load_bias, | 
 | 3852 |                                  extinfo->relro_fd) < 0) { | 
 | 3853 |       DL_ERR("failed mapping GNU RELRO section for \"%s\": %s", | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 3854 |              get_realpath(), strerror(errno)); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3855 |       return false; | 
 | 3856 |     } | 
 | 3857 |   } | 
| Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 3858 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3859 |   notify_gdb_of_load(this); | 
 | 3860 |   return true; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3861 | } | 
 | 3862 |  | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 3863 | /* | 
| Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 3864 |  * This function add vdso to internal dso list. | 
 | 3865 |  * It helps to stack unwinding through signal handlers. | 
 | 3866 |  * Also, it makes bionic more like glibc. | 
 | 3867 |  */ | 
| Kito Cheng | 812fd42 | 2014-03-25 22:53:56 +0800 | [diff] [blame] | 3868 | static void add_vdso(KernelArgumentBlock& args __unused) { | 
| Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 3869 | #if defined(AT_SYSINFO_EHDR) | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3870 |   ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR)); | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 3871 |   if (ehdr_vdso == nullptr) { | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3872 |     return; | 
 | 3873 |   } | 
| Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 3874 |  | 
| Dmitriy Ivanov | d9b08a0 | 2015-11-16 13:17:27 -0800 | [diff] [blame] | 3875 |   soinfo* si = soinfo_alloc(&g_default_namespace, "[vdso]", nullptr, 0, 0); | 
| Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 3876 |  | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3877 |   si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff); | 
 | 3878 |   si->phnum = ehdr_vdso->e_phnum; | 
 | 3879 |   si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso); | 
 | 3880 |   si->size = phdr_table_get_load_size(si->phdr, si->phnum); | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3881 |   si->load_bias = get_elf_exec_load_bias(ehdr_vdso); | 
| Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 3882 |  | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3883 |   si->prelink_image(); | 
 | 3884 |   si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr); | 
| Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 3885 | #endif | 
 | 3886 | } | 
 | 3887 |  | 
 | 3888 | /* | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3889 |  * This is linker soinfo for GDB. See details below. | 
 | 3890 |  */ | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 3891 | #if defined(__LP64__) | 
 | 3892 | #define LINKER_PATH "/system/bin/linker64" | 
 | 3893 | #else | 
 | 3894 | #define LINKER_PATH "/system/bin/linker" | 
 | 3895 | #endif | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3896 |  | 
 | 3897 | // This is done to avoid calling c-tor prematurely | 
 | 3898 | // because soinfo c-tor needs memory allocator | 
 | 3899 | // which might be initialized after global variables. | 
 | 3900 | static uint8_t linker_soinfo_for_gdb_buf[sizeof(soinfo)] __attribute__((aligned(8))); | 
 | 3901 | static soinfo* linker_soinfo_for_gdb = nullptr; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3902 |  | 
 | 3903 | /* gdb expects the linker to be in the debug shared object list. | 
 | 3904 |  * Without this, gdb has trouble locating the linker's ".text" | 
 | 3905 |  * and ".plt" sections. Gdb could also potentially use this to | 
 | 3906 |  * relocate the offset of our exported 'rtld_db_dlactivity' symbol. | 
 | 3907 |  * Don't use soinfo_alloc(), because the linker shouldn't | 
 | 3908 |  * be on the soinfo list. | 
 | 3909 |  */ | 
 | 3910 | static void init_linker_info_for_gdb(ElfW(Addr) linker_base) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 3911 |   linker_soinfo_for_gdb = new (linker_soinfo_for_gdb_buf) soinfo(nullptr, LINKER_PATH, | 
 | 3912 |                                                                  nullptr, 0, 0); | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3913 |  | 
| Dmitriy Ivanov | 175dae9 | 2015-06-10 19:46:19 -0700 | [diff] [blame] | 3914 |   linker_soinfo_for_gdb->load_bias = linker_base; | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3915 |  | 
 | 3916 |   /* | 
 | 3917 |    * Set the dynamic field in the link map otherwise gdb will complain with | 
 | 3918 |    * the following: | 
 | 3919 |    *   warning: .dynamic section for "/system/bin/linker" is not at the | 
 | 3920 |    *   expected address (wrong library or version mismatch?) | 
 | 3921 |    */ | 
 | 3922 |   ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base); | 
 | 3923 |   ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff); | 
 | 3924 |   phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base, | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3925 |                                  &linker_soinfo_for_gdb->dynamic, nullptr); | 
 | 3926 |   insert_soinfo_into_debug_map(linker_soinfo_for_gdb); | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3927 | } | 
 | 3928 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 3929 | static void init_default_namespace() { | 
 | 3930 |   g_default_namespace.set_name("(default)"); | 
 | 3931 |   g_default_namespace.set_isolated(false); | 
 | 3932 |  | 
| Evgenii Stepanov | d640b22 | 2015-07-10 17:54:01 -0700 | [diff] [blame] | 3933 |   const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum, | 
 | 3934 |                                                        somain->load_bias); | 
 | 3935 |   const char* bname = basename(interp); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 3936 |   if (bname && (strcmp(bname, "linker_asan") == 0 || strcmp(bname, "linker_asan64") == 0)) { | 
| Evgenii Stepanov | d640b22 | 2015-07-10 17:54:01 -0700 | [diff] [blame] | 3937 |     g_default_ld_paths = kAsanDefaultLdPaths; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 3938 |   } else { | 
| Evgenii Stepanov | d640b22 | 2015-07-10 17:54:01 -0700 | [diff] [blame] | 3939 |     g_default_ld_paths = kDefaultLdPaths; | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 3940 |   } | 
 | 3941 |  | 
 | 3942 |   std::vector<std::string> ld_default_paths; | 
 | 3943 |   for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) { | 
 | 3944 |     ld_default_paths.push_back(g_default_ld_paths[i]); | 
 | 3945 |   } | 
 | 3946 |  | 
 | 3947 |   g_default_namespace.set_default_library_paths(std::move(ld_default_paths)); | 
| Evgenii Stepanov | d640b22 | 2015-07-10 17:54:01 -0700 | [diff] [blame] | 3948 | }; | 
 | 3949 |  | 
| Dmitriy Ivanov | b4e5067 | 2015-04-28 15:49:26 -0700 | [diff] [blame] | 3950 | extern "C" int __system_properties_init(void); | 
 | 3951 |  | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3952 | /* | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 3953 |  * This code is called after the linker has linked itself and | 
 | 3954 |  * fixed it's own GOT. It is safe to make references to externs | 
 | 3955 |  * and other non-local data at this point. | 
 | 3956 |  */ | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3957 | static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) { | 
| Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 3958 | #if TIMING | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3959 |   struct timeval t0, t1; | 
 | 3960 |   gettimeofday(&t0, 0); | 
| Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 3961 | #endif | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3962 |  | 
| Elliott Hughes | 1801db3 | 2015-06-08 18:04:00 -0700 | [diff] [blame] | 3963 |   // Sanitize the environment. | 
 | 3964 |   __libc_init_AT_SECURE(args); | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 3965 |  | 
| Dmitriy Ivanov | b4e5067 | 2015-04-28 15:49:26 -0700 | [diff] [blame] | 3966 |   // Initialize system properties | 
 | 3967 |   __system_properties_init(); // may use 'environ' | 
 | 3968 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3969 |   debuggerd_init(); | 
 | 3970 |  | 
 | 3971 |   // Get a few environment variables. | 
| Elliott Hughes | 1801db3 | 2015-06-08 18:04:00 -0700 | [diff] [blame] | 3972 |   const char* LD_DEBUG = getenv("LD_DEBUG"); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3973 |   if (LD_DEBUG != nullptr) { | 
 | 3974 |     g_ld_debug_verbosity = atoi(LD_DEBUG); | 
 | 3975 |   } | 
 | 3976 |  | 
| Elliott Hughes | 116b569 | 2016-01-04 17:45:36 -0800 | [diff] [blame] | 3977 | #if defined(__LP64__) | 
 | 3978 |   INFO("[ Android dynamic linker (64-bit) ]"); | 
 | 3979 | #else | 
 | 3980 |   INFO("[ Android dynamic linker (32-bit) ]"); | 
 | 3981 | #endif | 
 | 3982 |  | 
| Elliott Hughes | 1801db3 | 2015-06-08 18:04:00 -0700 | [diff] [blame] | 3983 |   // These should have been sanitized by __libc_init_AT_SECURE, but the test | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3984 |   // doesn't cost us anything. | 
 | 3985 |   const char* ldpath_env = nullptr; | 
 | 3986 |   const char* ldpreload_env = nullptr; | 
| Elliott Hughes | 1801db3 | 2015-06-08 18:04:00 -0700 | [diff] [blame] | 3987 |   if (!getauxval(AT_SECURE)) { | 
 | 3988 |     ldpath_env = getenv("LD_LIBRARY_PATH"); | 
| Elliott Hughes | 116b569 | 2016-01-04 17:45:36 -0800 | [diff] [blame] | 3989 |     if (ldpath_env != nullptr) { | 
 | 3990 |       INFO("[ LD_LIBRARY_PATH set to '%s' ]", ldpath_env); | 
 | 3991 |     } | 
| Elliott Hughes | 1801db3 | 2015-06-08 18:04:00 -0700 | [diff] [blame] | 3992 |     ldpreload_env = getenv("LD_PRELOAD"); | 
| Elliott Hughes | 116b569 | 2016-01-04 17:45:36 -0800 | [diff] [blame] | 3993 |     if (ldpreload_env != nullptr) { | 
 | 3994 |       INFO("[ LD_PRELOAD set to '%s' ]", ldpreload_env); | 
 | 3995 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3996 |   } | 
 | 3997 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 3998 |   soinfo* si = soinfo_alloc(&g_default_namespace, args.argv[0], nullptr, 0, RTLD_GLOBAL); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3999 |   if (si == nullptr) { | 
 | 4000 |     exit(EXIT_FAILURE); | 
 | 4001 |   } | 
 | 4002 |  | 
 | 4003 |   /* bootstrap the link map, the main exe always needs to be first */ | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 4004 |   si->set_main_executable(); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4005 |   link_map* map = &(si->link_map_head); | 
 | 4006 |  | 
 | 4007 |   map->l_addr = 0; | 
 | 4008 |   map->l_name = args.argv[0]; | 
 | 4009 |   map->l_prev = nullptr; | 
 | 4010 |   map->l_next = nullptr; | 
 | 4011 |  | 
 | 4012 |   _r_debug.r_map = map; | 
 | 4013 |   r_debug_tail = map; | 
 | 4014 |  | 
 | 4015 |   init_linker_info_for_gdb(linker_base); | 
 | 4016 |  | 
 | 4017 |   // Extract information passed from the kernel. | 
 | 4018 |   si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR)); | 
 | 4019 |   si->phnum = args.getauxval(AT_PHNUM); | 
 | 4020 |   si->entry = args.getauxval(AT_ENTRY); | 
 | 4021 |  | 
 | 4022 |   /* Compute the value of si->base. We can't rely on the fact that | 
 | 4023 |    * the first entry is the PHDR because this will not be true | 
 | 4024 |    * for certain executables (e.g. some in the NDK unit test suite) | 
 | 4025 |    */ | 
 | 4026 |   si->base = 0; | 
 | 4027 |   si->size = phdr_table_get_load_size(si->phdr, si->phnum); | 
 | 4028 |   si->load_bias = 0; | 
 | 4029 |   for (size_t i = 0; i < si->phnum; ++i) { | 
 | 4030 |     if (si->phdr[i].p_type == PT_PHDR) { | 
 | 4031 |       si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr; | 
 | 4032 |       si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset; | 
 | 4033 |       break; | 
| Nick Kralevich | 8d3e91d | 2013-04-25 13:15:24 -0700 | [diff] [blame] | 4034 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4035 |   } | 
 | 4036 |   si->dynamic = nullptr; | 
| Nick Kralevich | 8d3e91d | 2013-04-25 13:15:24 -0700 | [diff] [blame] | 4037 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4038 |   ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base); | 
 | 4039 |   if (elf_hdr->e_type != ET_DYN) { | 
 | 4040 |     __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n"); | 
 | 4041 |     exit(EXIT_FAILURE); | 
 | 4042 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4043 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4044 |   // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid). | 
 | 4045 |   parse_LD_LIBRARY_PATH(ldpath_env); | 
 | 4046 |   parse_LD_PRELOAD(ldpreload_env); | 
| David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 4047 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4048 |   somain = si; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4049 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 4050 |   init_default_namespace(); | 
| Evgenii Stepanov | d640b22 | 2015-07-10 17:54:01 -0700 | [diff] [blame] | 4051 |  | 
| Dmitriy Ivanov | 6718125 | 2015-01-07 15:48:25 -0800 | [diff] [blame] | 4052 |   if (!si->prelink_image()) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 4053 |     __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer()); | 
| Dmitriy Ivanov | 6718125 | 2015-01-07 15:48:25 -0800 | [diff] [blame] | 4054 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4055 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 4056 |   // add somain to global group | 
 | 4057 |   si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL); | 
 | 4058 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4059 |   // Load ld_preloads and dependencies. | 
 | 4060 |   StringLinkedList needed_library_name_list; | 
 | 4061 |   size_t needed_libraries_count = 0; | 
 | 4062 |   size_t ld_preloads_count = 0; | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 4063 |  | 
 | 4064 |   for (const auto& ld_preload_name : g_ld_preload_names) { | 
 | 4065 |     needed_library_name_list.push_back(ld_preload_name.c_str()); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4066 |     ++needed_libraries_count; | 
| Dmitriy Ivanov | f8093a9 | 2015-04-28 18:09:53 -0700 | [diff] [blame] | 4067 |     ++ld_preloads_count; | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4068 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4069 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4070 |   for_each_dt_needed(si, [&](const char* name) { | 
 | 4071 |     needed_library_name_list.push_back(name); | 
 | 4072 |     ++needed_libraries_count; | 
 | 4073 |   }); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4074 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4075 |   const char* needed_library_names[needed_libraries_count]; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4076 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4077 |   memset(needed_library_names, 0, sizeof(needed_library_names)); | 
 | 4078 |   needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4079 |  | 
| Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 4080 |   if (needed_libraries_count > 0 && | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 4081 |       !find_libraries(&g_default_namespace, si, needed_library_names, needed_libraries_count, | 
 | 4082 |                       nullptr, &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr, | 
| Evgenii Stepanov | 0cdef7e | 2015-07-06 17:56:31 -0700 | [diff] [blame] | 4083 |                       /* add_as_children */ true)) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 4084 |     __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer()); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 4085 |   } else if (needed_libraries_count == 0) { | 
 | 4086 |     if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 4087 |       __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer()); | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 4088 |     } | 
 | 4089 |     si->increment_ref_count(); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4090 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4091 |  | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4092 |   add_vdso(args); | 
| Nick Kralevich | 2aebf54 | 2014-05-07 10:32:39 -0700 | [diff] [blame] | 4093 |  | 
| Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 4094 |   { | 
 | 4095 |     ProtectedDataGuard guard; | 
| Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 4096 |  | 
| Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 4097 |     si->call_pre_init_constructors(); | 
 | 4098 |  | 
 | 4099 |     /* After the prelink_image, the si->load_bias is initialized. | 
 | 4100 |      * For so lib, the map->l_addr will be updated in notify_gdb_of_load. | 
 | 4101 |      * We need to update this value for so exe here. So Unwind_Backtrace | 
 | 4102 |      * for some arch like x86 could work correctly within so exe. | 
 | 4103 |      */ | 
 | 4104 |     map->l_addr = si->load_bias; | 
 | 4105 |     si->call_constructors(); | 
 | 4106 |   } | 
| Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 4107 |  | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4108 | #if TIMING | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4109 |   gettimeofday(&t1, nullptr); | 
 | 4110 |   PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) ( | 
 | 4111 |            (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) - | 
 | 4112 |            (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec))); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4113 | #endif | 
 | 4114 | #if STATS | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4115 |   PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0], | 
 | 4116 |          linker_stats.count[kRelocAbsolute], | 
 | 4117 |          linker_stats.count[kRelocRelative], | 
 | 4118 |          linker_stats.count[kRelocCopy], | 
 | 4119 |          linker_stats.count[kRelocSymbol]); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4120 | #endif | 
 | 4121 | #if COUNT_PAGES | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4122 |   { | 
 | 4123 |     unsigned n; | 
 | 4124 |     unsigned i; | 
 | 4125 |     unsigned count = 0; | 
 | 4126 |     for (n = 0; n < 4096; n++) { | 
 | 4127 |       if (bitmask[n]) { | 
 | 4128 |         unsigned x = bitmask[n]; | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 4129 | #if defined(__LP64__) | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4130 |         for (i = 0; i < 32; i++) { | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 4131 | #else | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4132 |         for (i = 0; i < 8; i++) { | 
| Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 4133 | #endif | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4134 |           if (x & 1) { | 
 | 4135 |             count++; | 
 | 4136 |           } | 
 | 4137 |           x >>= 1; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4138 |         } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4139 |       } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4140 |     } | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4141 |     PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4); | 
 | 4142 |   } | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4143 | #endif | 
 | 4144 |  | 
 | 4145 | #if TIMING || STATS || COUNT_PAGES | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4146 |   fflush(stdout); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4147 | #endif | 
 | 4148 |  | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 4149 |   TRACE("[ Ready to execute '%s' @ %p ]", si->get_realpath(), reinterpret_cast<void*>(si->entry)); | 
| Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 4150 |   return si->entry; | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 4151 | } | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 4152 |  | 
| David 'Digit' Turner | bea23e5 | 2012-06-18 23:38:46 +0200 | [diff] [blame] | 4153 | /* Compute the load-bias of an existing executable. This shall only | 
 | 4154 |  * be used to compute the load bias of an executable or shared library | 
 | 4155 |  * that was loaded by the kernel itself. | 
 | 4156 |  * | 
 | 4157 |  * Input: | 
 | 4158 |  *    elf    -> address of ELF header, assumed to be at the start of the file. | 
 | 4159 |  * Return: | 
 | 4160 |  *    load bias, i.e. add the value of any p_vaddr in the file to get | 
 | 4161 |  *    the corresponding address in memory. | 
 | 4162 |  */ | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 4163 | static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) { | 
 | 4164 |   ElfW(Addr) offset = elf->e_phoff; | 
| Dmitriy Ivanov | 3edb918 | 2015-05-07 10:48:00 -0700 | [diff] [blame] | 4165 |   const ElfW(Phdr)* phdr_table = | 
 | 4166 |       reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset); | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 4167 |   const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum; | 
| David 'Digit' Turner | bea23e5 | 2012-06-18 23:38:46 +0200 | [diff] [blame] | 4168 |  | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 4169 |   for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) { | 
| Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 4170 |     if (phdr->p_type == PT_LOAD) { | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 4171 |       return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr; | 
| David 'Digit' Turner | bea23e5 | 2012-06-18 23:38:46 +0200 | [diff] [blame] | 4172 |     } | 
| Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 4173 |   } | 
 | 4174 |   return 0; | 
| David 'Digit' Turner | bea23e5 | 2012-06-18 23:38:46 +0200 | [diff] [blame] | 4175 | } | 
 | 4176 |  | 
| Elliott Hughes | 42d949f | 2016-01-06 19:51:43 -0800 | [diff] [blame] | 4177 | extern "C" int __set_tls(void*); | 
| Dmitriy Ivanov | efe1383 | 2014-07-28 15:05:51 -0700 | [diff] [blame] | 4178 | extern "C" void _start(); | 
 | 4179 |  | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 4180 | /* | 
 | 4181 |  * This is the entry point for the linker, called from begin.S. This | 
 | 4182 |  * method is responsible for fixing the linker's own relocations, and | 
 | 4183 |  * then calling __linker_init_post_relocation(). | 
 | 4184 |  * | 
 | 4185 |  * Because this method is called before the linker has fixed it's own | 
 | 4186 |  * relocations, any attempt to reference an extern variable, extern | 
 | 4187 |  * function, or other GOT reference will generate a segfault. | 
 | 4188 |  */ | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 4189 | extern "C" ElfW(Addr) __linker_init(void* raw_args) { | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 4190 |   KernelArgumentBlock args(raw_args); | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 4191 |  | 
| Elliott Hughes | 42d949f | 2016-01-06 19:51:43 -0800 | [diff] [blame] | 4192 |   void* tls[BIONIC_TLS_SLOTS]; | 
 | 4193 |   __set_tls(tls); | 
 | 4194 |  | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 4195 |   ElfW(Addr) linker_addr = args.getauxval(AT_BASE); | 
| Dmitriy Ivanov | efe1383 | 2014-07-28 15:05:51 -0700 | [diff] [blame] | 4196 |   ElfW(Addr) entry_point = args.getauxval(AT_ENTRY); | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 4197 |   ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr); | 
| Elliott Hughes | faf05ba | 2014-02-11 16:59:37 -0800 | [diff] [blame] | 4198 |   ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff); | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 4199 |  | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 4200 |   soinfo linker_so(nullptr, nullptr, nullptr, 0, 0); | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 4201 |  | 
| Dmitriy Ivanov | efe1383 | 2014-07-28 15:05:51 -0700 | [diff] [blame] | 4202 |   // If the linker is not acting as PT_INTERP entry_point is equal to | 
 | 4203 |   // _start. Which means that the linker is running as an executable and | 
 | 4204 |   // already linked by PT_INTERP. | 
 | 4205 |   // | 
 | 4206 |   // This happens when user tries to run 'adb shell /system/bin/linker' | 
 | 4207 |   // see also https://code.google.com/p/android/issues/detail?id=63174 | 
 | 4208 |   if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 4209 |     __libc_fatal("This is %s, the helper program for shared library executables.", args.argv[0]); | 
| Dmitriy Ivanov | efe1383 | 2014-07-28 15:05:51 -0700 | [diff] [blame] | 4210 |   } | 
 | 4211 |  | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 4212 |   linker_so.base = linker_addr; | 
 | 4213 |   linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum); | 
 | 4214 |   linker_so.load_bias = get_elf_exec_load_bias(elf_hdr); | 
| Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 4215 |   linker_so.dynamic = nullptr; | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 4216 |   linker_so.phdr = phdr; | 
 | 4217 |   linker_so.phnum = elf_hdr->e_phnum; | 
| Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 4218 |   linker_so.set_linker_flag(); | 
| Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 4219 |  | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 4220 |   // This might not be obvious... The reasons why we pass g_empty_list | 
 | 4221 |   // in place of local_group here are (1) we do not really need it, because | 
 | 4222 |   // linker is built with DT_SYMBOLIC and therefore relocates its symbols against | 
 | 4223 |   // itself without having to look into local_group and (2) allocators | 
 | 4224 |   // are not yet initialized, and therefore we cannot use linked_list.push_* | 
 | 4225 |   // functions at this point. | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 4226 |   if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) { | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 4227 |     __libc_fatal("CANNOT LINK EXECUTABLE: %s", linker_get_error_buffer()); | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 4228 |   } | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 4229 |  | 
| Elliott Hughes | d294863 | 2015-07-21 11:57:09 -0700 | [diff] [blame] | 4230 |   __libc_init_main_thread(args); | 
| Dmitriy Ivanov | 1424140 | 2014-08-26 14:16:52 -0700 | [diff] [blame] | 4231 |  | 
| Josh Gao | 93c0f5e | 2015-10-06 11:08:13 -0700 | [diff] [blame] | 4232 |   // Initialize the linker's static libc's globals | 
 | 4233 |   __libc_init_globals(args); | 
 | 4234 |  | 
| Dmitriy Ivanov | efe1383 | 2014-07-28 15:05:51 -0700 | [diff] [blame] | 4235 |   // Initialize the linker's own global variables | 
| Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 4236 |   linker_so.call_constructors(); | 
| Dmitriy Ivanov | 4151ea7 | 2014-07-24 15:33:25 -0700 | [diff] [blame] | 4237 |  | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 4238 |   // Initialize static variables. Note that in order to | 
 | 4239 |   // get correct libdl_info we need to call constructors | 
 | 4240 |   // before get_libdl_info(). | 
 | 4241 |   solist = get_libdl_info(); | 
 | 4242 |   sonext = get_libdl_info(); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 4243 |   g_default_namespace.soinfo_list().push_back(get_libdl_info()); | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 4244 |  | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 4245 |   // We have successfully fixed our own relocations. It's safe to run | 
 | 4246 |   // the main part of the linker now. | 
| Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 4247 |   args.abort_message_ptr = &g_abort_message; | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 4248 |   ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr); | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 4249 |  | 
| Elliott Hughes | 116b569 | 2016-01-04 17:45:36 -0800 | [diff] [blame] | 4250 |   INFO("[ Jumping to _start (%p)... ]", reinterpret_cast<void*>(start_address)); | 
| Elliott Hughes | 611f956 | 2015-01-23 10:43:58 -0800 | [diff] [blame] | 4251 |  | 
| Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 4252 |   // Return the address that the calling assembly stub should jump to. | 
 | 4253 |   return start_address; | 
| Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 4254 | } |