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