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