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