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