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