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 | |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 29 | #include <dlfcn.h> |
| 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> |
Dmitriy Ivanov | bfa15e4 | 2015-01-07 15:05:49 -0800 | [diff] [blame] | 39 | #include <sys/personality.h> |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 40 | #include <unistd.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 41 | |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 42 | #include <new> |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 43 | #include <string> |
| 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 | 04dc91a | 2014-07-01 14:10:16 -0700 | [diff] [blame] | 50 | #include "private/ScopedFd.h" |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 51 | #include "private/ScopeGuard.h" |
| 52 | #include "private/UniquePtr.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 53 | |
| 54 | #include "linker.h" |
Dmitriy Ivanov | c9ce70d | 2015-03-10 15:30:26 -0700 | [diff] [blame] | 55 | #include "linker_block_allocator.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 56 | #include "linker_debug.h" |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 57 | #include "linker_environ.h" |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 58 | #include "linker_leb128.h" |
David 'Digit' Turner | 23363ed | 2012-06-18 18:13:49 +0200 | [diff] [blame] | 59 | #include "linker_phdr.h" |
Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 60 | #include "linker_relocs.h" |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 61 | #include "linker_reloc_iterators.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 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 64 | /* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<< |
| 65 | * |
| 66 | * Do NOT use malloc() and friends or pthread_*() code here. |
| 67 | * Don't use printf() either; it's caused mysterious memory |
| 68 | * corruption in the past. |
| 69 | * The linker runs before we bring up libc and it's easiest |
| 70 | * to make sure it does not depend on any complex libc features |
| 71 | * |
| 72 | * open issues / todo: |
| 73 | * |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 74 | * - cleaner error reporting |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 75 | * - after linking, set as much stuff as possible to READONLY |
| 76 | * and NOEXEC |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 77 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 78 | |
Dmitriy Ivanov | 1649e7e | 2015-01-22 16:04:25 -0800 | [diff] [blame] | 79 | // Override macros to use C++ style casts |
| 80 | #undef ELF_ST_TYPE |
| 81 | #define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf) |
| 82 | |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 83 | 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] | 84 | |
Dmitriy Ivanov | 600bc3c | 2015-03-10 15:43:50 -0700 | [diff] [blame] | 85 | static LinkerTypeAllocator<soinfo> g_soinfo_allocator; |
| 86 | static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator; |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 87 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 88 | static soinfo* solist; |
| 89 | static soinfo* sonext; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 90 | 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] | 91 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 92 | static const char* const kDefaultLdPaths[] = { |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 93 | #if defined(__LP64__) |
Elliott Hughes | 011bc0b | 2013-10-08 14:27:10 -0700 | [diff] [blame] | 94 | "/vendor/lib64", |
| 95 | "/system/lib64", |
| 96 | #else |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 97 | "/vendor/lib", |
| 98 | "/system/lib", |
Elliott Hughes | 011bc0b | 2013-10-08 14:27:10 -0700 | [diff] [blame] | 99 | #endif |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 100 | nullptr |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 101 | }; |
David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 102 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 103 | static const ElfW(Versym) kVersymNotNeeded = 0; |
| 104 | static const ElfW(Versym) kVersymGlobal = 1; |
| 105 | |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 106 | static std::vector<std::string> g_ld_library_paths; |
| 107 | static std::vector<std::string> g_ld_preload_names; |
Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 108 | |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 109 | static std::vector<soinfo*> g_ld_preloads; |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 110 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 111 | __LIBC_HIDDEN__ int g_ld_debug_verbosity; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 112 | |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 113 | __LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd. |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 114 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 115 | #if STATS |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 116 | struct linker_stats_t { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 117 | int count[kRelocMax]; |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 118 | }; |
| 119 | |
| 120 | static linker_stats_t linker_stats; |
| 121 | |
Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 122 | void count_relocation(RelocationKind kind) { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 123 | ++linker_stats.count[kind]; |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 124 | } |
| 125 | #else |
Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 126 | void count_relocation(RelocationKind) { |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 127 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 128 | #endif |
| 129 | |
| 130 | #if COUNT_PAGES |
Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 131 | uint32_t bitmask[4096]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 132 | #endif |
| 133 | |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 134 | static char __linker_dl_err_buf[768]; |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 135 | |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 136 | char* linker_get_error_buffer() { |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 137 | return &__linker_dl_err_buf[0]; |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 138 | } |
| 139 | |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 140 | size_t linker_get_error_buffer_size() { |
| 141 | return sizeof(__linker_dl_err_buf); |
| 142 | } |
| 143 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 144 | // This function is an empty stub where GDB locates a breakpoint to get notified |
| 145 | // about linker activity. |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 146 | extern "C" |
| 147 | void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 148 | |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 149 | static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER; |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 150 | static r_debug _r_debug = |
| 151 | {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0}; |
| 152 | |
Elliott Hughes | 3a9c5d6 | 2014-02-10 13:31:13 -0800 | [diff] [blame] | 153 | static link_map* r_debug_tail = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 154 | |
Elliott Hughes | 3a9c5d6 | 2014-02-10 13:31:13 -0800 | [diff] [blame] | 155 | static void insert_soinfo_into_debug_map(soinfo* info) { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 156 | // Copy the necessary fields into the debug structure. |
| 157 | link_map* map = &(info->link_map_head); |
| 158 | map->l_addr = info->load_bias; |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 159 | // link_map l_name field is not const. |
| 160 | map->l_name = const_cast<char*>(info->get_realpath()); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 161 | map->l_ld = info->dynamic; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 162 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 163 | // Stick the new library at the end of the list. |
| 164 | // gdb tends to care more about libc than it does |
| 165 | // about leaf libraries, and ordering it this way |
| 166 | // reduces the back-and-forth over the wire. |
| 167 | if (r_debug_tail) { |
| 168 | r_debug_tail->l_next = map; |
| 169 | map->l_prev = r_debug_tail; |
| 170 | map->l_next = 0; |
| 171 | } else { |
| 172 | _r_debug.r_map = map; |
| 173 | map->l_prev = 0; |
| 174 | map->l_next = 0; |
| 175 | } |
| 176 | r_debug_tail = map; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 177 | } |
| 178 | |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 179 | static void remove_soinfo_from_debug_map(soinfo* info) { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 180 | link_map* map = &(info->link_map_head); |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 181 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 182 | if (r_debug_tail == map) { |
| 183 | r_debug_tail = map->l_prev; |
| 184 | } |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 185 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 186 | if (map->l_prev) { |
| 187 | map->l_prev->l_next = map->l_next; |
| 188 | } |
| 189 | if (map->l_next) { |
| 190 | map->l_next->l_prev = map->l_prev; |
| 191 | } |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 192 | } |
| 193 | |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 194 | static void notify_gdb_of_load(soinfo* info) { |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 195 | if (info->is_main_executable()) { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 196 | // GDB already knows about the main executable |
| 197 | return; |
| 198 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 199 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 200 | ScopedPthreadMutexLocker locker(&g__r_debug_mutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 201 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 202 | _r_debug.r_state = r_debug::RT_ADD; |
| 203 | rtld_db_dlactivity(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 204 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 205 | insert_soinfo_into_debug_map(info); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 206 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 207 | _r_debug.r_state = r_debug::RT_CONSISTENT; |
| 208 | rtld_db_dlactivity(); |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 209 | } |
| 210 | |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 211 | static void notify_gdb_of_unload(soinfo* info) { |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 212 | if (info->is_main_executable()) { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 213 | // GDB already knows about the main executable |
| 214 | return; |
| 215 | } |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 216 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 217 | ScopedPthreadMutexLocker locker(&g__r_debug_mutex); |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 218 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 219 | _r_debug.r_state = r_debug::RT_DELETE; |
| 220 | rtld_db_dlactivity(); |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 221 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 222 | remove_soinfo_from_debug_map(info); |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 223 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 224 | _r_debug.r_state = r_debug::RT_CONSISTENT; |
| 225 | rtld_db_dlactivity(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 226 | } |
| 227 | |
Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 228 | void notify_gdb_of_libraries() { |
Elliott Hughes | 3a9c5d6 | 2014-02-10 13:31:13 -0800 | [diff] [blame] | 229 | _r_debug.r_state = r_debug::RT_ADD; |
| 230 | rtld_db_dlactivity(); |
| 231 | _r_debug.r_state = r_debug::RT_CONSISTENT; |
| 232 | rtld_db_dlactivity(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 233 | } |
| 234 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 235 | LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() { |
| 236 | return g_soinfo_links_allocator.alloc(); |
| 237 | } |
| 238 | |
| 239 | void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) { |
| 240 | g_soinfo_links_allocator.free(entry); |
| 241 | } |
| 242 | |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 243 | static soinfo* soinfo_alloc(const char* name, struct stat* file_stat, |
| 244 | off64_t file_offset, uint32_t rtld_flags) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 245 | if (strlen(name) >= PATH_MAX) { |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 246 | DL_ERR("library name \"%s\" too long", name); |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 247 | return nullptr; |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 248 | } |
| 249 | |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 250 | soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags); |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 251 | |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 252 | sonext->next = si; |
| 253 | sonext = si; |
| 254 | |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 255 | TRACE("name %s: allocated soinfo @ %p", name, si); |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 256 | return si; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 257 | } |
| 258 | |
Elliott Hughes | faf05ba | 2014-02-11 16:59:37 -0800 | [diff] [blame] | 259 | static void soinfo_free(soinfo* si) { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 260 | if (si == nullptr) { |
| 261 | return; |
| 262 | } |
| 263 | |
| 264 | if (si->base != 0 && si->size != 0) { |
| 265 | munmap(reinterpret_cast<void*>(si->base), si->size); |
| 266 | } |
| 267 | |
| 268 | soinfo *prev = nullptr, *trav; |
| 269 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 270 | TRACE("name %s: freeing soinfo @ %p", si->get_soname(), si); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 271 | |
| 272 | for (trav = solist; trav != nullptr; trav = trav->next) { |
| 273 | if (trav == si) { |
| 274 | break; |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 275 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 276 | prev = trav; |
| 277 | } |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 278 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 279 | if (trav == nullptr) { |
| 280 | // si was not in solist |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 281 | DL_ERR("name \"%s\"@%p is not in solist!", si->get_soname(), si); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 282 | return; |
| 283 | } |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 284 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 285 | // clear links to/from si |
| 286 | si->remove_all_links(); |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 287 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 288 | // prev will never be null, because the first entry in solist is |
| 289 | // always the static libdl_info. |
| 290 | prev->next = si->next; |
| 291 | if (si == sonext) { |
| 292 | sonext = prev; |
| 293 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 294 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 295 | g_soinfo_allocator.free(si); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 296 | } |
| 297 | |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 298 | static void parse_path(const char* path, const char* delimiters, |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 299 | std::vector<std::string>* paths) { |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 300 | if (path == nullptr) { |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 301 | return; |
| 302 | } |
| 303 | |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 304 | paths->clear(); |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 305 | |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 306 | for (const char *p = path; ; ++p) { |
| 307 | size_t len = strcspn(p, delimiters); |
| 308 | // skip empty tokens |
| 309 | if (len == 0) { |
| 310 | continue; |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 311 | } |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 312 | |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 313 | paths->push_back(std::string(p, len)); |
| 314 | p += len; |
| 315 | |
| 316 | if (*p == '\0') { |
| 317 | break; |
| 318 | } |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 319 | } |
| 320 | } |
| 321 | |
| 322 | static void parse_LD_LIBRARY_PATH(const char* path) { |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 323 | parse_path(path, ":", &g_ld_library_paths); |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 324 | } |
| 325 | |
| 326 | static void parse_LD_PRELOAD(const char* path) { |
| 327 | // We have historically supported ':' as well as ' ' in LD_PRELOAD. |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 328 | parse_path(path, " :", &g_ld_preload_names); |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 329 | } |
| 330 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 331 | static bool realpath_fd(int fd, std::string* realpath) { |
| 332 | std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX); |
| 333 | snprintf(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd); |
| 334 | if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) { |
| 335 | return false; |
| 336 | } |
| 337 | |
| 338 | *realpath = std::string(&buf[0]); |
| 339 | return true; |
| 340 | } |
| 341 | |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 342 | #if defined(__arm__) |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 343 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 344 | // For a given PC, find the .so that it belongs to. |
| 345 | // Returns the base address of the .ARM.exidx section |
| 346 | // for that .so, and the number of 8-byte entries |
| 347 | // in that section (via *pcount). |
| 348 | // |
| 349 | // Intended to be called by libc's __gnu_Unwind_Find_exidx(). |
| 350 | // |
| 351 | // This function is exposed via dlfcn.cpp and libdl.so. |
Elliott Hughes | faf05ba | 2014-02-11 16:59:37 -0800 | [diff] [blame] | 352 | _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) { |
Dmitriy Ivanov | 1649e7e | 2015-01-22 16:04:25 -0800 | [diff] [blame] | 353 | uintptr_t addr = reinterpret_cast<uintptr_t>(pc); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 354 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 355 | for (soinfo* si = solist; si != 0; si = si->next) { |
| 356 | if ((addr >= si->base) && (addr < (si->base + si->size))) { |
| 357 | *pcount = si->ARM_exidx_count; |
Dmitriy Ivanov | 1649e7e | 2015-01-22 16:04:25 -0800 | [diff] [blame] | 358 | return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 359 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 360 | } |
| 361 | *pcount = 0; |
| 362 | return nullptr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 363 | } |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 364 | |
Christopher Ferris | 24053a4 | 2013-08-19 17:45:09 -0700 | [diff] [blame] | 365 | #endif |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 366 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 367 | // Here, we only have to provide a callback to iterate across all the |
| 368 | // loaded libraries. gcc_eh does the rest. |
Elliott Hughes | faf05ba | 2014-02-11 16:59:37 -0800 | [diff] [blame] | 369 | int 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] | 370 | int rv = 0; |
| 371 | for (soinfo* si = solist; si != nullptr; si = si->next) { |
| 372 | dl_phdr_info dl_info; |
| 373 | dl_info.dlpi_addr = si->link_map_head.l_addr; |
| 374 | dl_info.dlpi_name = si->link_map_head.l_name; |
| 375 | dl_info.dlpi_phdr = si->phdr; |
| 376 | dl_info.dlpi_phnum = si->phnum; |
| 377 | rv = cb(&dl_info, sizeof(dl_phdr_info), data); |
| 378 | if (rv != 0) { |
| 379 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 380 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 381 | } |
| 382 | return rv; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 383 | } |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 384 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 385 | const ElfW(Versym)* soinfo::get_versym(size_t n) const { |
| 386 | if (has_min_version(2) && versym_ != nullptr) { |
| 387 | return versym_ + n; |
| 388 | } |
| 389 | |
| 390 | return nullptr; |
| 391 | } |
| 392 | |
| 393 | ElfW(Addr) soinfo::get_verneed_ptr() const { |
| 394 | if (has_min_version(2)) { |
| 395 | return verneed_ptr_; |
| 396 | } |
| 397 | |
| 398 | return 0; |
| 399 | } |
| 400 | |
| 401 | size_t soinfo::get_verneed_cnt() const { |
| 402 | if (has_min_version(2)) { |
| 403 | return verneed_cnt_; |
| 404 | } |
| 405 | |
| 406 | return 0; |
| 407 | } |
| 408 | |
| 409 | ElfW(Addr) soinfo::get_verdef_ptr() const { |
| 410 | if (has_min_version(2)) { |
| 411 | return verdef_ptr_; |
| 412 | } |
| 413 | |
| 414 | return 0; |
| 415 | } |
| 416 | |
| 417 | size_t soinfo::get_verdef_cnt() const { |
| 418 | if (has_min_version(2)) { |
| 419 | return verdef_cnt_; |
| 420 | } |
| 421 | |
| 422 | return 0; |
| 423 | } |
| 424 | |
| 425 | template<typename F> |
| 426 | static bool for_each_verdef(const soinfo* si, F functor) { |
| 427 | if (!si->has_min_version(2)) { |
| 428 | return true; |
| 429 | } |
| 430 | |
| 431 | uintptr_t verdef_ptr = si->get_verdef_ptr(); |
| 432 | if (verdef_ptr == 0) { |
| 433 | return true; |
| 434 | } |
| 435 | |
| 436 | size_t offset = 0; |
| 437 | |
| 438 | size_t verdef_cnt = si->get_verdef_cnt(); |
| 439 | for (size_t i = 0; i<verdef_cnt; ++i) { |
| 440 | const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset); |
| 441 | size_t verdaux_offset = offset + verdef->vd_aux; |
| 442 | offset += verdef->vd_next; |
| 443 | |
| 444 | if (verdef->vd_version != 1) { |
| 445 | DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1)", i, verdef->vd_version); |
| 446 | return false; |
| 447 | } |
| 448 | |
| 449 | if ((verdef->vd_flags & VER_FLG_BASE) != 0) { |
| 450 | // "this is the version of the file itself. It must not be used for |
| 451 | // matching a symbol. It can be used to match references." |
| 452 | // |
| 453 | // http://www.akkadia.org/drepper/symbol-versioning |
| 454 | continue; |
| 455 | } |
| 456 | |
| 457 | if (verdef->vd_cnt == 0) { |
| 458 | DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i); |
| 459 | return false; |
| 460 | } |
| 461 | |
| 462 | const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset); |
| 463 | |
| 464 | if (functor(i, verdef, verdaux) == true) { |
| 465 | break; |
| 466 | } |
| 467 | } |
| 468 | |
| 469 | return true; |
| 470 | } |
| 471 | |
| 472 | bool soinfo::find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const { |
| 473 | if (vi == nullptr) { |
| 474 | *versym = kVersymNotNeeded; |
| 475 | return true; |
| 476 | } |
| 477 | |
| 478 | *versym = kVersymGlobal; |
| 479 | |
| 480 | return for_each_verdef(this, |
| 481 | [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) { |
| 482 | if (verdef->vd_hash == vi->elf_hash && |
| 483 | strcmp(vi->name, get_string(verdaux->vda_name)) == 0) { |
| 484 | *versym = verdef->vd_ndx; |
| 485 | return true; |
| 486 | } |
| 487 | |
| 488 | return false; |
| 489 | } |
| 490 | ); |
| 491 | } |
| 492 | |
| 493 | bool soinfo::find_symbol_by_name(SymbolName& symbol_name, |
| 494 | const version_info* vi, |
| 495 | const ElfW(Sym)** symbol) const { |
| 496 | uint32_t symbol_index; |
| 497 | bool success = |
| 498 | is_gnu_hash() ? |
| 499 | gnu_lookup(symbol_name, vi, &symbol_index) : |
| 500 | elf_lookup(symbol_name, vi, &symbol_index); |
| 501 | |
| 502 | if (success) { |
| 503 | *symbol = symbol_index == 0 ? nullptr : symtab_ + symbol_index; |
| 504 | } |
| 505 | |
| 506 | return success; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 507 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 508 | |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 509 | static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) { |
| 510 | if (ELF_ST_BIND(s->st_info) == STB_GLOBAL || |
| 511 | ELF_ST_BIND(s->st_info) == STB_WEAK) { |
| 512 | return s->st_shndx != SHN_UNDEF; |
| 513 | } else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) { |
| 514 | DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 515 | ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_soname()); |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 516 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 517 | |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 518 | return false; |
| 519 | } |
| 520 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 521 | static const ElfW(Versym) kVersymHiddenBit = 0x8000; |
| 522 | |
| 523 | static inline bool is_versym_hidden(const ElfW(Versym)* versym) { |
| 524 | // the symbol is hidden if bit 15 of versym is set. |
| 525 | return versym != nullptr && (*versym & kVersymHiddenBit) != 0; |
| 526 | } |
| 527 | |
| 528 | static inline bool check_symbol_version(const ElfW(Versym) verneed, |
| 529 | const ElfW(Versym)* verdef) { |
| 530 | return verneed == kVersymNotNeeded || |
| 531 | verdef == nullptr || |
| 532 | verneed == (*verdef & ~kVersymHiddenBit); |
| 533 | } |
| 534 | |
| 535 | bool soinfo::gnu_lookup(SymbolName& symbol_name, |
| 536 | const version_info* vi, |
| 537 | uint32_t* symbol_index) const { |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 538 | uint32_t hash = symbol_name.gnu_hash(); |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 539 | uint32_t h2 = hash >> gnu_shift2_; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 540 | |
| 541 | uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8; |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 542 | uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_; |
| 543 | ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num]; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 544 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 545 | *symbol_index = 0; |
| 546 | |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 547 | TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 548 | symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(base)); |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 549 | |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 550 | // test against bloom filter |
| 551 | 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] | 552 | TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 553 | symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(base)); |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 554 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 555 | return true; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 556 | } |
| 557 | |
| 558 | // bloom test says "probably yes"... |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 559 | uint32_t n = gnu_bucket_[hash % gnu_nbucket_]; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 560 | |
| 561 | if (n == 0) { |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 562 | TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 563 | symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(base)); |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 564 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 565 | return true; |
| 566 | } |
| 567 | |
| 568 | // lookup versym for the version definition in this library |
| 569 | // note the difference between "version is not requested" (vi == nullptr) |
| 570 | // and "version not found". In the first case verneed is kVersymNotNeeded |
| 571 | // which implies that the default version can be accepted; the second case results in |
| 572 | // verneed = 1 (kVersymGlobal) and implies that we should ignore versioned symbols |
| 573 | // for this library and consider only *global* ones. |
| 574 | ElfW(Versym) verneed = 0; |
| 575 | if (!find_verdef_version_index(vi, &verneed)) { |
| 576 | return false; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 577 | } |
| 578 | |
| 579 | do { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 580 | ElfW(Sym)* s = symtab_ + n; |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 581 | const ElfW(Versym)* verdef = get_versym(n); |
| 582 | // skip hidden versions when verneed == kVersymNotNeeded (0) |
| 583 | if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) { |
| 584 | continue; |
| 585 | } |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 586 | if (((gnu_chain_[n] ^ hash) >> 1) == 0 && |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 587 | check_symbol_version(verneed, verdef) && |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 588 | strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && |
| 589 | is_symbol_global_and_defined(this, s)) { |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 590 | TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 591 | symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(s->st_value), |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 592 | static_cast<size_t>(s->st_size)); |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 593 | *symbol_index = n; |
| 594 | return true; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 595 | } |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 596 | } while ((gnu_chain_[n++] & 1) == 0); |
| 597 | |
| 598 | TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 599 | symbol_name.get_name(), get_soname(), reinterpret_cast<void*>(base)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 600 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 601 | return true; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 602 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 603 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 604 | bool soinfo::elf_lookup(SymbolName& symbol_name, |
| 605 | const version_info* vi, |
| 606 | uint32_t* symbol_index) const { |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 607 | uint32_t hash = symbol_name.elf_hash(); |
| 608 | |
| 609 | TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 610 | symbol_name.get_name(), get_soname(), |
| 611 | reinterpret_cast<void*>(base), hash, hash % nbucket_); |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 612 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 613 | ElfW(Versym) verneed = 0; |
| 614 | if (!find_verdef_version_index(vi, &verneed)) { |
| 615 | return false; |
| 616 | } |
| 617 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 618 | for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) { |
| 619 | ElfW(Sym)* s = symtab_ + n; |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 620 | const ElfW(Versym)* verdef = get_versym(n); |
| 621 | |
| 622 | // skip hidden versions when verneed == 0 |
| 623 | if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) { |
| 624 | continue; |
| 625 | } |
| 626 | |
| 627 | if (check_symbol_version(verneed, verdef) && |
| 628 | strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 && |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 629 | is_symbol_global_and_defined(this, s)) { |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 630 | TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 631 | symbol_name.get_name(), get_soname(), |
| 632 | reinterpret_cast<void*>(s->st_value), |
| 633 | static_cast<size_t>(s->st_size)); |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 634 | *symbol_index = n; |
| 635 | return true; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 636 | } |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 637 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 638 | |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 639 | TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 640 | symbol_name.get_name(), get_soname(), |
| 641 | reinterpret_cast<void*>(base), hash, hash % nbucket_); |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 642 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 643 | *symbol_index = 0; |
| 644 | return true; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 645 | } |
| 646 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 647 | soinfo::soinfo(const char* realpath, const struct stat* file_stat, |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 648 | off64_t file_offset, int rtld_flags) { |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 649 | memset(this, 0, sizeof(*this)); |
| 650 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 651 | if (realpath != nullptr) { |
| 652 | realpath_ = realpath; |
| 653 | } |
| 654 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 655 | flags_ = FLAG_NEW_SOINFO; |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 656 | version_ = SOINFO_VERSION; |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 657 | |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 658 | if (file_stat != nullptr) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 659 | this->st_dev_ = file_stat->st_dev; |
| 660 | this->st_ino_ = file_stat->st_ino; |
| 661 | this->file_offset_ = file_offset; |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 662 | } |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 663 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 664 | this->rtld_flags_ = rtld_flags; |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 665 | } |
| 666 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 667 | |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 668 | uint32_t SymbolName::elf_hash() { |
| 669 | if (!has_elf_hash_) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 670 | const uint8_t* name = reinterpret_cast<const uint8_t*>(name_); |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 671 | uint32_t h = 0, g; |
| 672 | |
| 673 | while (*name) { |
| 674 | h = (h << 4) + *name++; |
| 675 | g = h & 0xf0000000; |
| 676 | h ^= g; |
| 677 | h ^= g >> 24; |
| 678 | } |
| 679 | |
| 680 | elf_hash_ = h; |
| 681 | has_elf_hash_ = true; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 682 | } |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 683 | |
| 684 | return elf_hash_; |
| 685 | } |
| 686 | |
| 687 | uint32_t SymbolName::gnu_hash() { |
| 688 | if (!has_gnu_hash_) { |
| 689 | uint32_t h = 5381; |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 690 | const uint8_t* name = reinterpret_cast<const uint8_t*>(name_); |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 691 | while (*name != 0) { |
| 692 | h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c |
| 693 | } |
| 694 | |
| 695 | gnu_hash_ = h; |
| 696 | has_gnu_hash_ = true; |
| 697 | } |
| 698 | |
| 699 | return gnu_hash_; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 700 | } |
| 701 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 702 | bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi, |
| 703 | soinfo** si_found_in, const soinfo::soinfo_list_t& global_group, |
| 704 | const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol) { |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 705 | SymbolName symbol_name(name); |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 706 | const ElfW(Sym)* s = nullptr; |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 707 | |
Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 708 | /* "This element's presence in a shared object library alters the dynamic linker's |
| 709 | * symbol resolution algorithm for references within the library. Instead of starting |
| 710 | * a symbol search with the executable file, the dynamic linker starts from the shared |
| 711 | * object itself. If the shared object fails to supply the referenced symbol, the |
| 712 | * dynamic linker then searches the executable file and other shared objects as usual." |
| 713 | * |
| 714 | * http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html |
| 715 | * |
| 716 | * Note that this is unlikely since static linker avoids generating |
| 717 | * relocations for -Bsymbolic linked dynamic executables. |
| 718 | */ |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 719 | if (si_from->has_DT_SYMBOLIC) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 720 | DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_soname(), name); |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 721 | if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) { |
| 722 | return false; |
| 723 | } |
| 724 | |
Dmitriy Ivanov | 8f61d99 | 2014-09-16 14:31:06 -0700 | [diff] [blame] | 725 | if (s != nullptr) { |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 726 | *si_found_in = si_from; |
Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 727 | } |
| 728 | } |
| 729 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 730 | // 1. Look for it in global_group |
| 731 | if (s == nullptr) { |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 732 | bool error = false; |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 733 | global_group.visit([&](soinfo* global_si) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 734 | DEBUG("%s: looking up %s in %s (from global group)", |
| 735 | si_from->get_soname(), name, global_si->get_soname()); |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 736 | if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) { |
| 737 | error = true; |
| 738 | return false; |
| 739 | } |
| 740 | |
Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 741 | if (s != nullptr) { |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 742 | *si_found_in = global_si; |
| 743 | return false; |
Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 744 | } |
Dmitriy Ivanov | c204894 | 2014-08-29 10:15:25 -0700 | [diff] [blame] | 745 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 746 | return true; |
| 747 | }); |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 748 | |
| 749 | if (error) { |
| 750 | return false; |
| 751 | } |
Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 752 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 753 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 754 | // 2. Look for it in the local group |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 755 | if (s == nullptr) { |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 756 | bool error = false; |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 757 | local_group.visit([&](soinfo* local_si) { |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 758 | if (local_si == si_from && si_from->has_DT_SYMBOLIC) { |
Dmitriy Ivanov | e47b3f8 | 2014-10-23 14:19:07 -0700 | [diff] [blame] | 759 | // we already did this - skip |
| 760 | return true; |
| 761 | } |
| 762 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 763 | DEBUG("%s: looking up %s in %s (from local group)", |
| 764 | si_from->get_soname(), name, local_si->get_soname()); |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 765 | if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) { |
| 766 | error = true; |
| 767 | return false; |
| 768 | } |
| 769 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 770 | if (s != nullptr) { |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 771 | *si_found_in = local_si; |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 772 | return false; |
| 773 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 774 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 775 | return true; |
| 776 | }); |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 777 | |
| 778 | if (error) { |
| 779 | return false; |
| 780 | } |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 781 | } |
| 782 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 783 | if (s != nullptr) { |
| 784 | TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, " |
| 785 | "found in %s, base = %p, load bias = %p", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 786 | si_from->get_soname(), name, reinterpret_cast<void*>(s->st_value), |
| 787 | (*si_found_in)->get_soname(), reinterpret_cast<void*>((*si_found_in)->base), |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 788 | reinterpret_cast<void*>((*si_found_in)->load_bias)); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 789 | } |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 790 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 791 | *symbol = s; |
| 792 | return true; |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 793 | } |
| 794 | |
Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 795 | class ProtectedDataGuard { |
| 796 | public: |
| 797 | ProtectedDataGuard() { |
| 798 | if (ref_count_++ == 0) { |
| 799 | protect_data(PROT_READ | PROT_WRITE); |
| 800 | } |
| 801 | } |
| 802 | |
| 803 | ~ProtectedDataGuard() { |
| 804 | if (ref_count_ == 0) { // overflow |
| 805 | __libc_fatal("Too many nested calls to dlopen()"); |
| 806 | } |
| 807 | |
| 808 | if (--ref_count_ == 0) { |
| 809 | protect_data(PROT_READ); |
| 810 | } |
| 811 | } |
| 812 | private: |
| 813 | void protect_data(int protection) { |
| 814 | g_soinfo_allocator.protect_all(protection); |
| 815 | g_soinfo_links_allocator.protect_all(protection); |
| 816 | } |
| 817 | |
| 818 | static size_t ref_count_; |
| 819 | }; |
| 820 | |
| 821 | size_t ProtectedDataGuard::ref_count_ = 0; |
| 822 | |
Dmitriy Ivanov | 0cd83eb | 2014-09-01 16:15:52 -0700 | [diff] [blame] | 823 | // Each size has it's own allocator. |
| 824 | template<size_t size> |
| 825 | class SizeBasedAllocator { |
| 826 | public: |
| 827 | static void* alloc() { |
| 828 | return allocator_.alloc(); |
| 829 | } |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 830 | |
Dmitriy Ivanov | 0cd83eb | 2014-09-01 16:15:52 -0700 | [diff] [blame] | 831 | static void free(void* ptr) { |
| 832 | allocator_.free(ptr); |
| 833 | } |
Dmitriy Ivanov | 4bea498 | 2014-08-29 14:01:48 -0700 | [diff] [blame] | 834 | |
Dmitriy Ivanov | 0cd83eb | 2014-09-01 16:15:52 -0700 | [diff] [blame] | 835 | private: |
| 836 | static LinkerBlockAllocator allocator_; |
| 837 | }; |
| 838 | |
| 839 | template<size_t size> |
| 840 | LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size); |
| 841 | |
| 842 | template<typename T> |
| 843 | class TypeBasedAllocator { |
| 844 | public: |
| 845 | static T* alloc() { |
| 846 | return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc()); |
| 847 | } |
| 848 | |
| 849 | static void free(T* ptr) { |
| 850 | SizeBasedAllocator<sizeof(T)>::free(ptr); |
| 851 | } |
| 852 | }; |
| 853 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 854 | class LoadTask { |
| 855 | public: |
| 856 | struct deleter_t { |
| 857 | void operator()(LoadTask* t) { |
| 858 | TypeBasedAllocator<LoadTask>::free(t); |
| 859 | } |
| 860 | }; |
| 861 | |
| 862 | typedef UniquePtr<LoadTask, deleter_t> unique_ptr; |
| 863 | |
| 864 | static deleter_t deleter; |
| 865 | |
| 866 | static LoadTask* create(const char* name, soinfo* needed_by) { |
| 867 | LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc(); |
| 868 | return new (ptr) LoadTask(name, needed_by); |
| 869 | } |
| 870 | |
| 871 | const char* get_name() const { |
| 872 | return name_; |
| 873 | } |
| 874 | |
| 875 | soinfo* get_needed_by() const { |
| 876 | return needed_by_; |
| 877 | } |
| 878 | private: |
| 879 | LoadTask(const char* name, soinfo* needed_by) |
| 880 | : name_(name), needed_by_(needed_by) {} |
| 881 | |
| 882 | const char* name_; |
| 883 | soinfo* needed_by_; |
| 884 | |
| 885 | DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask); |
| 886 | }; |
| 887 | |
Ningsheng Jian | e93be99 | 2014-09-16 15:22:10 +0800 | [diff] [blame] | 888 | LoadTask::deleter_t LoadTask::deleter; |
| 889 | |
Dmitriy Ivanov | 0cd83eb | 2014-09-01 16:15:52 -0700 | [diff] [blame] | 890 | template <typename T> |
| 891 | using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>; |
| 892 | |
| 893 | typedef linked_list_t<soinfo> SoinfoLinkedList; |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 894 | typedef linked_list_t<const char> StringLinkedList; |
| 895 | typedef linked_list_t<LoadTask> LoadTaskList; |
Dmitriy Ivanov | 0cd83eb | 2014-09-01 16:15:52 -0700 | [diff] [blame] | 896 | |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 897 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 898 | // This function walks down the tree of soinfo dependencies |
| 899 | // in breadth-first order and |
| 900 | // * calls action(soinfo* si) for each node, and |
| 901 | // * terminates walk if action returns false. |
| 902 | // |
| 903 | // walk_dependencies_tree returns false if walk was terminated |
| 904 | // by the action and true otherwise. |
| 905 | template<typename F> |
| 906 | 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] | 907 | SoinfoLinkedList visit_list; |
| 908 | SoinfoLinkedList visited; |
| 909 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 910 | for (size_t i = 0; i < root_soinfos_size; ++i) { |
| 911 | visit_list.push_back(root_soinfos[i]); |
| 912 | } |
| 913 | |
| 914 | soinfo* si; |
| 915 | while ((si = visit_list.pop_front()) != nullptr) { |
| 916 | if (visited.contains(si)) { |
Dmitriy Ivanov | 042426b | 2014-08-12 21:02:13 -0700 | [diff] [blame] | 917 | continue; |
| 918 | } |
| 919 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 920 | if (!action(si)) { |
| 921 | return false; |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 922 | } |
| 923 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 924 | visited.push_back(si); |
| 925 | |
| 926 | si->get_children().for_each([&](soinfo* child) { |
Dmitriy Ivanov | aa0f2bd | 2014-07-28 17:32:20 -0700 | [diff] [blame] | 927 | visit_list.push_back(child); |
| 928 | }); |
| 929 | } |
| 930 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 931 | return true; |
| 932 | } |
| 933 | |
| 934 | |
| 935 | // This is used by dlsym(3). It performs symbol lookup only within the |
| 936 | // specified soinfo object and its dependencies in breadth first order. |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 937 | const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) { |
| 938 | const ElfW(Sym)* result = nullptr; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 939 | SymbolName symbol_name(name); |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 940 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 941 | walk_dependencies_tree(&si, 1, [&](soinfo* current_soinfo) { |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 942 | if (!current_soinfo->find_symbol_by_name(symbol_name, nullptr, &result)) { |
| 943 | result = nullptr; |
| 944 | return false; |
| 945 | } |
| 946 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 947 | if (result != nullptr) { |
| 948 | *found = current_soinfo; |
| 949 | return false; |
| 950 | } |
| 951 | |
| 952 | return true; |
| 953 | }); |
| 954 | |
| 955 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 956 | } |
| 957 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 958 | /* This is used by dlsym(3) to performs a global symbol lookup. If the |
| 959 | start value is null (for RTLD_DEFAULT), the search starts at the |
| 960 | beginning of the global solist. Otherwise the search starts at the |
| 961 | specified soinfo (for RTLD_NEXT). |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 962 | */ |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 963 | const ElfW(Sym)* dlsym_linear_lookup(const char* name, |
| 964 | soinfo** found, |
| 965 | soinfo* caller, |
| 966 | void* handle) { |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 967 | SymbolName symbol_name(name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 968 | |
Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 969 | soinfo* start = solist; |
| 970 | |
| 971 | if (handle == RTLD_NEXT) { |
| 972 | if (caller == nullptr || caller->next == nullptr) { |
| 973 | return nullptr; |
| 974 | } else { |
| 975 | start = caller->next; |
| 976 | } |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 977 | } |
| 978 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 979 | const ElfW(Sym)* s = nullptr; |
Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 980 | for (soinfo* si = start; si != nullptr; si = si->next) { |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 981 | if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0) { |
| 982 | continue; |
| 983 | } |
| 984 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 985 | if (!si->find_symbol_by_name(symbol_name, nullptr, &s)) { |
| 986 | return nullptr; |
| 987 | } |
| 988 | |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 989 | if (s != nullptr) { |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 990 | *found = si; |
| 991 | break; |
Matt Fischer | 1698d9e | 2009-12-31 12:17:56 -0600 | [diff] [blame] | 992 | } |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 993 | } |
Matt Fischer | 1698d9e | 2009-12-31 12:17:56 -0600 | [diff] [blame] | 994 | |
Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 995 | // If not found - look into local_group unless |
| 996 | // caller is part of the global group in which |
| 997 | // case we already did it. |
| 998 | if (s == nullptr && caller != nullptr && |
| 999 | (caller->get_rtld_flags() & RTLD_GLOBAL) == 0) { |
| 1000 | soinfo* local_group_root = caller->get_local_group_root(); |
| 1001 | |
| 1002 | if (handle == RTLD_DEFAULT) { |
| 1003 | start = local_group_root; |
| 1004 | } |
| 1005 | |
| 1006 | for (soinfo* si = start; si != nullptr; si = si->next) { |
| 1007 | if (si->get_local_group_root() != local_group_root) { |
| 1008 | break; |
| 1009 | } |
| 1010 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1011 | if (!si->find_symbol_by_name(symbol_name, nullptr, &s)) { |
| 1012 | return nullptr; |
| 1013 | } |
| 1014 | |
Dmitriy Ivanov | 76ac1ac | 2015-04-01 14:45:10 -0700 | [diff] [blame] | 1015 | if (s != nullptr) { |
| 1016 | *found = si; |
| 1017 | break; |
| 1018 | } |
| 1019 | } |
| 1020 | } |
| 1021 | |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1022 | if (s != nullptr) { |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1023 | TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p", |
| 1024 | name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base)); |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 1025 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1026 | |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 1027 | return s; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1028 | } |
| 1029 | |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1030 | soinfo* find_containing_library(const void* p) { |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 1031 | ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p); |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1032 | for (soinfo* si = solist; si != nullptr; si = si->next) { |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1033 | if (address >= si->base && address - si->base < si->size) { |
| 1034 | return si; |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 1035 | } |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1036 | } |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1037 | return nullptr; |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 1038 | } |
| 1039 | |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1040 | ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) { |
| 1041 | return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr); |
| 1042 | } |
| 1043 | |
| 1044 | static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) { |
| 1045 | return sym->st_shndx != SHN_UNDEF && |
| 1046 | soaddr >= sym->st_value && |
| 1047 | soaddr < sym->st_value + sym->st_size; |
| 1048 | } |
| 1049 | |
| 1050 | ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) { |
Chris Dearman | 8e55381 | 2013-11-13 17:22:33 -0800 | [diff] [blame] | 1051 | ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1052 | |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 1053 | for (size_t i = 0; i < gnu_nbucket_; ++i) { |
| 1054 | uint32_t n = gnu_bucket_[i]; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1055 | |
| 1056 | if (n == 0) { |
| 1057 | continue; |
| 1058 | } |
| 1059 | |
| 1060 | do { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 1061 | ElfW(Sym)* sym = symtab_ + n; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1062 | if (symbol_matches_soaddr(sym, soaddr)) { |
| 1063 | return sym; |
| 1064 | } |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 1065 | } while ((gnu_chain_[n++] & 1) == 0); |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1066 | } |
| 1067 | |
| 1068 | return nullptr; |
| 1069 | } |
| 1070 | |
| 1071 | ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) { |
Chris Dearman | 8e55381 | 2013-11-13 17:22:33 -0800 | [diff] [blame] | 1072 | ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias; |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 1073 | |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1074 | // Search the library's symbol table for any defined symbol which |
| 1075 | // contains this address. |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 1076 | for (size_t i = 0; i < nchain_; ++i) { |
| 1077 | ElfW(Sym)* sym = symtab_ + i; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 1078 | if (symbol_matches_soaddr(sym, soaddr)) { |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1079 | return sym; |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 1080 | } |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1081 | } |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 1082 | |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1083 | return nullptr; |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 1084 | } |
| 1085 | |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1086 | static int open_library_in_zipfile(const char* const path, |
| 1087 | off64_t* file_offset) { |
| 1088 | TRACE("Trying zip file open from path '%s'", path); |
| 1089 | |
| 1090 | // Treat an '!' character inside a path as the separator between the name |
| 1091 | // of the zip file on disk and the subdirectory to search within it. |
| 1092 | // For example, if path is "foo.zip!bar/bas/x.so", then we search for |
| 1093 | // "bar/bas/x.so" within "foo.zip". |
| 1094 | const char* separator = strchr(path, '!'); |
| 1095 | if (separator == nullptr) { |
| 1096 | return -1; |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1097 | } |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1098 | |
| 1099 | char buf[512]; |
| 1100 | if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) { |
| 1101 | PRINT("Warning: ignoring very long library path: %s", path); |
| 1102 | return -1; |
| 1103 | } |
| 1104 | |
| 1105 | buf[separator - path] = '\0'; |
| 1106 | |
| 1107 | const char* zip_path = buf; |
| 1108 | const char* file_path = &buf[separator - path + 1]; |
| 1109 | int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC)); |
| 1110 | if (fd == -1) { |
| 1111 | return -1; |
| 1112 | } |
| 1113 | |
| 1114 | ZipArchiveHandle handle; |
| 1115 | if (OpenArchiveFd(fd, "", &handle, false) != 0) { |
| 1116 | // invalid zip-file (?) |
| 1117 | close(fd); |
| 1118 | return -1; |
| 1119 | } |
| 1120 | |
| 1121 | auto archive_guard = make_scope_guard([&]() { |
| 1122 | CloseArchive(handle); |
| 1123 | }); |
| 1124 | |
| 1125 | ZipEntry entry; |
| 1126 | |
| 1127 | if (FindEntry(handle, ZipEntryName(file_path), &entry) != 0) { |
| 1128 | // Entry was not found. |
| 1129 | close(fd); |
| 1130 | return -1; |
| 1131 | } |
| 1132 | |
| 1133 | // Check if it is properly stored |
| 1134 | if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) { |
| 1135 | close(fd); |
| 1136 | return -1; |
| 1137 | } |
| 1138 | |
| 1139 | *file_offset = entry.offset; |
| 1140 | return fd; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1141 | } |
| 1142 | |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1143 | static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) { |
| 1144 | int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name); |
| 1145 | if (n < 0 || n >= static_cast<int>(buf_size)) { |
| 1146 | PRINT("Warning: ignoring very long library path: %s/%s", path, name); |
| 1147 | return false; |
| 1148 | } |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1149 | |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1150 | return true; |
| 1151 | } |
| 1152 | |
| 1153 | static int open_library_on_default_path(const char* name, off64_t* file_offset) { |
| 1154 | for (size_t i = 0; kDefaultLdPaths[i] != nullptr; ++i) { |
| 1155 | char buf[512]; |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1156 | if (!format_path(buf, sizeof(buf), kDefaultLdPaths[i], name)) { |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1157 | continue; |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1158 | } |
| 1159 | |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1160 | int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC)); |
| 1161 | if (fd != -1) { |
| 1162 | *file_offset = 0; |
| 1163 | return fd; |
| 1164 | } |
| 1165 | } |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1166 | |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1167 | return -1; |
| 1168 | } |
| 1169 | |
| 1170 | static int open_library_on_ld_library_path(const char* name, off64_t* file_offset) { |
| 1171 | for (const auto& path_str : g_ld_library_paths) { |
| 1172 | char buf[512]; |
| 1173 | const char* const path = path_str.c_str(); |
| 1174 | if (!format_path(buf, sizeof(buf), path, name)) { |
| 1175 | continue; |
| 1176 | } |
| 1177 | |
| 1178 | int fd = -1; |
| 1179 | if (strchr(buf, '!') != nullptr) { |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1180 | fd = open_library_in_zipfile(buf, file_offset); |
| 1181 | } |
| 1182 | |
| 1183 | if (fd == -1) { |
| 1184 | fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC)); |
| 1185 | if (fd != -1) { |
| 1186 | *file_offset = 0; |
| 1187 | } |
| 1188 | } |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1189 | |
| 1190 | if (fd != -1) { |
| 1191 | return fd; |
| 1192 | } |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1193 | } |
| 1194 | |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1195 | return -1; |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1196 | } |
| 1197 | |
| 1198 | static int open_library(const char* name, off64_t* file_offset) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1199 | TRACE("[ opening %s ]", name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1200 | |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1201 | // 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] | 1202 | if (strchr(name, '/') != nullptr) { |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1203 | if (strchr(name, '!') != nullptr) { |
| 1204 | int fd = open_library_in_zipfile(name, file_offset); |
| 1205 | if (fd != -1) { |
| 1206 | return fd; |
| 1207 | } |
| 1208 | } |
| 1209 | |
Elliott Hughes | 6971fe4 | 2012-11-01 22:59:19 -0700 | [diff] [blame] | 1210 | int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC)); |
| 1211 | if (fd != -1) { |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1212 | *file_offset = 0; |
Elliott Hughes | 6971fe4 | 2012-11-01 22:59:19 -0700 | [diff] [blame] | 1213 | } |
Dmitriy Ivanov | e44fffd | 2015-03-17 17:12:18 -0700 | [diff] [blame] | 1214 | return fd; |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1215 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1216 | |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1217 | // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths. |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1218 | int fd = open_library_on_ld_library_path(name, file_offset); |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1219 | if (fd == -1) { |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1220 | fd = open_library_on_default_path(name, file_offset); |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1221 | } |
| 1222 | return fd; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1223 | } |
| 1224 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1225 | template<typename F> |
| 1226 | static void for_each_dt_needed(const soinfo* si, F action) { |
| 1227 | for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) { |
| 1228 | if (d->d_tag == DT_NEEDED) { |
Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 1229 | action(si->get_string(d->d_un.d_val)); |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1230 | } |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1231 | } |
| 1232 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1233 | |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1234 | static soinfo* load_library(LoadTaskList& load_tasks, |
| 1235 | const char* name, int rtld_flags, |
| 1236 | const android_dlextinfo* extinfo) { |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1237 | int fd = -1; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 1238 | off64_t file_offset = 0; |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1239 | ScopedFd file_guard(-1); |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1240 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1241 | if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) { |
| 1242 | fd = extinfo->library_fd; |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 1243 | if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) { |
| 1244 | file_offset = extinfo->library_fd_offset; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 1245 | } |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1246 | } else { |
| 1247 | // Open the file. |
Simon Baldwin | aef7195 | 2015-01-16 13:22:54 +0000 | [diff] [blame] | 1248 | fd = open_library(name, &file_offset); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1249 | if (fd == -1) { |
| 1250 | DL_ERR("library \"%s\" not found", name); |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1251 | return nullptr; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1252 | } |
| 1253 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1254 | file_guard.reset(fd); |
| 1255 | } |
| 1256 | |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 1257 | if ((file_offset % PAGE_SIZE) != 0) { |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 1258 | DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 1259 | return nullptr; |
| 1260 | } |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 1261 | if (file_offset < 0) { |
| 1262 | DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset); |
| 1263 | return nullptr; |
| 1264 | } |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 1265 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1266 | struct stat file_stat; |
| 1267 | if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) { |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 1268 | DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno)); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1269 | return nullptr; |
| 1270 | } |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 1271 | if (file_offset >= file_stat.st_size) { |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 1272 | DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64, |
| 1273 | name, file_offset, file_stat.st_size); |
Yabin Cui | 16f7f8d | 2014-11-04 11:08:05 -0800 | [diff] [blame] | 1274 | return nullptr; |
| 1275 | } |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1276 | |
| 1277 | // Check for symlink and other situations where |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 1278 | // file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set |
| 1279 | if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) { |
| 1280 | for (soinfo* si = solist; si != nullptr; si = si->next) { |
| 1281 | if (si->get_st_dev() != 0 && |
| 1282 | si->get_st_ino() != 0 && |
| 1283 | si->get_st_dev() == file_stat.st_dev && |
| 1284 | si->get_st_ino() == file_stat.st_ino && |
| 1285 | si->get_file_offset() == file_offset) { |
| 1286 | TRACE("library \"%s\" is already loaded under different name/path \"%s\" - " |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1287 | "will return existing soinfo", name, si->get_realpath()); |
Dmitriy Ivanov | 9b82136 | 2015-04-02 16:03:56 -0700 | [diff] [blame] | 1288 | return si; |
| 1289 | } |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1290 | } |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1291 | } |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1292 | |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 1293 | if ((rtld_flags & RTLD_NOLOAD) != 0) { |
Dmitriy Ivanov | a6ac54a | 2014-09-09 10:21:42 -0700 | [diff] [blame] | 1294 | DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1295 | return nullptr; |
| 1296 | } |
Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 1297 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1298 | std::string realpath = name; |
| 1299 | if (!realpath_fd(fd, &realpath)) { |
| 1300 | PRINT("cannot resolve realpath for the library \"%s\": %s", name, strerror(errno)); |
| 1301 | realpath = name; |
| 1302 | } |
| 1303 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1304 | // Read the ELF header and load the segments. |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1305 | ElfReader elf_reader(realpath.c_str(), fd, file_offset); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1306 | if (!elf_reader.Load(extinfo)) { |
| 1307 | return nullptr; |
| 1308 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1309 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1310 | soinfo* si = soinfo_alloc(realpath.c_str(), &file_stat, file_offset, rtld_flags); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1311 | if (si == nullptr) { |
| 1312 | return nullptr; |
| 1313 | } |
| 1314 | si->base = elf_reader.load_start(); |
| 1315 | si->size = elf_reader.load_size(); |
| 1316 | si->load_bias = elf_reader.load_bias(); |
| 1317 | si->phnum = elf_reader.phdr_count(); |
| 1318 | si->phdr = elf_reader.loaded_phdr(); |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1319 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 1320 | if (!si->prelink_image()) { |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1321 | soinfo_free(si); |
| 1322 | return nullptr; |
| 1323 | } |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1324 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1325 | for_each_dt_needed(si, [&] (const char* name) { |
| 1326 | load_tasks.push_back(LoadTask::create(name, si)); |
| 1327 | }); |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1328 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1329 | return si; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1330 | } |
| 1331 | |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 1332 | static soinfo *find_loaded_library_by_soname(const char* name) { |
| 1333 | // Ignore filename with path. |
| 1334 | if (strchr(name, '/') != nullptr) { |
| 1335 | return nullptr; |
| 1336 | } |
| 1337 | |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1338 | for (soinfo* si = solist; si != nullptr; si = si->next) { |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 1339 | const char* soname = si->get_soname(); |
| 1340 | if (soname != nullptr && (strcmp(name, soname) == 0)) { |
Dmitriy Ivanov | 489e498 | 2014-05-19 15:19:52 -0700 | [diff] [blame] | 1341 | return si; |
Ard Biesheuvel | 12c78bb | 2012-08-14 12:30:09 +0200 | [diff] [blame] | 1342 | } |
Dmitriy Ivanov | 489e498 | 2014-05-19 15:19:52 -0700 | [diff] [blame] | 1343 | } |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1344 | return nullptr; |
Ard Biesheuvel | 12c78bb | 2012-08-14 12:30:09 +0200 | [diff] [blame] | 1345 | } |
| 1346 | |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 1347 | static soinfo* find_library_internal(LoadTaskList& load_tasks, const char* name, |
| 1348 | int rtld_flags, const android_dlextinfo* extinfo) { |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 1349 | soinfo* si = find_loaded_library_by_soname(name); |
Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 1350 | |
| 1351 | // Library might still be loaded, the accurate detection |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1352 | // of this fact is done by load_library. |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1353 | if (si == nullptr) { |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 1354 | TRACE("[ '%s' has not been found by soname. Trying harder...]", name); |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 1355 | si = load_library(load_tasks, name, rtld_flags, extinfo); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1356 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1357 | |
Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 1358 | return si; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1359 | } |
| 1360 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1361 | static void soinfo_unload(soinfo* si); |
| 1362 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 1363 | // TODO: this is slightly unusual way to construct |
| 1364 | // the global group for relocation. Not every RTLD_GLOBAL |
| 1365 | // library is included in this group for backwards-compatibility |
| 1366 | // reasons. |
| 1367 | // |
| 1368 | // This group consists of the main executable, LD_PRELOADs |
| 1369 | // and libraries with the DF_1_GLOBAL flag set. |
| 1370 | static soinfo::soinfo_list_t make_global_group() { |
| 1371 | soinfo::soinfo_list_t global_group; |
| 1372 | for (soinfo* si = somain; si != nullptr; si = si->next) { |
| 1373 | if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) { |
| 1374 | global_group.push_back(si); |
| 1375 | } |
| 1376 | } |
| 1377 | |
| 1378 | return global_group; |
| 1379 | } |
| 1380 | |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1381 | static bool find_libraries(soinfo* start_with, const char* const library_names[], |
| 1382 | size_t library_names_count, soinfo* soinfos[], std::vector<soinfo*>* ld_preloads, |
| 1383 | size_t ld_preloads_count, int rtld_flags, const android_dlextinfo* extinfo) { |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1384 | // Step 0: prepare. |
| 1385 | LoadTaskList load_tasks; |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1386 | for (size_t i = 0; i < library_names_count; ++i) { |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1387 | const char* name = library_names[i]; |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1388 | load_tasks.push_back(LoadTask::create(name, start_with)); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1389 | } |
| 1390 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 1391 | // Construct global_group. |
| 1392 | soinfo::soinfo_list_t global_group = make_global_group(); |
| 1393 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1394 | // If soinfos array is null allocate one on stack. |
| 1395 | // The array is needed in case of failure; for example |
| 1396 | // when library_names[] = {libone.so, libtwo.so} and libone.so |
| 1397 | // is loaded correctly but libtwo.so failed for some reason. |
| 1398 | // In this case libone.so should be unloaded on return. |
| 1399 | // See also implementation of failure_guard below. |
| 1400 | |
| 1401 | if (soinfos == nullptr) { |
| 1402 | size_t soinfos_size = sizeof(soinfo*)*library_names_count; |
| 1403 | soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size)); |
| 1404 | memset(soinfos, 0, soinfos_size); |
| 1405 | } |
| 1406 | |
| 1407 | // list of libraries to link - see step 2. |
| 1408 | size_t soinfos_count = 0; |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1409 | |
Dmitriy Ivanov | d9ff722 | 2014-09-08 16:22:22 -0700 | [diff] [blame] | 1410 | auto failure_guard = make_scope_guard([&]() { |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1411 | // Housekeeping |
| 1412 | load_tasks.for_each([] (LoadTask* t) { |
| 1413 | LoadTask::deleter(t); |
| 1414 | }); |
| 1415 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1416 | for (size_t i = 0; i<soinfos_count; ++i) { |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1417 | soinfo_unload(soinfos[i]); |
| 1418 | } |
| 1419 | }); |
| 1420 | |
| 1421 | // Step 1: load and pre-link all DT_NEEDED libraries in breadth first order. |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 1422 | for (LoadTask::unique_ptr task(load_tasks.pop_front()); |
| 1423 | task.get() != nullptr; task.reset(load_tasks.pop_front())) { |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 1424 | soinfo* si = find_library_internal(load_tasks, task->get_name(), rtld_flags, extinfo); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1425 | if (si == nullptr) { |
| 1426 | return false; |
| 1427 | } |
| 1428 | |
| 1429 | soinfo* needed_by = task->get_needed_by(); |
| 1430 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1431 | if (needed_by != nullptr) { |
| 1432 | needed_by->add_child(si); |
| 1433 | } |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1434 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1435 | if (si->is_linked()) { |
| 1436 | si->increment_ref_count(); |
| 1437 | } |
| 1438 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1439 | // When ld_preloads is not null, the first |
| 1440 | // ld_preloads_count libs are in fact ld_preloads. |
| 1441 | if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) { |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 1442 | // Add LD_PRELOADed libraries to the global group for future runs. |
| 1443 | // There is no need to explicitly add them to the global group |
| 1444 | // for this run because they are going to appear in the local |
| 1445 | // group in the correct order. |
| 1446 | si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL); |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 1447 | ld_preloads->push_back(si); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1448 | } |
| 1449 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1450 | if (soinfos_count < library_names_count) { |
| 1451 | soinfos[soinfos_count++] = si; |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1452 | } |
| 1453 | } |
| 1454 | |
| 1455 | // Step 2: link libraries. |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1456 | soinfo::soinfo_list_t local_group; |
| 1457 | walk_dependencies_tree( |
| 1458 | start_with == nullptr ? soinfos : &start_with, |
| 1459 | start_with == nullptr ? soinfos_count : 1, |
| 1460 | [&] (soinfo* si) { |
| 1461 | local_group.push_back(si); |
| 1462 | return true; |
| 1463 | }); |
| 1464 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1465 | // We need to increment ref_count in case |
| 1466 | // the root of the local group was not linked. |
| 1467 | bool was_local_group_root_linked = local_group.front()->is_linked(); |
| 1468 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1469 | bool linked = local_group.visit([&](soinfo* si) { |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1470 | if (!si->is_linked()) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 1471 | if (!si->link_image(global_group, local_group, extinfo)) { |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1472 | return false; |
| 1473 | } |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1474 | si->set_linked(); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1475 | } |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1476 | |
| 1477 | return true; |
| 1478 | }); |
| 1479 | |
| 1480 | if (linked) { |
| 1481 | failure_guard.disable(); |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1482 | } |
| 1483 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1484 | if (!was_local_group_root_linked) { |
| 1485 | local_group.front()->increment_ref_count(); |
| 1486 | } |
| 1487 | |
Dmitriy Ivanov | cfa97f1 | 2014-10-21 09:23:18 -0700 | [diff] [blame] | 1488 | return linked; |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1489 | } |
| 1490 | |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 1491 | static soinfo* find_library(const char* name, int rtld_flags, const android_dlextinfo* extinfo) { |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1492 | soinfo* si; |
| 1493 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1494 | if (name == nullptr) { |
| 1495 | si = somain; |
| 1496 | } else if (!find_libraries(nullptr, &name, 1, &si, nullptr, 0, rtld_flags, extinfo)) { |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1497 | return nullptr; |
| 1498 | } |
| 1499 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1500 | return si; |
| 1501 | } |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 1502 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1503 | static void soinfo_unload(soinfo* root) { |
| 1504 | // Note that the library can be loaded but not linked; |
| 1505 | // in which case there is no root but we still need |
| 1506 | // to walk the tree and unload soinfos involved. |
| 1507 | // |
| 1508 | // This happens on unsuccessful dlopen, when one of |
| 1509 | // the DT_NEEDED libraries could not be linked/found. |
| 1510 | if (root->is_linked()) { |
| 1511 | root = root->get_local_group_root(); |
| 1512 | } |
| 1513 | |
| 1514 | if (!root->can_unload()) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1515 | TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_soname()); |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 1516 | return; |
| 1517 | } |
| 1518 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1519 | 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] | 1520 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1521 | if (ref_count == 0) { |
| 1522 | soinfo::soinfo_list_t local_unload_list; |
| 1523 | soinfo::soinfo_list_t external_unload_list; |
| 1524 | soinfo::soinfo_list_t depth_first_list; |
| 1525 | depth_first_list.push_back(root); |
| 1526 | soinfo* si = nullptr; |
| 1527 | |
| 1528 | while ((si = depth_first_list.pop_front()) != nullptr) { |
Dmitriy Ivanov | 5ae82cb | 2014-12-02 17:08:42 -0800 | [diff] [blame] | 1529 | if (local_unload_list.contains(si)) { |
| 1530 | continue; |
| 1531 | } |
| 1532 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1533 | local_unload_list.push_back(si); |
Dmitriy Ivanov | 5ae82cb | 2014-12-02 17:08:42 -0800 | [diff] [blame] | 1534 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1535 | if (si->has_min_version(0)) { |
| 1536 | soinfo* child = nullptr; |
| 1537 | while ((child = si->get_children().pop_front()) != nullptr) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1538 | TRACE("%s@%p needs to unload %s@%p", si->get_soname(), si, child->get_soname(), child); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1539 | if (local_unload_list.contains(child)) { |
| 1540 | continue; |
Dmitriy Ivanov | 5ae82cb | 2014-12-02 17:08:42 -0800 | [diff] [blame] | 1541 | } else if (child->is_linked() && child->get_local_group_root() != root) { |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1542 | external_unload_list.push_back(child); |
| 1543 | } else { |
| 1544 | depth_first_list.push_front(child); |
| 1545 | } |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 1546 | } |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1547 | } else { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1548 | #if !defined(__arm__) |
| 1549 | __libc_fatal("soinfo for \"%s\"@%p has no version", si->get_soname(), si); |
Dmitriy Ivanov | 5ae82cb | 2014-12-02 17:08:42 -0800 | [diff] [blame] | 1550 | #else |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1551 | PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_soname(), si); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1552 | for_each_dt_needed(si, [&] (const char* library_name) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1553 | TRACE("deprecated (old format of soinfo): %s needs to unload %s", |
| 1554 | si->get_soname(), library_name); |
| 1555 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1556 | soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr); |
| 1557 | if (needed != nullptr) { |
| 1558 | // Not found: for example if symlink was deleted between dlopen and dlclose |
| 1559 | // Since we cannot really handle errors at this point - print and continue. |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1560 | PRINT("warning: couldn't find %s needed by %s on unload.", |
| 1561 | library_name, si->get_soname()); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1562 | return; |
| 1563 | } else if (local_unload_list.contains(needed)) { |
| 1564 | // already visited |
| 1565 | return; |
Dmitriy Ivanov | 5ae82cb | 2014-12-02 17:08:42 -0800 | [diff] [blame] | 1566 | } else if (needed->is_linked() && needed->get_local_group_root() != root) { |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1567 | // external group |
| 1568 | external_unload_list.push_back(needed); |
| 1569 | } else { |
| 1570 | // local group |
| 1571 | depth_first_list.push_front(needed); |
| 1572 | } |
| 1573 | }); |
Dmitriy Ivanov | 5ae82cb | 2014-12-02 17:08:42 -0800 | [diff] [blame] | 1574 | #endif |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1575 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1576 | } |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1577 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 1578 | local_unload_list.for_each([](soinfo* si) { |
| 1579 | si->call_destructors(); |
| 1580 | }); |
| 1581 | |
| 1582 | while ((si = local_unload_list.pop_front()) != nullptr) { |
| 1583 | notify_gdb_of_unload(si); |
| 1584 | soinfo_free(si); |
| 1585 | } |
| 1586 | |
| 1587 | while ((si = external_unload_list.pop_front()) != nullptr) { |
| 1588 | soinfo_unload(si); |
| 1589 | } |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1590 | } else { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1591 | TRACE("not unloading '%s' group, decrementing ref_count to %zd", root->get_soname(), ref_count); |
Dmitriy Ivanov | a254705 | 2014-11-18 12:03:09 -0800 | [diff] [blame] | 1592 | } |
| 1593 | } |
| 1594 | |
Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 1595 | 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] | 1596 | // Use basic string manipulation calls to avoid snprintf. |
| 1597 | // snprintf indirectly calls pthread_getspecific to get the size of a buffer. |
| 1598 | // When debug malloc is enabled, this call returns 0. This in turn causes |
| 1599 | // snprintf to do nothing, which causes libraries to fail to load. |
| 1600 | // See b/17302493 for further details. |
| 1601 | // Once the above bug is fixed, this code can be modified to use |
| 1602 | // snprintf again. |
| 1603 | size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2; |
| 1604 | if (buffer_size < required_len) { |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 1605 | __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: " |
| 1606 | "buffer len %zu, required len %zu", buffer_size, required_len); |
Christopher Ferris | 052fa3a | 2014-08-26 20:48:11 -0700 | [diff] [blame] | 1607 | } |
| 1608 | char* end = stpcpy(buffer, kDefaultLdPaths[0]); |
| 1609 | *end = ':'; |
| 1610 | strcpy(end + 1, kDefaultLdPaths[1]); |
Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 1611 | } |
| 1612 | |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 1613 | void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) { |
Nick Kralevich | 6bb01b6 | 2015-03-07 13:37:05 -0800 | [diff] [blame] | 1614 | parse_LD_LIBRARY_PATH(ld_library_path); |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 1615 | } |
| 1616 | |
Elliott Hughes | 1a58629 | 2014-06-03 16:23:08 -0700 | [diff] [blame] | 1617 | soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) { |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 1618 | 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] | 1619 | DL_ERR("invalid flags to dlopen: %x", flags); |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1620 | return nullptr; |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 1621 | } |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 1622 | if (extinfo != nullptr) { |
| 1623 | if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) { |
| 1624 | DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags); |
| 1625 | return nullptr; |
| 1626 | } |
| 1627 | if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 && |
Dmitriy Ivanov | a6c1279 | 2014-10-21 12:09:18 -0700 | [diff] [blame] | 1628 | (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) { |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 1629 | DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without " |
| 1630 | "ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags); |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 1631 | return nullptr; |
| 1632 | } |
Torne (Richard Coles) | 012cb45 | 2014-02-06 14:34:21 +0000 | [diff] [blame] | 1633 | } |
Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 1634 | |
| 1635 | ProtectedDataGuard guard; |
Dmitriy Ivanov | 9fb216f | 2014-11-01 02:30:38 +0000 | [diff] [blame] | 1636 | soinfo* si = find_library(name, flags, extinfo); |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1637 | if (si != nullptr) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 1638 | si->call_constructors(); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1639 | } |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1640 | return si; |
| 1641 | } |
| 1642 | |
Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 1643 | void do_dlclose(soinfo* si) { |
Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 1644 | ProtectedDataGuard guard; |
Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 1645 | soinfo_unload(si); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1646 | } |
| 1647 | |
Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 1648 | static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) { |
| 1649 | typedef ElfW(Addr) (*ifunc_resolver_t)(void); |
| 1650 | ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr); |
| 1651 | ElfW(Addr) ifunc_addr = ifunc_resolver(); |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 1652 | TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p", |
| 1653 | ifunc_resolver, reinterpret_cast<void*>(ifunc_addr)); |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 1654 | |
Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 1655 | return ifunc_addr; |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 1656 | } |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 1657 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1658 | const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const { |
| 1659 | if (source_symver < 2 || |
| 1660 | source_symver >= version_infos.size() || |
| 1661 | version_infos[source_symver].name == nullptr) { |
| 1662 | return nullptr; |
| 1663 | } |
| 1664 | |
| 1665 | return &version_infos[source_symver]; |
| 1666 | } |
| 1667 | |
| 1668 | void VersionTracker::add_version_info(size_t source_index, |
| 1669 | ElfW(Word) elf_hash, |
| 1670 | const char* ver_name, |
| 1671 | const soinfo* target_si) { |
| 1672 | if (source_index >= version_infos.size()) { |
| 1673 | version_infos.resize(source_index+1); |
| 1674 | } |
| 1675 | |
| 1676 | version_infos[source_index].elf_hash = elf_hash; |
| 1677 | version_infos[source_index].name = ver_name; |
| 1678 | version_infos[source_index].target_si = target_si; |
| 1679 | } |
| 1680 | |
| 1681 | bool VersionTracker::init_verneed(const soinfo* si_from) { |
| 1682 | uintptr_t verneed_ptr = si_from->get_verneed_ptr(); |
| 1683 | |
| 1684 | if (verneed_ptr == 0) { |
| 1685 | return true; |
| 1686 | } |
| 1687 | |
| 1688 | size_t verneed_cnt = si_from->get_verneed_cnt(); |
| 1689 | |
| 1690 | for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) { |
| 1691 | const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset); |
| 1692 | size_t vernaux_offset = offset + verneed->vn_aux; |
| 1693 | offset += verneed->vn_next; |
| 1694 | |
| 1695 | if (verneed->vn_version != 1) { |
| 1696 | DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version); |
| 1697 | return false; |
| 1698 | } |
| 1699 | |
| 1700 | const char* target_soname = si_from->get_string(verneed->vn_file); |
| 1701 | // find it in dependencies |
| 1702 | soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) { |
| 1703 | return strcmp(si->get_soname(), target_soname) == 0; |
| 1704 | }); |
| 1705 | |
| 1706 | if (target_si == nullptr) { |
| 1707 | DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"", |
| 1708 | target_soname, i, si_from->get_soname()); |
| 1709 | return false; |
| 1710 | } |
| 1711 | |
| 1712 | for (size_t j = 0; j<verneed->vn_cnt; ++j) { |
| 1713 | const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset); |
| 1714 | vernaux_offset += vernaux->vna_next; |
| 1715 | |
| 1716 | const ElfW(Word) elf_hash = vernaux->vna_hash; |
| 1717 | const char* ver_name = si_from->get_string(vernaux->vna_name); |
| 1718 | ElfW(Half) source_index = vernaux->vna_other; |
| 1719 | |
| 1720 | add_version_info(source_index, elf_hash, ver_name, target_si); |
| 1721 | } |
| 1722 | } |
| 1723 | |
| 1724 | return true; |
| 1725 | } |
| 1726 | |
| 1727 | bool VersionTracker::init_verdef(const soinfo* si_from) { |
| 1728 | return for_each_verdef(si_from, |
| 1729 | [&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) { |
| 1730 | add_version_info(verdef->vd_ndx, verdef->vd_hash, |
| 1731 | si_from->get_string(verdaux->vda_name), si_from); |
| 1732 | return false; |
| 1733 | } |
| 1734 | ); |
| 1735 | } |
| 1736 | |
| 1737 | bool VersionTracker::init(const soinfo* si_from) { |
| 1738 | if (!si_from->has_min_version(2)) { |
| 1739 | return true; |
| 1740 | } |
| 1741 | |
| 1742 | return init_verneed(si_from) && init_verdef(si_from); |
| 1743 | } |
| 1744 | |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1745 | #if !defined(__mips__) |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1746 | #if defined(USE_RELA) |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1747 | static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) { |
| 1748 | return rela->r_addend; |
| 1749 | } |
| 1750 | #else |
| 1751 | static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) { |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 1752 | if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE || |
| 1753 | ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) { |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1754 | return *reinterpret_cast<ElfW(Addr)*>(reloc_addr); |
| 1755 | } |
| 1756 | return 0; |
| 1757 | } |
| 1758 | #endif |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1759 | |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 1760 | template<typename ElfRelIteratorT> |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 1761 | bool soinfo::relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group, |
| 1762 | const soinfo_list_t& local_group) { |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1763 | VersionTracker version_tracker; |
| 1764 | |
| 1765 | if (!version_tracker.init(this)) { |
| 1766 | return false; |
| 1767 | } |
| 1768 | |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 1769 | for (size_t idx = 0; rel_iterator.has_next(); ++idx) { |
| 1770 | const auto rel = rel_iterator.next(); |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 1771 | if (rel == nullptr) { |
| 1772 | return false; |
| 1773 | } |
| 1774 | |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1775 | ElfW(Word) type = ELFW(R_TYPE)(rel->r_info); |
| 1776 | ElfW(Word) sym = ELFW(R_SYM)(rel->r_info); |
| 1777 | |
| 1778 | ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias); |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 1779 | ElfW(Addr) sym_addr = 0; |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1780 | const char* sym_name = nullptr; |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1781 | ElfW(Addr) addend = get_addend(rel, reloc); |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1782 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1783 | DEBUG("Processing '%s' relocation at index %zd", get_soname(), idx); |
Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 1784 | if (type == R_GENERIC_NONE) { |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1785 | continue; |
| 1786 | } |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1787 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1788 | const ElfW(Sym)* s = nullptr; |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 1789 | soinfo* lsi = nullptr; |
| 1790 | |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1791 | if (sym != 0) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 1792 | sym_name = get_string(symtab_[sym].st_name); |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 1793 | const ElfW(Versym)* sym_ver_ptr = get_versym(sym); |
| 1794 | ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr; |
| 1795 | |
| 1796 | if (sym_ver == VER_NDX_LOCAL || sym_ver == VER_NDX_GLOBAL) { |
| 1797 | // there is no version info for this one |
| 1798 | if (!soinfo_do_lookup(this, sym_name, nullptr, &lsi, global_group, local_group, &s)) { |
| 1799 | return false; |
| 1800 | } |
| 1801 | } else { |
| 1802 | const version_info* vi = version_tracker.get_version_info(sym_ver); |
| 1803 | |
| 1804 | if (vi == nullptr) { |
| 1805 | DL_ERR("cannot find verneed/verdef for version index=%d " |
| 1806 | "referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_soname()); |
| 1807 | return false; |
| 1808 | } |
| 1809 | |
| 1810 | if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) { |
| 1811 | return false; |
| 1812 | } |
| 1813 | } |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 1814 | if (s == nullptr) { |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1815 | // We only allow an undefined symbol if this is a weak reference... |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 1816 | s = &symtab_[sym]; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1817 | if (ELF_ST_BIND(s->st_info) != STB_WEAK) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 1818 | DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_soname()); |
Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 1819 | return false; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1820 | } |
| 1821 | |
| 1822 | /* IHI0044C AAELF 4.5.1.1: |
| 1823 | |
| 1824 | Libraries are not searched to resolve weak references. |
| 1825 | It is not an error for a weak reference to remain unsatisfied. |
| 1826 | |
| 1827 | During linking, the value of an undefined weak reference is: |
| 1828 | - Zero if the relocation type is absolute |
| 1829 | - The address of the place if the relocation is pc-relative |
| 1830 | - The address of nominal base address if the relocation |
| 1831 | type is base-relative. |
| 1832 | */ |
| 1833 | |
| 1834 | switch (type) { |
Dmitriy Ivanov | 1b69469 | 2015-01-13 12:17:31 -0800 | [diff] [blame] | 1835 | case R_GENERIC_JUMP_SLOT: |
| 1836 | case R_GENERIC_GLOB_DAT: |
| 1837 | case R_GENERIC_RELATIVE: |
| 1838 | case R_GENERIC_IRELATIVE: |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1839 | #if defined(__aarch64__) |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 1840 | case R_AARCH64_ABS64: |
| 1841 | case R_AARCH64_ABS32: |
| 1842 | case R_AARCH64_ABS16: |
Dmitriy Ivanov | 1b69469 | 2015-01-13 12:17:31 -0800 | [diff] [blame] | 1843 | #elif defined(__x86_64__) |
| 1844 | case R_X86_64_32: |
| 1845 | case R_X86_64_64: |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1846 | #elif defined(__arm__) |
| 1847 | case R_ARM_ABS32: |
| 1848 | #elif defined(__i386__) |
| 1849 | case R_386_32: |
Dmitriy Ivanov | 1b69469 | 2015-01-13 12:17:31 -0800 | [diff] [blame] | 1850 | #endif |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 1851 | /* |
| 1852 | * The sym_addr was initialized to be zero above, or the relocation |
| 1853 | * code below does not care about value of sym_addr. |
| 1854 | * No need to do anything. |
| 1855 | */ |
| 1856 | break; |
Dmitriy Ivanov | 1b69469 | 2015-01-13 12:17:31 -0800 | [diff] [blame] | 1857 | #if defined(__x86_64__) |
Dimitry Ivanov | d338aac | 2015-01-13 22:31:54 +0000 | [diff] [blame] | 1858 | case R_X86_64_PC32: |
| 1859 | sym_addr = reloc; |
| 1860 | break; |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1861 | #elif defined(__i386__) |
| 1862 | case R_386_PC32: |
| 1863 | sym_addr = reloc; |
| 1864 | break; |
| 1865 | #endif |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 1866 | default: |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1867 | DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx); |
Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 1868 | return false; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1869 | } |
| 1870 | } else { |
| 1871 | // We got a definition. |
Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 1872 | sym_addr = lsi->resolve_symbol_address(s); |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1873 | } |
| 1874 | count_relocation(kRelocSymbol); |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1875 | } |
| 1876 | |
| 1877 | switch (type) { |
Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 1878 | case R_GENERIC_JUMP_SLOT: |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1879 | count_relocation(kRelocAbsolute); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1880 | MARK(rel->r_offset); |
| 1881 | TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n", |
| 1882 | reinterpret_cast<void*>(reloc), |
| 1883 | reinterpret_cast<void*>(sym_addr + addend), sym_name); |
| 1884 | |
| 1885 | *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend); |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1886 | break; |
Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 1887 | case R_GENERIC_GLOB_DAT: |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1888 | count_relocation(kRelocAbsolute); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1889 | MARK(rel->r_offset); |
| 1890 | TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n", |
| 1891 | reinterpret_cast<void*>(reloc), |
| 1892 | reinterpret_cast<void*>(sym_addr + addend), sym_name); |
| 1893 | *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend); |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1894 | break; |
Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 1895 | case R_GENERIC_RELATIVE: |
| 1896 | count_relocation(kRelocRelative); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1897 | MARK(rel->r_offset); |
| 1898 | TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n", |
| 1899 | reinterpret_cast<void*>(reloc), |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 1900 | reinterpret_cast<void*>(load_bias + addend)); |
| 1901 | *reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend); |
Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 1902 | break; |
Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 1903 | case R_GENERIC_IRELATIVE: |
| 1904 | count_relocation(kRelocRelative); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1905 | MARK(rel->r_offset); |
| 1906 | TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n", |
| 1907 | reinterpret_cast<void*>(reloc), |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 1908 | reinterpret_cast<void*>(load_bias + addend)); |
| 1909 | *reinterpret_cast<ElfW(Addr)*>(reloc) = call_ifunc_resolver(load_bias + addend); |
Dmitriy Ivanov | cefef7d | 2015-01-08 23:30:15 -0800 | [diff] [blame] | 1910 | break; |
| 1911 | |
| 1912 | #if defined(__aarch64__) |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 1913 | case R_AARCH64_ABS64: |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1914 | count_relocation(kRelocAbsolute); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1915 | MARK(rel->r_offset); |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 1916 | TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n", |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1917 | reloc, (sym_addr + addend), sym_name); |
| 1918 | *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend); |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1919 | break; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 1920 | case R_AARCH64_ABS32: |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1921 | count_relocation(kRelocAbsolute); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1922 | MARK(rel->r_offset); |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 1923 | TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n", |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1924 | reloc, (sym_addr + addend), sym_name); |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 1925 | { |
| 1926 | const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc); |
| 1927 | const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN); |
| 1928 | const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX); |
| 1929 | if ((min_value <= (reloc_value + (sym_addr + addend))) && |
| 1930 | ((reloc_value + (sym_addr + addend)) <= max_value)) { |
| 1931 | *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend); |
| 1932 | } else { |
| 1933 | DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", |
| 1934 | (reloc_value + (sym_addr + addend)), min_value, max_value); |
| 1935 | return false; |
| 1936 | } |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1937 | } |
| 1938 | break; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 1939 | case R_AARCH64_ABS16: |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1940 | count_relocation(kRelocAbsolute); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1941 | MARK(rel->r_offset); |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 1942 | TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n", |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1943 | reloc, (sym_addr + addend), sym_name); |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 1944 | { |
| 1945 | const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc); |
| 1946 | const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN); |
| 1947 | const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX); |
| 1948 | if ((min_value <= (reloc_value + (sym_addr + addend))) && |
| 1949 | ((reloc_value + (sym_addr + addend)) <= max_value)) { |
| 1950 | *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend); |
| 1951 | } else { |
| 1952 | DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", |
| 1953 | reloc_value + (sym_addr + addend), min_value, max_value); |
| 1954 | return false; |
| 1955 | } |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1956 | } |
| 1957 | break; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 1958 | case R_AARCH64_PREL64: |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1959 | count_relocation(kRelocRelative); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1960 | MARK(rel->r_offset); |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 1961 | TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n", |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1962 | reloc, (sym_addr + addend), rel->r_offset, sym_name); |
| 1963 | *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + addend) - rel->r_offset; |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1964 | break; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 1965 | case R_AARCH64_PREL32: |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1966 | count_relocation(kRelocRelative); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1967 | MARK(rel->r_offset); |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 1968 | TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n", |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1969 | reloc, (sym_addr + addend), rel->r_offset, sym_name); |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 1970 | { |
| 1971 | const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc); |
| 1972 | const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN); |
| 1973 | const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX); |
| 1974 | if ((min_value <= (reloc_value + ((sym_addr + addend) - rel->r_offset))) && |
| 1975 | ((reloc_value + ((sym_addr + addend) - rel->r_offset)) <= max_value)) { |
| 1976 | *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset); |
| 1977 | } else { |
| 1978 | DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", |
| 1979 | reloc_value + ((sym_addr + addend) - rel->r_offset), min_value, max_value); |
| 1980 | return false; |
| 1981 | } |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1982 | } |
| 1983 | break; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 1984 | case R_AARCH64_PREL16: |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 1985 | count_relocation(kRelocRelative); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1986 | MARK(rel->r_offset); |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 1987 | TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n", |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 1988 | reloc, (sym_addr + addend), rel->r_offset, sym_name); |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 1989 | { |
| 1990 | const ElfW(Addr) reloc_value = *reinterpret_cast<ElfW(Addr)*>(reloc); |
| 1991 | const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN); |
| 1992 | const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX); |
| 1993 | if ((min_value <= (reloc_value + ((sym_addr + addend) - rel->r_offset))) && |
| 1994 | ((reloc_value + ((sym_addr + addend) - rel->r_offset)) <= max_value)) { |
| 1995 | *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + addend) - rel->r_offset); |
| 1996 | } else { |
| 1997 | DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", |
| 1998 | reloc_value + ((sym_addr + addend) - rel->r_offset), min_value, max_value); |
| 1999 | return false; |
| 2000 | } |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2001 | } |
| 2002 | break; |
| 2003 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2004 | case R_AARCH64_COPY: |
Nick Kralevich | 76e289c | 2014-07-03 12:04:31 -0700 | [diff] [blame] | 2005 | /* |
| 2006 | * ET_EXEC is not supported so this should not happen. |
| 2007 | * |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2008 | * 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] | 2009 | * |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2010 | * Section 4.6.11 "Dynamic relocations" |
Nick Kralevich | 76e289c | 2014-07-03 12:04:31 -0700 | [diff] [blame] | 2011 | * R_AARCH64_COPY may only appear in executable objects where e_type is |
| 2012 | * set to ET_EXEC. |
| 2013 | */ |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2014 | DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_soname()); |
Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 2015 | return false; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2016 | case R_AARCH64_TLS_TPREL64: |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2017 | TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n", |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2018 | reloc, (sym_addr + addend), rel->r_offset); |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2019 | break; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2020 | case R_AARCH64_TLS_DTPREL32: |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2021 | TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n", |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2022 | reloc, (sym_addr + addend), rel->r_offset); |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 2023 | break; |
| 2024 | #elif defined(__x86_64__) |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2025 | case R_X86_64_32: |
| 2026 | count_relocation(kRelocRelative); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2027 | MARK(rel->r_offset); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2028 | TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc), |
| 2029 | static_cast<size_t>(sym_addr), sym_name); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2030 | *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2031 | break; |
| 2032 | case R_X86_64_64: |
| 2033 | count_relocation(kRelocRelative); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2034 | MARK(rel->r_offset); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2035 | TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc), |
| 2036 | static_cast<size_t>(sym_addr), sym_name); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2037 | *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2038 | break; |
| 2039 | case R_X86_64_PC32: |
| 2040 | count_relocation(kRelocRelative); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2041 | MARK(rel->r_offset); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2042 | TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s", |
| 2043 | static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc), |
| 2044 | static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name); |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2045 | *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - reloc; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2046 | break; |
Dmitriy Ivanov | bcc04d0 | 2015-01-13 12:12:38 -0800 | [diff] [blame] | 2047 | #elif defined(__arm__) |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2048 | case R_ARM_ABS32: |
| 2049 | count_relocation(kRelocAbsolute); |
| 2050 | MARK(rel->r_offset); |
| 2051 | TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name); |
| 2052 | *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr; |
| 2053 | break; |
| 2054 | case R_ARM_REL32: |
| 2055 | count_relocation(kRelocRelative); |
| 2056 | MARK(rel->r_offset); |
| 2057 | TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s", |
| 2058 | reloc, sym_addr, rel->r_offset, sym_name); |
| 2059 | *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset; |
| 2060 | break; |
| 2061 | case R_ARM_COPY: |
| 2062 | /* |
| 2063 | * ET_EXEC is not supported so this should not happen. |
| 2064 | * |
| 2065 | * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf |
| 2066 | * |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2067 | * Section 4.6.1.10 "Dynamic relocations" |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2068 | * R_ARM_COPY may only appear in executable objects where e_type is |
| 2069 | * set to ET_EXEC. |
| 2070 | */ |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2071 | DL_ERR("%s R_ARM_COPY relocations are not supported", get_soname()); |
Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 2072 | return false; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2073 | #elif defined(__i386__) |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2074 | case R_386_32: |
| 2075 | count_relocation(kRelocRelative); |
| 2076 | MARK(rel->r_offset); |
| 2077 | TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name); |
| 2078 | *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr; |
| 2079 | break; |
| 2080 | case R_386_PC32: |
| 2081 | count_relocation(kRelocRelative); |
| 2082 | MARK(rel->r_offset); |
| 2083 | TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s", |
| 2084 | reloc, (sym_addr - reloc), sym_addr, reloc, sym_name); |
| 2085 | *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc); |
| 2086 | break; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2087 | #endif |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2088 | default: |
| 2089 | DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2090 | return false; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2091 | } |
| 2092 | } |
| 2093 | return true; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 2094 | } |
Dmitriy Ivanov | 114ff69 | 2015-01-14 11:36:38 -0800 | [diff] [blame] | 2095 | #endif // !defined(__mips__) |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 2096 | |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2097 | void soinfo::call_array(const char* array_name __unused, linker_function_t* functions, |
| 2098 | size_t count, bool reverse) { |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 2099 | if (functions == nullptr) { |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2100 | return; |
| 2101 | } |
David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 2102 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2103 | TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_soname()); |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 2104 | |
| 2105 | int begin = reverse ? (count - 1) : 0; |
| 2106 | int end = reverse ? -1 : count; |
| 2107 | int step = reverse ? -1 : 1; |
| 2108 | |
| 2109 | for (int i = begin; i != end; i += step) { |
| 2110 | TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]); |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2111 | call_function("function", functions[i]); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2112 | } |
David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 2113 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2114 | TRACE("[ Done calling %s for '%s' ]", array_name, get_soname()); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2115 | } |
| 2116 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2117 | 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] | 2118 | 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] | 2119 | return; |
| 2120 | } |
| 2121 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2122 | TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_soname()); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2123 | function(); |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2124 | TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_soname()); |
Evgeniy Stepanov | 9181a5d | 2012-08-13 17:58:37 +0400 | [diff] [blame] | 2125 | } |
| 2126 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2127 | void soinfo::call_pre_init_constructors() { |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 2128 | // DT_PREINIT_ARRAY functions are called before any other constructors for executables, |
| 2129 | // but ignored in a shared library. |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2130 | call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2131 | } |
Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 2132 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2133 | void soinfo::call_constructors() { |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2134 | if (constructors_called) { |
| 2135 | return; |
| 2136 | } |
Jesse Hall | f5d1693 | 2012-01-30 15:39:57 -0800 | [diff] [blame] | 2137 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2138 | // We set constructors_called before actually calling the constructors, otherwise it doesn't |
| 2139 | // protect against recursive constructor calls. One simple example of constructor recursion |
| 2140 | // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so: |
| 2141 | // 1. The program depends on libc, so libc's constructor is called here. |
| 2142 | // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so. |
| 2143 | // 3. dlopen() calls the constructors on the newly created |
| 2144 | // soinfo for libc_malloc_debug_leak.so. |
| 2145 | // 4. The debug .so depends on libc, so CallConstructors is |
| 2146 | // called again with the libc soinfo. If it doesn't trigger the early- |
| 2147 | // out above, the libc constructor will be called again (recursively!). |
| 2148 | constructors_called = true; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2149 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2150 | if (!is_main_executable() && preinit_array_ != nullptr) { |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 2151 | // The GNU dynamic linker silently ignores these, but we warn the developer. |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 2152 | PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2153 | get_soname(), preinit_array_count_); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2154 | } |
| 2155 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2156 | get_children().for_each([] (soinfo* si) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2157 | si->call_constructors(); |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2158 | }); |
Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 2159 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2160 | TRACE("\"%s\": calling constructors", get_soname()); |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 2161 | |
| 2162 | // 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] | 2163 | call_function("DT_INIT", init_func_); |
| 2164 | call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false); |
Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 2165 | } |
David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 2166 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2167 | void soinfo::call_destructors() { |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2168 | if (!constructors_called) { |
| 2169 | return; |
| 2170 | } |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2171 | TRACE("\"%s\": calling destructors", get_soname()); |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 2172 | |
| 2173 | // DT_FINI_ARRAY must be parsed in reverse order. |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2174 | call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true); |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 2175 | |
| 2176 | // 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] | 2177 | call_function("DT_FINI", fini_func_); |
Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 2178 | |
| 2179 | // This is needed on second call to dlopen |
| 2180 | // after library has been unloaded with RTLD_NODELETE |
| 2181 | constructors_called = false; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2182 | } |
| 2183 | |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2184 | void soinfo::add_child(soinfo* child) { |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 2185 | if (has_min_version(0)) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2186 | child->parents_.push_back(this); |
| 2187 | this->children_.push_back(child); |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2188 | } |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2189 | } |
| 2190 | |
| 2191 | void soinfo::remove_all_links() { |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 2192 | if (!has_min_version(0)) { |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2193 | return; |
| 2194 | } |
| 2195 | |
| 2196 | // 1. Untie connected soinfos from 'this'. |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2197 | children_.for_each([&] (soinfo* child) { |
| 2198 | child->parents_.remove_if([&] (const soinfo* parent) { |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2199 | return parent == this; |
| 2200 | }); |
| 2201 | }); |
| 2202 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2203 | parents_.for_each([&] (soinfo* parent) { |
| 2204 | parent->children_.remove_if([&] (const soinfo* child) { |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2205 | return child == this; |
| 2206 | }); |
| 2207 | }); |
| 2208 | |
| 2209 | // 2. Once everything untied - clear local lists. |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2210 | parents_.clear(); |
| 2211 | children_.clear(); |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2212 | } |
| 2213 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2214 | dev_t soinfo::get_st_dev() const { |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 2215 | if (has_min_version(0)) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2216 | return st_dev_; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2217 | } |
| 2218 | |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 2219 | return 0; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2220 | }; |
| 2221 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2222 | ino_t soinfo::get_st_ino() const { |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 2223 | if (has_min_version(0)) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2224 | return st_ino_; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2225 | } |
| 2226 | |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 2227 | return 0; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2228 | } |
| 2229 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2230 | off64_t soinfo::get_file_offset() const { |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 2231 | if (has_min_version(1)) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2232 | return file_offset_; |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 2233 | } |
| 2234 | |
| 2235 | return 0; |
| 2236 | } |
| 2237 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2238 | uint32_t soinfo::get_rtld_flags() const { |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 2239 | if (has_min_version(1)) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2240 | return rtld_flags_; |
Dmitriy Ivanov | e8ba50f | 2014-09-15 17:00:10 -0700 | [diff] [blame] | 2241 | } |
| 2242 | |
| 2243 | return 0; |
| 2244 | } |
| 2245 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2246 | uint32_t soinfo::get_dt_flags_1() const { |
| 2247 | if (has_min_version(1)) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2248 | return dt_flags_1_; |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2249 | } |
| 2250 | |
| 2251 | return 0; |
| 2252 | } |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 2253 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2254 | void soinfo::set_dt_flags_1(uint32_t dt_flags_1) { |
| 2255 | if (has_min_version(1)) { |
| 2256 | if ((dt_flags_1 & DF_1_GLOBAL) != 0) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2257 | rtld_flags_ |= RTLD_GLOBAL; |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2258 | } |
| 2259 | |
| 2260 | if ((dt_flags_1 & DF_1_NODELETE) != 0) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2261 | rtld_flags_ |= RTLD_NODELETE; |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2262 | } |
| 2263 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2264 | dt_flags_1_ = dt_flags_1; |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2265 | } |
| 2266 | } |
| 2267 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2268 | const char* soinfo::get_realpath() const { |
| 2269 | #if defined(__arm__) |
| 2270 | if (has_min_version(2)) { |
| 2271 | return realpath_.c_str(); |
| 2272 | } else { |
| 2273 | return old_name_; |
| 2274 | } |
| 2275 | #else |
| 2276 | return realpath_.c_str(); |
| 2277 | #endif |
| 2278 | } |
| 2279 | |
| 2280 | const char* soinfo::get_soname() const { |
| 2281 | #if defined(__arm__) |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 2282 | if (has_min_version(2)) { |
| 2283 | return soname_; |
| 2284 | } else { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2285 | return old_name_; |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 2286 | } |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2287 | #else |
| 2288 | return soname_; |
| 2289 | #endif |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 2290 | } |
| 2291 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2292 | // This is a return on get_children()/get_parents() if |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2293 | // 'this->flags' does not have FLAG_NEW_SOINFO set. |
| 2294 | static soinfo::soinfo_list_t g_empty_list; |
| 2295 | |
| 2296 | soinfo::soinfo_list_t& soinfo::get_children() { |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 2297 | if (has_min_version(0)) { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2298 | return children_; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2299 | } |
| 2300 | |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 2301 | return g_empty_list; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 2302 | } |
| 2303 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2304 | const soinfo::soinfo_list_t& soinfo::get_children() const { |
| 2305 | if (has_min_version(0)) { |
| 2306 | return children_; |
| 2307 | } |
| 2308 | |
| 2309 | return g_empty_list; |
| 2310 | } |
| 2311 | |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2312 | soinfo::soinfo_list_t& soinfo::get_parents() { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2313 | if (has_min_version(0)) { |
| 2314 | return parents_; |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2315 | } |
| 2316 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2317 | return g_empty_list; |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2318 | } |
| 2319 | |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2320 | ElfW(Addr) soinfo::resolve_symbol_address(const ElfW(Sym)* s) const { |
Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 2321 | if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) { |
| 2322 | return call_ifunc_resolver(s->st_value + load_bias); |
| 2323 | } |
| 2324 | |
| 2325 | return static_cast<ElfW(Addr)>(s->st_value + load_bias); |
| 2326 | } |
| 2327 | |
Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 2328 | const char* soinfo::get_string(ElfW(Word) index) const { |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2329 | if (has_min_version(1) && (index >= strtab_size_)) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2330 | __libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d", |
| 2331 | get_soname(), strtab_size_, index); |
Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 2332 | } |
| 2333 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2334 | return strtab_ + index; |
Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 2335 | } |
| 2336 | |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 2337 | bool soinfo::is_gnu_hash() const { |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2338 | return (flags_ & FLAG_GNU_HASH) != 0; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 2339 | } |
| 2340 | |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 2341 | bool soinfo::can_unload() const { |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2342 | return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0; |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 2343 | } |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2344 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2345 | bool soinfo::is_linked() const { |
| 2346 | return (flags_ & FLAG_LINKED) != 0; |
| 2347 | } |
| 2348 | |
| 2349 | bool soinfo::is_main_executable() const { |
| 2350 | return (flags_ & FLAG_EXE) != 0; |
| 2351 | } |
| 2352 | |
| 2353 | void soinfo::set_linked() { |
| 2354 | flags_ |= FLAG_LINKED; |
| 2355 | } |
| 2356 | |
| 2357 | void soinfo::set_linker_flag() { |
| 2358 | flags_ |= FLAG_LINKER; |
| 2359 | } |
| 2360 | |
| 2361 | void soinfo::set_main_executable() { |
| 2362 | flags_ |= FLAG_EXE; |
| 2363 | } |
| 2364 | |
| 2365 | void soinfo::increment_ref_count() { |
| 2366 | local_group_root_->ref_count_++; |
| 2367 | } |
| 2368 | |
| 2369 | size_t soinfo::decrement_ref_count() { |
| 2370 | return --local_group_root_->ref_count_; |
| 2371 | } |
| 2372 | |
| 2373 | soinfo* soinfo::get_local_group_root() const { |
| 2374 | return local_group_root_; |
| 2375 | } |
| 2376 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2377 | /* Force any of the closed stdin, stdout and stderr to be associated with |
| 2378 | /dev/null. */ |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 2379 | static int nullify_closed_stdio() { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2380 | int dev_null, i, status; |
| 2381 | int return_value = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2382 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2383 | dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)); |
| 2384 | if (dev_null < 0) { |
| 2385 | DL_ERR("cannot open /dev/null: %s", strerror(errno)); |
| 2386 | return -1; |
| 2387 | } |
| 2388 | TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2389 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2390 | /* If any of the stdio file descriptors is valid and not associated |
| 2391 | with /dev/null, dup /dev/null to it. */ |
| 2392 | for (i = 0; i < 3; i++) { |
| 2393 | /* If it is /dev/null already, we are done. */ |
| 2394 | if (i == dev_null) { |
| 2395 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2396 | } |
| 2397 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2398 | TRACE("[ Nullifying stdio file descriptor %d]", i); |
| 2399 | status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL)); |
| 2400 | |
| 2401 | /* If file is opened, we are good. */ |
| 2402 | if (status != -1) { |
| 2403 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2404 | } |
| 2405 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2406 | /* The only error we allow is that the file descriptor does not |
| 2407 | exist, in which case we dup /dev/null to it. */ |
| 2408 | if (errno != EBADF) { |
| 2409 | DL_ERR("fcntl failed: %s", strerror(errno)); |
| 2410 | return_value = -1; |
| 2411 | continue; |
| 2412 | } |
| 2413 | |
| 2414 | /* Try dupping /dev/null to this stdio file descriptor and |
| 2415 | repeat if there is a signal. Note that any errors in closing |
| 2416 | the stdio descriptor are lost. */ |
| 2417 | status = TEMP_FAILURE_RETRY(dup2(dev_null, i)); |
| 2418 | if (status < 0) { |
| 2419 | DL_ERR("dup2 failed: %s", strerror(errno)); |
| 2420 | return_value = -1; |
| 2421 | continue; |
| 2422 | } |
| 2423 | } |
| 2424 | |
| 2425 | /* If /dev/null is not one of the stdio file descriptors, close it. */ |
| 2426 | if (dev_null > 2) { |
| 2427 | TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null); |
| 2428 | status = TEMP_FAILURE_RETRY(close(dev_null)); |
| 2429 | if (status == -1) { |
| 2430 | DL_ERR("close failed: %s", strerror(errno)); |
| 2431 | return_value = -1; |
| 2432 | } |
| 2433 | } |
| 2434 | |
| 2435 | return return_value; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2436 | } |
| 2437 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2438 | bool soinfo::prelink_image() { |
Ningsheng Jian | e93be99 | 2014-09-16 15:22:10 +0800 | [diff] [blame] | 2439 | /* Extract dynamic section */ |
| 2440 | ElfW(Word) dynamic_flags = 0; |
| 2441 | phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags); |
Dmitriy Ivanov | 498eb18 | 2014-09-05 14:57:59 -0700 | [diff] [blame] | 2442 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2443 | /* We can't log anything until the linker is relocated */ |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2444 | bool relocating_linker = (flags_ & FLAG_LINKER) != 0; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2445 | if (!relocating_linker) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2446 | INFO("[ linking %s ]", get_soname()); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2447 | 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] | 2448 | } |
| 2449 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2450 | if (dynamic == nullptr) { |
David 'Digit' Turner | b52e438 | 2012-06-19 01:24:17 +0200 | [diff] [blame] | 2451 | if (!relocating_linker) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2452 | DL_ERR("missing PT_DYNAMIC in \"%s\"", get_soname()); |
David 'Digit' Turner | b52e438 | 2012-06-19 01:24:17 +0200 | [diff] [blame] | 2453 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2454 | return false; |
| 2455 | } else { |
| 2456 | if (!relocating_linker) { |
| 2457 | DEBUG("dynamic = %p", dynamic); |
David 'Digit' Turner | 63f99f4 | 2012-06-19 00:08:39 +0200 | [diff] [blame] | 2458 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2459 | } |
David 'Digit' Turner | 63f99f4 | 2012-06-19 00:08:39 +0200 | [diff] [blame] | 2460 | |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 2461 | #if defined(__arm__) |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2462 | (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias, |
| 2463 | &ARM_exidx, &ARM_exidx_count); |
David 'Digit' Turner | 63f99f4 | 2012-06-19 00:08:39 +0200 | [diff] [blame] | 2464 | #endif |
| 2465 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2466 | // Extract useful information from dynamic section. |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 2467 | // Note that: "Except for the DT_NULL element at the end of the array, |
| 2468 | // and the relative order of DT_NEEDED elements, entries may appear in any order." |
| 2469 | // |
| 2470 | // 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] | 2471 | uint32_t needed_count = 0; |
| 2472 | for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) { |
| 2473 | DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p", |
| 2474 | d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val)); |
| 2475 | switch (d->d_tag) { |
Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 2476 | case DT_SONAME: |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 2477 | // this is parsed after we have strtab initialized (see below). |
Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 2478 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2479 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2480 | case DT_HASH: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2481 | nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0]; |
| 2482 | nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1]; |
| 2483 | bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8); |
| 2484 | 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] | 2485 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2486 | |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 2487 | case DT_GNU_HASH: |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 2488 | 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] | 2489 | // skip symndx |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2490 | gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2]; |
| 2491 | 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] | 2492 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2493 | 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] | 2494 | gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_); |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 2495 | // amend chain for symndx = header[1] |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2496 | gnu_chain_ = gnu_bucket_ + gnu_nbucket_ - |
| 2497 | reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1]; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 2498 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2499 | if (!powerof2(gnu_maskwords_)) { |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2500 | 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] | 2501 | gnu_maskwords_, get_realpath()); |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 2502 | return false; |
| 2503 | } |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2504 | --gnu_maskwords_; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 2505 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2506 | flags_ |= FLAG_GNU_HASH; |
Dmitriy Ivanov | ec18ce0 | 2014-11-09 19:27:20 -0800 | [diff] [blame] | 2507 | break; |
| 2508 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2509 | case DT_STRTAB: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2510 | strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2511 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2512 | |
Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 2513 | case DT_STRSZ: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2514 | strtab_size_ = d->d_un.d_val; |
Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 2515 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2516 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2517 | case DT_SYMTAB: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2518 | symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2519 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2520 | |
Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 2521 | case DT_SYMENT: |
| 2522 | if (d->d_un.d_val != sizeof(ElfW(Sym))) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2523 | DL_ERR("invalid DT_SYMENT: %zd in \"%s\"", |
| 2524 | static_cast<size_t>(d->d_un.d_val), get_realpath()); |
Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 2525 | return false; |
| 2526 | } |
| 2527 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2528 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2529 | case DT_PLTREL: |
Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 2530 | #if defined(USE_RELA) |
| 2531 | if (d->d_un.d_val != DT_RELA) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2532 | DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath()); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2533 | return false; |
| 2534 | } |
Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 2535 | #else |
| 2536 | if (d->d_un.d_val != DT_REL) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2537 | DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath()); |
Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 2538 | return false; |
| 2539 | } |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2540 | #endif |
Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 2541 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2542 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2543 | case DT_JMPREL: |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 2544 | #if defined(USE_RELA) |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2545 | 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] | 2546 | #else |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2547 | 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] | 2548 | #endif |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2549 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2550 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2551 | case DT_PLTRELSZ: |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 2552 | #if defined(USE_RELA) |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2553 | plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela)); |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2554 | #else |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2555 | plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel)); |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2556 | #endif |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2557 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2558 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2559 | case DT_PLTGOT: |
Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 2560 | #if defined(__mips__) |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2561 | // Used by mips and mips64. |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2562 | 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] | 2563 | #endif |
Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 2564 | // Ignore for other platforms... (because RTLD_LAZY is not supported) |
| 2565 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2566 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2567 | case DT_DEBUG: |
| 2568 | // Set the DT_DEBUG entry to the address of _r_debug for GDB |
| 2569 | // if the dynamic table is writable |
Chris Dearman | 9918665 | 2014-02-06 20:36:51 -0800 | [diff] [blame] | 2570 | // FIXME: not working currently for N64 |
| 2571 | // The flags for the LOAD and DYNAMIC program headers do not agree. |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2572 | // The LOAD section containing the dynamic table has been mapped as |
Chris Dearman | 9918665 | 2014-02-06 20:36:51 -0800 | [diff] [blame] | 2573 | // read-only, but the DYNAMIC header claims it is writable. |
| 2574 | #if !(defined(__mips__) && defined(__LP64__)) |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2575 | if ((dynamic_flags & PF_W) != 0) { |
| 2576 | d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug); |
| 2577 | } |
Chris Dearman | 9918665 | 2014-02-06 20:36:51 -0800 | [diff] [blame] | 2578 | #endif |
Dmitriy Ivanov | c6292ea | 2015-02-13 16:29:50 -0800 | [diff] [blame] | 2579 | break; |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 2580 | #if defined(USE_RELA) |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2581 | case DT_RELA: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2582 | rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2583 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2584 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2585 | case DT_RELASZ: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2586 | rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela)); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2587 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2588 | |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2589 | case DT_ANDROID_RELA: |
| 2590 | android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr); |
| 2591 | break; |
| 2592 | |
| 2593 | case DT_ANDROID_RELASZ: |
| 2594 | android_relocs_size_ = d->d_un.d_val; |
| 2595 | break; |
| 2596 | |
| 2597 | case DT_ANDROID_REL: |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2598 | DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath()); |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2599 | return false; |
| 2600 | |
| 2601 | case DT_ANDROID_RELSZ: |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2602 | DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath()); |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2603 | return false; |
| 2604 | |
Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 2605 | case DT_RELAENT: |
| 2606 | if (d->d_un.d_val != sizeof(ElfW(Rela))) { |
Dmitriy Ivanov | f240aa8 | 2014-09-16 23:34:20 -0700 | [diff] [blame] | 2607 | 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] | 2608 | return false; |
| 2609 | } |
| 2610 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2611 | |
| 2612 | // ignored (see DT_RELCOUNT comments for details) |
Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 2613 | case DT_RELACOUNT: |
Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 2614 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2615 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2616 | case DT_REL: |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2617 | DL_ERR("unsupported DT_REL in \"%s\"", get_realpath()); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2618 | return false; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2619 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2620 | case DT_RELSZ: |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2621 | DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath()); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2622 | return false; |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2623 | |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2624 | #else |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2625 | case DT_REL: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2626 | rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2627 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2628 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2629 | case DT_RELSZ: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2630 | rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel)); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2631 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2632 | |
Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 2633 | case DT_RELENT: |
| 2634 | if (d->d_un.d_val != sizeof(ElfW(Rel))) { |
Dmitriy Ivanov | f240aa8 | 2014-09-16 23:34:20 -0700 | [diff] [blame] | 2635 | 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] | 2636 | return false; |
| 2637 | } |
| 2638 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2639 | |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2640 | case DT_ANDROID_REL: |
| 2641 | android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr); |
| 2642 | break; |
| 2643 | |
| 2644 | case DT_ANDROID_RELSZ: |
| 2645 | android_relocs_size_ = d->d_un.d_val; |
| 2646 | break; |
| 2647 | |
| 2648 | case DT_ANDROID_RELA: |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2649 | DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath()); |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2650 | return false; |
| 2651 | |
| 2652 | case DT_ANDROID_RELASZ: |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2653 | DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath()); |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2654 | return false; |
| 2655 | |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2656 | // "Indicates that all RELATIVE relocations have been concatenated together, |
| 2657 | // and specifies the RELATIVE relocation count." |
| 2658 | // |
| 2659 | // TODO: Spec also mentions that this can be used to optimize relocation process; |
| 2660 | // Not currently used by bionic linker - ignored. |
Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 2661 | case DT_RELCOUNT: |
Dmitriy Ivanov | 4a6e9a8 | 2014-09-16 15:51:25 -0700 | [diff] [blame] | 2662 | break; |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2663 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2664 | case DT_RELA: |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2665 | DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath()); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2666 | return false; |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2667 | |
| 2668 | case DT_RELASZ: |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2669 | DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath()); |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2670 | return false; |
| 2671 | |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2672 | #endif |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2673 | case DT_INIT: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2674 | 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] | 2675 | DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2676 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2677 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2678 | case DT_FINI: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2679 | 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] | 2680 | DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2681 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2682 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2683 | case DT_INIT_ARRAY: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2684 | 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] | 2685 | 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] | 2686 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2687 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2688 | case DT_INIT_ARRAYSZ: |
Dmitriy Ivanov | 1649e7e | 2015-01-22 16:04:25 -0800 | [diff] [blame] | 2689 | 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] | 2690 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2691 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2692 | case DT_FINI_ARRAY: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2693 | 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] | 2694 | 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] | 2695 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2696 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2697 | case DT_FINI_ARRAYSZ: |
Dmitriy Ivanov | 1649e7e | 2015-01-22 16:04:25 -0800 | [diff] [blame] | 2698 | 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] | 2699 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2700 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2701 | case DT_PREINIT_ARRAY: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2702 | 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] | 2703 | 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] | 2704 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2705 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2706 | case DT_PREINIT_ARRAYSZ: |
Dmitriy Ivanov | 1649e7e | 2015-01-22 16:04:25 -0800 | [diff] [blame] | 2707 | 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] | 2708 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2709 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2710 | case DT_TEXTREL: |
Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 2711 | #if defined(__LP64__) |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2712 | 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] | 2713 | return false; |
Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 2714 | #else |
| 2715 | has_text_relocations = true; |
| 2716 | break; |
| 2717 | #endif |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2718 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2719 | case DT_SYMBOLIC: |
Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 2720 | has_DT_SYMBOLIC = true; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2721 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2722 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2723 | case DT_NEEDED: |
| 2724 | ++needed_count; |
| 2725 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2726 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2727 | case DT_FLAGS: |
| 2728 | if (d->d_un.d_val & DF_TEXTREL) { |
Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 2729 | #if defined(__LP64__) |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2730 | 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] | 2731 | return false; |
Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 2732 | #else |
| 2733 | has_text_relocations = true; |
| 2734 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2735 | } |
Dmitriy Ivanov | 96bc37f | 2014-09-29 12:10:36 -0700 | [diff] [blame] | 2736 | if (d->d_un.d_val & DF_SYMBOLIC) { |
| 2737 | has_DT_SYMBOLIC = true; |
| 2738 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2739 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2740 | |
Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 2741 | case DT_FLAGS_1: |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2742 | set_dt_flags_1(d->d_un.d_val); |
Dmitriy Ivanov | 1b20daf | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 2743 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 2744 | if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) { |
Dmitriy Ivanov | 6cdeb52 | 2014-09-29 19:14:45 -0700 | [diff] [blame] | 2745 | DL_WARN("Unsupported flags DT_FLAGS_1=%p", reinterpret_cast<void*>(d->d_un.d_val)); |
| 2746 | } |
| 2747 | break; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2748 | #if defined(__mips__) |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2749 | case DT_MIPS_RLD_MAP: |
| 2750 | // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB. |
| 2751 | { |
| 2752 | r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr); |
| 2753 | *dp = &_r_debug; |
| 2754 | } |
| 2755 | break; |
Raghu Gandham | 6881572 | 2014-12-18 19:12:19 -0800 | [diff] [blame] | 2756 | case DT_MIPS_RLD_MAP2: |
| 2757 | // Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB. |
| 2758 | { |
Dmitriy Ivanov | 20d89cb | 2015-03-30 18:43:38 -0700 | [diff] [blame] | 2759 | r_debug** dp = reinterpret_cast<r_debug**>( |
| 2760 | reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val); |
Raghu Gandham | 6881572 | 2014-12-18 19:12:19 -0800 | [diff] [blame] | 2761 | *dp = &_r_debug; |
| 2762 | } |
| 2763 | break; |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2764 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2765 | case DT_MIPS_RLD_VERSION: |
| 2766 | case DT_MIPS_FLAGS: |
| 2767 | case DT_MIPS_BASE_ADDRESS: |
| 2768 | case DT_MIPS_UNREFEXTNO: |
| 2769 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2770 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2771 | case DT_MIPS_SYMTABNO: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2772 | mips_symtabno_ = d->d_un.d_val; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2773 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2774 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2775 | case DT_MIPS_LOCAL_GOTNO: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2776 | mips_local_gotno_ = d->d_un.d_val; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2777 | break; |
| 2778 | |
| 2779 | case DT_MIPS_GOTSYM: |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2780 | mips_gotsym_ = d->d_un.d_val; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2781 | break; |
| 2782 | #endif |
Dmitriy Ivanov | ea6eae1 | 2014-10-15 14:59:01 -0700 | [diff] [blame] | 2783 | // Ignored: "Its use has been superseded by the DF_BIND_NOW flag" |
| 2784 | case DT_BIND_NOW: |
| 2785 | break; |
| 2786 | |
Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 2787 | case DT_VERSYM: |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2788 | versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr); |
| 2789 | break; |
| 2790 | |
Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 2791 | case DT_VERDEF: |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2792 | verdef_ptr_ = load_bias + d->d_un.d_ptr; |
| 2793 | break; |
Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 2794 | case DT_VERDEFNUM: |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2795 | verdef_cnt_ = d->d_un.d_val; |
| 2796 | break; |
| 2797 | |
Alexander Ivchenko | e831433 | 2014-12-02 15:32:25 +0300 | [diff] [blame] | 2798 | case DT_VERNEED: |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2799 | verneed_ptr_ = load_bias + d->d_un.d_ptr; |
| 2800 | break; |
| 2801 | |
Alexander Ivchenko | e831433 | 2014-12-02 15:32:25 +0300 | [diff] [blame] | 2802 | case DT_VERNEEDNUM: |
Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 2803 | verneed_cnt_ = d->d_un.d_val; |
Dmitriy Ivanov | 513e29e | 2014-10-06 11:30:43 -0700 | [diff] [blame] | 2804 | break; |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2805 | |
| 2806 | default: |
Dmitriy Ivanov | 8f61d99 | 2014-09-16 14:31:06 -0700 | [diff] [blame] | 2807 | if (!relocating_linker) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2808 | DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(), |
Dmitriy Ivanov | 8f61d99 | 2014-09-16 14:31:06 -0700 | [diff] [blame] | 2809 | reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val)); |
| 2810 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2811 | break; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 2812 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2813 | } |
| 2814 | |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 2815 | // second pass - parse entries relying on strtab |
| 2816 | for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) { |
| 2817 | if (d->d_tag == DT_SONAME) { |
| 2818 | soname_ = get_string(d->d_un.d_val); |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2819 | #if defined(__arm__) |
| 2820 | strlcpy(old_name_, soname_, sizeof(old_name_)); |
| 2821 | #endif |
Dmitriy Ivanov | 618f1a3 | 2015-03-17 20:06:36 -0700 | [diff] [blame] | 2822 | break; |
| 2823 | } |
| 2824 | } |
| 2825 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2826 | DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p", |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2827 | reinterpret_cast<void*>(base), strtab_, symtab_); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2828 | |
| 2829 | // Sanity checks. |
| 2830 | if (relocating_linker && needed_count != 0) { |
| 2831 | DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries"); |
| 2832 | return false; |
| 2833 | } |
Dmitriy Ivanov | 3597b80 | 2015-03-09 12:02:02 -0700 | [diff] [blame] | 2834 | if (nbucket_ == 0 && gnu_nbucket_ == 0) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2835 | DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" " |
| 2836 | "(new hash type from the future?)", get_soname()); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2837 | return false; |
| 2838 | } |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2839 | if (strtab_ == 0) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2840 | DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_soname()); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2841 | return false; |
| 2842 | } |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2843 | if (symtab_ == 0) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2844 | DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_soname()); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2845 | return false; |
| 2846 | } |
| 2847 | return true; |
Dmitriy Ivanov | 14669a9 | 2014-09-05 16:42:53 -0700 | [diff] [blame] | 2848 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2849 | |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2850 | bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group, |
| 2851 | const android_dlextinfo* extinfo) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2852 | |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 2853 | local_group_root_ = local_group.front(); |
| 2854 | if (local_group_root_ == nullptr) { |
| 2855 | local_group_root_ = this; |
| 2856 | } |
| 2857 | |
Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 2858 | #if !defined(__LP64__) |
| 2859 | if (has_text_relocations) { |
| 2860 | // Make segments writable to allow text relocations to work properly. We will later call |
| 2861 | // phdr_table_protect_segments() after all of them are applied and all constructors are run. |
| 2862 | DL_WARN("%s has text relocations. This is wasting memory and prevents " |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2863 | "security hardening. Please fix.", get_soname()); |
Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 2864 | if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) { |
| 2865 | DL_ERR("can't unprotect loadable segments for \"%s\": %s", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2866 | get_soname(), strerror(errno)); |
Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 2867 | return false; |
| 2868 | } |
| 2869 | } |
| 2870 | #endif |
| 2871 | |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2872 | if (android_relocs_ != nullptr) { |
| 2873 | // check signature |
| 2874 | if (android_relocs_size_ > 3 && |
| 2875 | android_relocs_[0] == 'A' && |
| 2876 | android_relocs_[1] == 'P' && |
| 2877 | (android_relocs_[2] == 'U' || android_relocs_[2] == 'S') && |
| 2878 | android_relocs_[3] == '2') { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2879 | DEBUG("[ android relocating %s ]", get_soname()); |
Dmitriy Ivanov | 18a6956 | 2015-02-04 16:05:30 -0800 | [diff] [blame] | 2880 | |
| 2881 | bool relocated = false; |
| 2882 | const uint8_t* packed_relocs = android_relocs_ + 4; |
| 2883 | const size_t packed_relocs_size = android_relocs_size_ - 4; |
| 2884 | |
| 2885 | if (android_relocs_[2] == 'U') { |
| 2886 | relocated = relocate( |
| 2887 | packed_reloc_iterator<leb128_decoder>( |
| 2888 | leb128_decoder(packed_relocs, packed_relocs_size)), |
| 2889 | global_group, local_group); |
| 2890 | } else { // android_relocs_[2] == 'S' |
| 2891 | relocated = relocate( |
| 2892 | packed_reloc_iterator<sleb128_decoder>( |
| 2893 | sleb128_decoder(packed_relocs, packed_relocs_size)), |
| 2894 | global_group, local_group); |
| 2895 | } |
| 2896 | |
| 2897 | if (!relocated) { |
| 2898 | return false; |
| 2899 | } |
| 2900 | } else { |
| 2901 | DL_ERR("bad android relocation header."); |
| 2902 | return false; |
| 2903 | } |
| 2904 | } |
| 2905 | |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 2906 | #if defined(USE_RELA) |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2907 | if (rela_ != nullptr) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2908 | DEBUG("[ relocating %s ]", get_soname()); |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 2909 | if (!relocate(plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2910 | return false; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 2911 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2912 | } |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2913 | if (plt_rela_ != nullptr) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2914 | DEBUG("[ relocating %s plt ]", get_soname()); |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 2915 | if (!relocate(plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2916 | return false; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2917 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2918 | } |
Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 2919 | #else |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2920 | if (rel_ != nullptr) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2921 | DEBUG("[ relocating %s ]", get_soname()); |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 2922 | if (!relocate(plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2923 | return false; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2924 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2925 | } |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 2926 | if (plt_rel_ != nullptr) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2927 | DEBUG("[ relocating %s plt ]", get_soname()); |
Dmitriy Ivanov | fa26eee | 2015-02-03 16:06:47 -0800 | [diff] [blame] | 2928 | if (!relocate(plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2929 | return false; |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 2930 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2931 | } |
Dmitriy Ivanov | 9aea164 | 2014-09-11 15:16:03 -0700 | [diff] [blame] | 2932 | #endif |
Brigid Smith | c5a13ef | 2014-07-23 11:22:25 -0700 | [diff] [blame] | 2933 | |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 2934 | #if defined(__mips__) |
Dmitriy Ivanov | 8894091 | 2014-11-12 18:20:39 -0800 | [diff] [blame] | 2935 | if (!mips_relocate_got(global_group, local_group)) { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2936 | return false; |
| 2937 | } |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 2938 | #endif |
| 2939 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2940 | DEBUG("[ finished linking %s ]", get_soname()); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2941 | |
Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 2942 | #if !defined(__LP64__) |
| 2943 | if (has_text_relocations) { |
| 2944 | // All relocations are done, we can protect our segments back to read-only. |
| 2945 | if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) { |
| 2946 | DL_ERR("can't protect segments for \"%s\": %s", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2947 | get_soname(), strerror(errno)); |
Dimitry Ivanov | 56be6ed | 2015-04-01 21:18:48 +0000 | [diff] [blame] | 2948 | return false; |
| 2949 | } |
| 2950 | } |
| 2951 | #endif |
| 2952 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2953 | /* We can also turn on GNU RELRO protection */ |
| 2954 | if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) { |
| 2955 | DL_ERR("can't enable GNU RELRO protection for \"%s\": %s", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2956 | get_soname(), strerror(errno)); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2957 | return false; |
| 2958 | } |
Nick Kralevich | 9ec0f03 | 2012-02-28 10:40:00 -0800 | [diff] [blame] | 2959 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2960 | /* Handle serializing/sharing the RELRO segment */ |
| 2961 | if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) { |
| 2962 | if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias, |
| 2963 | extinfo->relro_fd) < 0) { |
| 2964 | DL_ERR("failed serializing GNU RELRO section for \"%s\": %s", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2965 | get_soname(), strerror(errno)); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2966 | return false; |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 2967 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2968 | } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) { |
| 2969 | if (phdr_table_map_gnu_relro(phdr, phnum, load_bias, |
| 2970 | extinfo->relro_fd) < 0) { |
| 2971 | DL_ERR("failed mapping GNU RELRO section for \"%s\": %s", |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 2972 | get_soname(), strerror(errno)); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2973 | return false; |
| 2974 | } |
| 2975 | } |
Torne (Richard Coles) | 183ad9d | 2014-02-27 13:18:00 +0000 | [diff] [blame] | 2976 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 2977 | notify_gdb_of_load(this); |
| 2978 | return true; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2979 | } |
| 2980 | |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 2981 | /* |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 2982 | * This function add vdso to internal dso list. |
| 2983 | * It helps to stack unwinding through signal handlers. |
| 2984 | * Also, it makes bionic more like glibc. |
| 2985 | */ |
Kito Cheng | 812fd42 | 2014-03-25 22:53:56 +0800 | [diff] [blame] | 2986 | static void add_vdso(KernelArgumentBlock& args __unused) { |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 2987 | #if defined(AT_SYSINFO_EHDR) |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2988 | 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] | 2989 | if (ehdr_vdso == nullptr) { |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2990 | return; |
| 2991 | } |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 2992 | |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 2993 | soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0); |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 2994 | |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2995 | si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff); |
| 2996 | si->phnum = ehdr_vdso->e_phnum; |
| 2997 | si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso); |
| 2998 | si->size = phdr_table_get_load_size(si->phdr, si->phnum); |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 2999 | si->load_bias = get_elf_exec_load_bias(ehdr_vdso); |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 3000 | |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3001 | si->prelink_image(); |
| 3002 | 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] | 3003 | #endif |
| 3004 | } |
| 3005 | |
| 3006 | /* |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3007 | * This is linker soinfo for GDB. See details below. |
| 3008 | */ |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 3009 | #if defined(__LP64__) |
| 3010 | #define LINKER_PATH "/system/bin/linker64" |
| 3011 | #else |
| 3012 | #define LINKER_PATH "/system/bin/linker" |
| 3013 | #endif |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3014 | |
| 3015 | // This is done to avoid calling c-tor prematurely |
| 3016 | // because soinfo c-tor needs memory allocator |
| 3017 | // which might be initialized after global variables. |
| 3018 | static uint8_t linker_soinfo_for_gdb_buf[sizeof(soinfo)] __attribute__((aligned(8))); |
| 3019 | static soinfo* linker_soinfo_for_gdb = nullptr; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3020 | |
| 3021 | /* gdb expects the linker to be in the debug shared object list. |
| 3022 | * Without this, gdb has trouble locating the linker's ".text" |
| 3023 | * and ".plt" sections. Gdb could also potentially use this to |
| 3024 | * relocate the offset of our exported 'rtld_db_dlactivity' symbol. |
| 3025 | * Don't use soinfo_alloc(), because the linker shouldn't |
| 3026 | * be on the soinfo list. |
| 3027 | */ |
| 3028 | static void init_linker_info_for_gdb(ElfW(Addr) linker_base) { |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3029 | linker_soinfo_for_gdb = new (linker_soinfo_for_gdb_buf) soinfo(LINKER_PATH, nullptr, 0, 0); |
| 3030 | |
| 3031 | linker_soinfo_for_gdb->base = linker_base; |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3032 | |
| 3033 | /* |
| 3034 | * Set the dynamic field in the link map otherwise gdb will complain with |
| 3035 | * the following: |
| 3036 | * warning: .dynamic section for "/system/bin/linker" is not at the |
| 3037 | * expected address (wrong library or version mismatch?) |
| 3038 | */ |
| 3039 | ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base); |
| 3040 | ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff); |
| 3041 | phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base, |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3042 | &linker_soinfo_for_gdb->dynamic, nullptr); |
| 3043 | insert_soinfo_into_debug_map(linker_soinfo_for_gdb); |
Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 3044 | } |
| 3045 | |
| 3046 | /* |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 3047 | * This code is called after the linker has linked itself and |
| 3048 | * fixed it's own GOT. It is safe to make references to externs |
| 3049 | * and other non-local data at this point. |
| 3050 | */ |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3051 | 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] | 3052 | #if TIMING |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3053 | struct timeval t0, t1; |
| 3054 | gettimeofday(&t0, 0); |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 3055 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3056 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3057 | // Initialize environment functions, and get to the ELF aux vectors table. |
| 3058 | linker_env_init(args); |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 3059 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3060 | // If this is a setuid/setgid program, close the security hole described in |
| 3061 | // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc |
| 3062 | if (get_AT_SECURE()) { |
| 3063 | nullify_closed_stdio(); |
| 3064 | } |
| 3065 | |
| 3066 | debuggerd_init(); |
| 3067 | |
| 3068 | // Get a few environment variables. |
| 3069 | const char* LD_DEBUG = linker_env_get("LD_DEBUG"); |
| 3070 | if (LD_DEBUG != nullptr) { |
| 3071 | g_ld_debug_verbosity = atoi(LD_DEBUG); |
| 3072 | } |
| 3073 | |
| 3074 | // Normally, these are cleaned by linker_env_init, but the test |
| 3075 | // doesn't cost us anything. |
| 3076 | const char* ldpath_env = nullptr; |
| 3077 | const char* ldpreload_env = nullptr; |
| 3078 | if (!get_AT_SECURE()) { |
| 3079 | ldpath_env = linker_env_get("LD_LIBRARY_PATH"); |
| 3080 | ldpreload_env = linker_env_get("LD_PRELOAD"); |
| 3081 | } |
| 3082 | |
Dmitriy Ivanov | bfa15e4 | 2015-01-07 15:05:49 -0800 | [diff] [blame] | 3083 | #if !defined(__LP64__) |
| 3084 | if (personality(PER_LINUX32) == -1) { |
| 3085 | __libc_fatal("error setting PER_LINUX32 personality: %s", strerror(errno)); |
| 3086 | } |
| 3087 | #endif |
| 3088 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3089 | INFO("[ android linker & debugger ]"); |
| 3090 | |
Dmitriy Ivanov | 07e5bc1 | 2014-10-03 17:52:44 -0700 | [diff] [blame] | 3091 | soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3092 | if (si == nullptr) { |
| 3093 | exit(EXIT_FAILURE); |
| 3094 | } |
| 3095 | |
| 3096 | /* bootstrap the link map, the main exe always needs to be first */ |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 3097 | si->set_main_executable(); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3098 | link_map* map = &(si->link_map_head); |
| 3099 | |
| 3100 | map->l_addr = 0; |
| 3101 | map->l_name = args.argv[0]; |
| 3102 | map->l_prev = nullptr; |
| 3103 | map->l_next = nullptr; |
| 3104 | |
| 3105 | _r_debug.r_map = map; |
| 3106 | r_debug_tail = map; |
| 3107 | |
| 3108 | init_linker_info_for_gdb(linker_base); |
| 3109 | |
| 3110 | // Extract information passed from the kernel. |
| 3111 | si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR)); |
| 3112 | si->phnum = args.getauxval(AT_PHNUM); |
| 3113 | si->entry = args.getauxval(AT_ENTRY); |
| 3114 | |
| 3115 | /* Compute the value of si->base. We can't rely on the fact that |
| 3116 | * the first entry is the PHDR because this will not be true |
| 3117 | * for certain executables (e.g. some in the NDK unit test suite) |
| 3118 | */ |
| 3119 | si->base = 0; |
| 3120 | si->size = phdr_table_get_load_size(si->phdr, si->phnum); |
| 3121 | si->load_bias = 0; |
| 3122 | for (size_t i = 0; i < si->phnum; ++i) { |
| 3123 | if (si->phdr[i].p_type == PT_PHDR) { |
| 3124 | si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr; |
| 3125 | si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset; |
| 3126 | break; |
Nick Kralevich | 8d3e91d | 2013-04-25 13:15:24 -0700 | [diff] [blame] | 3127 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3128 | } |
| 3129 | si->dynamic = nullptr; |
Nick Kralevich | 8d3e91d | 2013-04-25 13:15:24 -0700 | [diff] [blame] | 3130 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3131 | ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base); |
| 3132 | if (elf_hdr->e_type != ET_DYN) { |
| 3133 | __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n"); |
| 3134 | exit(EXIT_FAILURE); |
| 3135 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3136 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3137 | // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid). |
| 3138 | parse_LD_LIBRARY_PATH(ldpath_env); |
| 3139 | parse_LD_PRELOAD(ldpreload_env); |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 3140 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3141 | somain = si; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3142 | |
Dmitriy Ivanov | 6718125 | 2015-01-07 15:48:25 -0800 | [diff] [blame] | 3143 | if (!si->prelink_image()) { |
| 3144 | __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer()); |
| 3145 | exit(EXIT_FAILURE); |
| 3146 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3147 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3148 | // add somain to global group |
| 3149 | si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL); |
| 3150 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3151 | // Load ld_preloads and dependencies. |
| 3152 | StringLinkedList needed_library_name_list; |
| 3153 | size_t needed_libraries_count = 0; |
| 3154 | size_t ld_preloads_count = 0; |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 3155 | |
| 3156 | for (const auto& ld_preload_name : g_ld_preload_names) { |
| 3157 | needed_library_name_list.push_back(ld_preload_name.c_str()); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3158 | ++needed_libraries_count; |
| 3159 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3160 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3161 | for_each_dt_needed(si, [&](const char* name) { |
| 3162 | needed_library_name_list.push_back(name); |
| 3163 | ++needed_libraries_count; |
| 3164 | }); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3165 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3166 | const char* needed_library_names[needed_libraries_count]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3167 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3168 | memset(needed_library_names, 0, sizeof(needed_library_names)); |
| 3169 | 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] | 3170 | |
Dmitriy Ivanov | d165f56 | 2015-03-23 18:43:02 -0700 | [diff] [blame] | 3171 | if (needed_libraries_count > 0 && |
| 3172 | !find_libraries(si, needed_library_names, needed_libraries_count, nullptr, |
| 3173 | &g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr)) { |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3174 | __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer()); |
| 3175 | exit(EXIT_FAILURE); |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 3176 | } else if (needed_libraries_count == 0) { |
| 3177 | if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) { |
| 3178 | __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer()); |
| 3179 | exit(EXIT_FAILURE); |
| 3180 | } |
| 3181 | si->increment_ref_count(); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3182 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3183 | |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3184 | add_vdso(args); |
Nick Kralevich | 2aebf54 | 2014-05-07 10:32:39 -0700 | [diff] [blame] | 3185 | |
Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 3186 | { |
| 3187 | ProtectedDataGuard guard; |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 3188 | |
Dmitriy Ivanov | 279a22f | 2015-01-23 12:03:53 -0800 | [diff] [blame] | 3189 | si->call_pre_init_constructors(); |
| 3190 | |
| 3191 | /* After the prelink_image, the si->load_bias is initialized. |
| 3192 | * For so lib, the map->l_addr will be updated in notify_gdb_of_load. |
| 3193 | * We need to update this value for so exe here. So Unwind_Backtrace |
| 3194 | * for some arch like x86 could work correctly within so exe. |
| 3195 | */ |
| 3196 | map->l_addr = si->load_bias; |
| 3197 | si->call_constructors(); |
| 3198 | } |
Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 3199 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3200 | #if TIMING |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3201 | gettimeofday(&t1, nullptr); |
| 3202 | PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) ( |
| 3203 | (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) - |
| 3204 | (((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] | 3205 | #endif |
| 3206 | #if STATS |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3207 | PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0], |
| 3208 | linker_stats.count[kRelocAbsolute], |
| 3209 | linker_stats.count[kRelocRelative], |
| 3210 | linker_stats.count[kRelocCopy], |
| 3211 | linker_stats.count[kRelocSymbol]); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3212 | #endif |
| 3213 | #if COUNT_PAGES |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3214 | { |
| 3215 | unsigned n; |
| 3216 | unsigned i; |
| 3217 | unsigned count = 0; |
| 3218 | for (n = 0; n < 4096; n++) { |
| 3219 | if (bitmask[n]) { |
| 3220 | unsigned x = bitmask[n]; |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 3221 | #if defined(__LP64__) |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3222 | for (i = 0; i < 32; i++) { |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 3223 | #else |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3224 | for (i = 0; i < 8; i++) { |
Marcus Oakland | e365f9d | 2013-10-10 15:19:31 +0100 | [diff] [blame] | 3225 | #endif |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3226 | if (x & 1) { |
| 3227 | count++; |
| 3228 | } |
| 3229 | x >>= 1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3230 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3231 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3232 | } |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3233 | PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4); |
| 3234 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3235 | #endif |
| 3236 | |
| 3237 | #if TIMING || STATS || COUNT_PAGES |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3238 | fflush(stdout); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3239 | #endif |
| 3240 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3241 | TRACE("[ Ready to execute '%s' @ %p ]", si->get_soname(), reinterpret_cast<void*>(si->entry)); |
Dmitriy Ivanov | 6abf624 | 2014-09-12 09:43:13 -0700 | [diff] [blame] | 3242 | return si->entry; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 3243 | } |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 3244 | |
David 'Digit' Turner | bea23e5 | 2012-06-18 23:38:46 +0200 | [diff] [blame] | 3245 | /* Compute the load-bias of an existing executable. This shall only |
| 3246 | * be used to compute the load bias of an executable or shared library |
| 3247 | * that was loaded by the kernel itself. |
| 3248 | * |
| 3249 | * Input: |
| 3250 | * elf -> address of ELF header, assumed to be at the start of the file. |
| 3251 | * Return: |
| 3252 | * load bias, i.e. add the value of any p_vaddr in the file to get |
| 3253 | * the corresponding address in memory. |
| 3254 | */ |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3255 | static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) { |
| 3256 | ElfW(Addr) offset = elf->e_phoff; |
Elliott Hughes | faf05ba | 2014-02-11 16:59:37 -0800 | [diff] [blame] | 3257 | const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset); |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3258 | const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum; |
David 'Digit' Turner | bea23e5 | 2012-06-18 23:38:46 +0200 | [diff] [blame] | 3259 | |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3260 | for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) { |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 3261 | if (phdr->p_type == PT_LOAD) { |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3262 | 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] | 3263 | } |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 3264 | } |
| 3265 | return 0; |
David 'Digit' Turner | bea23e5 | 2012-06-18 23:38:46 +0200 | [diff] [blame] | 3266 | } |
| 3267 | |
Dmitriy Ivanov | efe1383 | 2014-07-28 15:05:51 -0700 | [diff] [blame] | 3268 | extern "C" void _start(); |
| 3269 | |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 3270 | /* |
| 3271 | * This is the entry point for the linker, called from begin.S. This |
| 3272 | * method is responsible for fixing the linker's own relocations, and |
| 3273 | * then calling __linker_init_post_relocation(). |
| 3274 | * |
| 3275 | * Because this method is called before the linker has fixed it's own |
| 3276 | * relocations, any attempt to reference an extern variable, extern |
| 3277 | * function, or other GOT reference will generate a segfault. |
| 3278 | */ |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3279 | extern "C" ElfW(Addr) __linker_init(void* raw_args) { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 3280 | KernelArgumentBlock args(raw_args); |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 3281 | |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3282 | ElfW(Addr) linker_addr = args.getauxval(AT_BASE); |
Dmitriy Ivanov | efe1383 | 2014-07-28 15:05:51 -0700 | [diff] [blame] | 3283 | ElfW(Addr) entry_point = args.getauxval(AT_ENTRY); |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3284 | ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr); |
Elliott Hughes | faf05ba | 2014-02-11 16:59:37 -0800 | [diff] [blame] | 3285 | 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] | 3286 | |
Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 3287 | soinfo linker_so(nullptr, nullptr, 0, 0); |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 3288 | |
Dmitriy Ivanov | efe1383 | 2014-07-28 15:05:51 -0700 | [diff] [blame] | 3289 | // If the linker is not acting as PT_INTERP entry_point is equal to |
| 3290 | // _start. Which means that the linker is running as an executable and |
| 3291 | // already linked by PT_INTERP. |
| 3292 | // |
| 3293 | // This happens when user tries to run 'adb shell /system/bin/linker' |
| 3294 | // see also https://code.google.com/p/android/issues/detail?id=63174 |
| 3295 | if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) { |
| 3296 | __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]); |
| 3297 | } |
| 3298 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 3299 | linker_so.base = linker_addr; |
| 3300 | linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum); |
| 3301 | linker_so.load_bias = get_elf_exec_load_bias(elf_hdr); |
Dmitriy Ivanov | 851135b | 2014-08-29 12:02:36 -0700 | [diff] [blame] | 3302 | linker_so.dynamic = nullptr; |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 3303 | linker_so.phdr = phdr; |
| 3304 | linker_so.phnum = elf_hdr->e_phnum; |
Dmitriy Ivanov | ab972b9 | 2014-11-29 13:57:41 -0800 | [diff] [blame] | 3305 | linker_so.set_linker_flag(); |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 3306 | |
Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 3307 | // This might not be obvious... The reasons why we pass g_empty_list |
| 3308 | // in place of local_group here are (1) we do not really need it, because |
| 3309 | // linker is built with DT_SYMBOLIC and therefore relocates its symbols against |
| 3310 | // itself without having to look into local_group and (2) allocators |
| 3311 | // are not yet initialized, and therefore we cannot use linked_list.push_* |
| 3312 | // functions at this point. |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3313 | if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 3314 | // It would be nice to print an error message, but if the linker |
| 3315 | // can't link itself, there's no guarantee that we'll be able to |
Elliott Hughes | b93702a | 2013-12-21 16:07:45 -0800 | [diff] [blame] | 3316 | // call write() (because it involves a GOT reference). We may as |
| 3317 | // well try though... |
| 3318 | const char* msg = "CANNOT LINK EXECUTABLE: "; |
| 3319 | write(2, msg, strlen(msg)); |
| 3320 | write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf)); |
| 3321 | write(2, "\n", 1); |
| 3322 | _exit(EXIT_FAILURE); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 3323 | } |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 3324 | |
Dmitriy Ivanov | 1424140 | 2014-08-26 14:16:52 -0700 | [diff] [blame] | 3325 | __libc_init_tls(args); |
| 3326 | |
Dmitriy Ivanov | efe1383 | 2014-07-28 15:05:51 -0700 | [diff] [blame] | 3327 | // Initialize the linker's own global variables |
Dmitriy Ivanov | 047b593 | 2014-11-13 09:39:20 -0800 | [diff] [blame] | 3328 | linker_so.call_constructors(); |
Dmitriy Ivanov | 4151ea7 | 2014-07-24 15:33:25 -0700 | [diff] [blame] | 3329 | |
Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 3330 | // Initialize static variables. Note that in order to |
| 3331 | // get correct libdl_info we need to call constructors |
| 3332 | // before get_libdl_info(). |
| 3333 | solist = get_libdl_info(); |
| 3334 | sonext = get_libdl_info(); |
| 3335 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 3336 | // We have successfully fixed our own relocations. It's safe to run |
| 3337 | // the main part of the linker now. |
Elliott Hughes | 1728b23 | 2014-05-14 10:02:03 -0700 | [diff] [blame] | 3338 | args.abort_message_ptr = &g_abort_message; |
Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 3339 | ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 3340 | |
Elliott Hughes | 611f956 | 2015-01-23 10:43:58 -0800 | [diff] [blame] | 3341 | INFO("[ jumping to _start ]"); |
| 3342 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 3343 | // Return the address that the calling assembly stub should jump to. |
| 3344 | return start_address; |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 3345 | } |