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