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> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 32 | #include <linux/auxvec.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> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 37 | #include <sys/atomics.h> |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 38 | #include <sys/mman.h> |
| 39 | #include <sys/stat.h> |
| 40 | #include <unistd.h> |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 46 | |
| 47 | #include "linker.h" |
| 48 | #include "linker_debug.h" |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 49 | #include "linker_environ.h" |
David 'Digit' Turner | 23363ed | 2012-06-18 18:13:49 +0200 | [diff] [blame] | 50 | #include "linker_phdr.h" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 51 | |
David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 52 | /* Assume average path length of 64 and max 8 paths */ |
| 53 | #define LDPATH_BUFSIZE 512 |
| 54 | #define LDPATH_MAX 8 |
| 55 | |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 56 | #define LDPRELOAD_BUFSIZE 512 |
| 57 | #define LDPRELOAD_MAX 8 |
| 58 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 59 | /* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<< |
| 60 | * |
| 61 | * Do NOT use malloc() and friends or pthread_*() code here. |
| 62 | * Don't use printf() either; it's caused mysterious memory |
| 63 | * corruption in the past. |
| 64 | * The linker runs before we bring up libc and it's easiest |
| 65 | * to make sure it does not depend on any complex libc features |
| 66 | * |
| 67 | * open issues / todo: |
| 68 | * |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 69 | * - are we doing everything we should for ARM_COPY relocations? |
| 70 | * - cleaner error reporting |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 71 | * - after linking, set as much stuff as possible to READONLY |
| 72 | * and NOEXEC |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 73 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 74 | |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 75 | static bool soinfo_link_image(soinfo* si); |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 76 | static Elf_Addr get_elf_exec_load_bias(const Elf_Ehdr* elf); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 77 | |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 78 | // We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous |
| 79 | // maps, each a single page in size. The pages are broken up into as many struct soinfo |
| 80 | // objects as will fit, and they're all threaded together on a free list. |
| 81 | #define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo)) |
| 82 | struct soinfo_pool_t { |
| 83 | soinfo_pool_t* next; |
| 84 | soinfo info[SOINFO_PER_POOL]; |
| 85 | }; |
| 86 | static struct soinfo_pool_t* gSoInfoPools = NULL; |
| 87 | static soinfo* gSoInfoFreeList = NULL; |
| 88 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 89 | static soinfo* solist = &libdl_info; |
| 90 | static soinfo* sonext = &libdl_info; |
| 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 | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 93 | static const char* const gSoPaths[] = { |
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 |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 101 | NULL |
| 102 | }; |
David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 103 | |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 104 | static char gLdPathsBuffer[LDPATH_BUFSIZE]; |
| 105 | static const char* gLdPaths[LDPATH_MAX + 1]; |
| 106 | |
| 107 | static char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE]; |
| 108 | static const char* gLdPreloadNames[LDPRELOAD_MAX + 1]; |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 109 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 110 | static soinfo* gLdPreloads[LDPRELOAD_MAX + 1]; |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 111 | |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 112 | __LIBC_HIDDEN__ int gLdDebugVerbosity; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 113 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 114 | __LIBC_HIDDEN__ abort_msg_t* gAbortMessage = NULL; // For debuggerd. |
| 115 | |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 116 | enum RelocationKind { |
| 117 | kRelocAbsolute = 0, |
| 118 | kRelocRelative, |
| 119 | kRelocCopy, |
| 120 | kRelocSymbol, |
| 121 | kRelocMax |
| 122 | }; |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 123 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 124 | #if STATS |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 125 | struct linker_stats_t { |
| 126 | int count[kRelocMax]; |
| 127 | }; |
| 128 | |
| 129 | static linker_stats_t linker_stats; |
| 130 | |
| 131 | static void count_relocation(RelocationKind kind) { |
| 132 | ++linker_stats.count[kind]; |
| 133 | } |
| 134 | #else |
| 135 | static void count_relocation(RelocationKind) { |
| 136 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 137 | #endif |
| 138 | |
| 139 | #if COUNT_PAGES |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 140 | static unsigned bitmask[4096]; |
| 141 | #define MARK(offset) \ |
| 142 | do { \ |
| 143 | bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \ |
| 144 | } while(0) |
| 145 | #else |
| 146 | #define MARK(x) do {} while (0) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 147 | #endif |
| 148 | |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 149 | // You shouldn't try to call memory-allocating functions in the dynamic linker. |
| 150 | // Guard against the most obvious ones. |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 151 | #define DISALLOW_ALLOCATION(return_type, name, ...) \ |
| 152 | return_type name __VA_ARGS__ \ |
| 153 | { \ |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 154 | const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \ |
Elliott Hughes | 8f2a5a0 | 2013-03-15 15:30:25 -0700 | [diff] [blame] | 155 | __libc_format_log(ANDROID_LOG_FATAL, "linker", "%s", msg); \ |
| 156 | write(2, msg, strlen(msg)); \ |
| 157 | abort(); \ |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 158 | } |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 159 | #define UNUSED __attribute__((unused)) |
| 160 | DISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED)); |
| 161 | DISALLOW_ALLOCATION(void, free, (void* u UNUSED)); |
| 162 | DISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED)); |
| 163 | DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED)); |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 164 | |
Dima Zavin | 0353195 | 2009-05-29 17:30:25 -0700 | [diff] [blame] | 165 | static char tmp_err_buf[768]; |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 166 | static char __linker_dl_err_buf[768]; |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 167 | |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 168 | char* linker_get_error_buffer() { |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 169 | return &__linker_dl_err_buf[0]; |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 170 | } |
| 171 | |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 172 | size_t linker_get_error_buffer_size() { |
| 173 | return sizeof(__linker_dl_err_buf); |
| 174 | } |
| 175 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 176 | /* |
| 177 | * This function is an empty stub where GDB locates a breakpoint to get notified |
| 178 | * about linker activity. |
| 179 | */ |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 180 | 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] | 181 | |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 182 | static r_debug _r_debug = {1, NULL, &rtld_db_dlactivity, RT_CONSISTENT, 0}; |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 183 | static link_map_t* r_debug_tail = 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 184 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 185 | static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 186 | |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 187 | static void insert_soinfo_into_debug_map(soinfo * info) { |
| 188 | // Copy the necessary fields into the debug structure. |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 189 | link_map_t* map = &(info->link_map); |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 190 | map->l_addr = info->load_bias; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 191 | map->l_name = (char*) info->name; |
Thinker K.F Li | 5cf640c | 2009-07-03 19:40:32 +0800 | [diff] [blame] | 192 | map->l_ld = (uintptr_t)info->dynamic; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 193 | |
| 194 | /* Stick the new library at the end of the list. |
| 195 | * gdb tends to care more about libc than it does |
| 196 | * about leaf libraries, and ordering it this way |
| 197 | * reduces the back-and-forth over the wire. |
| 198 | */ |
| 199 | if (r_debug_tail) { |
| 200 | r_debug_tail->l_next = map; |
| 201 | map->l_prev = r_debug_tail; |
| 202 | map->l_next = 0; |
| 203 | } else { |
| 204 | _r_debug.r_map = map; |
| 205 | map->l_prev = 0; |
| 206 | map->l_next = 0; |
| 207 | } |
| 208 | r_debug_tail = map; |
| 209 | } |
| 210 | |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 211 | static void remove_soinfo_from_debug_map(soinfo* info) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 212 | link_map_t* map = &(info->link_map); |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 213 | |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 214 | if (r_debug_tail == map) { |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 215 | r_debug_tail = map->l_prev; |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 216 | } |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 217 | |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 218 | if (map->l_prev) { |
| 219 | map->l_prev->l_next = map->l_next; |
| 220 | } |
| 221 | if (map->l_next) { |
| 222 | map->l_next->l_prev = map->l_prev; |
| 223 | } |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 224 | } |
| 225 | |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 226 | static void notify_gdb_of_load(soinfo* info) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 227 | if (info->flags & FLAG_EXE) { |
| 228 | // GDB already knows about the main executable |
| 229 | return; |
| 230 | } |
| 231 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 232 | ScopedPthreadMutexLocker locker(&gDebugMutex); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 233 | |
| 234 | _r_debug.r_state = RT_ADD; |
| 235 | rtld_db_dlactivity(); |
| 236 | |
| 237 | insert_soinfo_into_debug_map(info); |
| 238 | |
| 239 | _r_debug.r_state = RT_CONSISTENT; |
| 240 | rtld_db_dlactivity(); |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 241 | } |
| 242 | |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 243 | static void notify_gdb_of_unload(soinfo* info) { |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 244 | if (info->flags & FLAG_EXE) { |
| 245 | // GDB already knows about the main executable |
| 246 | return; |
| 247 | } |
| 248 | |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 249 | ScopedPthreadMutexLocker locker(&gDebugMutex); |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 250 | |
| 251 | _r_debug.r_state = RT_DELETE; |
| 252 | rtld_db_dlactivity(); |
| 253 | |
| 254 | remove_soinfo_from_debug_map(info); |
| 255 | |
| 256 | _r_debug.r_state = RT_CONSISTENT; |
| 257 | rtld_db_dlactivity(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 258 | } |
| 259 | |
Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 260 | void notify_gdb_of_libraries() { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 261 | _r_debug.r_state = RT_ADD; |
| 262 | rtld_db_dlactivity(); |
| 263 | _r_debug.r_state = RT_CONSISTENT; |
| 264 | rtld_db_dlactivity(); |
| 265 | } |
| 266 | |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 267 | static bool ensure_free_list_non_empty() { |
| 268 | if (gSoInfoFreeList != NULL) { |
| 269 | return true; |
| 270 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 271 | |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 272 | // Allocate a new pool. |
| 273 | soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool), |
| 274 | PROT_READ|PROT_WRITE, |
| 275 | MAP_PRIVATE|MAP_ANONYMOUS, 0, 0)); |
| 276 | if (pool == MAP_FAILED) { |
| 277 | return false; |
| 278 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 279 | |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 280 | // Add the pool to our list of pools. |
| 281 | pool->next = gSoInfoPools; |
| 282 | gSoInfoPools = pool; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 283 | |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 284 | // Chain the entries in the new pool onto the free list. |
| 285 | gSoInfoFreeList = &pool->info[0]; |
| 286 | soinfo* next = NULL; |
| 287 | for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) { |
| 288 | pool->info[i].next = next; |
| 289 | next = &pool->info[i]; |
| 290 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 291 | |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 292 | return true; |
| 293 | } |
| 294 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 295 | static void set_soinfo_pool_protection(int protection) { |
| 296 | for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) { |
| 297 | if (mprotect(p, sizeof(*p), protection) == -1) { |
| 298 | abort(); // Can't happen. |
| 299 | } |
| 300 | } |
| 301 | } |
| 302 | |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 303 | static soinfo* soinfo_alloc(const char* name) { |
| 304 | if (strlen(name) >= SOINFO_NAME_LEN) { |
| 305 | DL_ERR("library name \"%s\" too long", name); |
| 306 | return NULL; |
| 307 | } |
| 308 | |
| 309 | if (!ensure_free_list_non_empty()) { |
| 310 | DL_ERR("out of memory when loading \"%s\"", name); |
| 311 | return NULL; |
| 312 | } |
| 313 | |
| 314 | // Take the head element off the free list. |
| 315 | soinfo* si = gSoInfoFreeList; |
| 316 | gSoInfoFreeList = gSoInfoFreeList->next; |
| 317 | |
| 318 | // Initialize the new element. |
| 319 | memset(si, 0, sizeof(soinfo)); |
| 320 | strlcpy(si->name, name, sizeof(si->name)); |
| 321 | sonext->next = si; |
| 322 | sonext = si; |
| 323 | |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 324 | TRACE("name %s: allocated soinfo @ %p", name, si); |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 325 | return si; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 326 | } |
| 327 | |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 328 | static void soinfo_free(soinfo* si) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 329 | { |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 330 | if (si == NULL) { |
| 331 | return; |
| 332 | } |
| 333 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 334 | soinfo *prev = NULL, *trav; |
| 335 | |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 336 | TRACE("name %s: freeing soinfo @ %p", si->name, si); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 337 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 338 | for (trav = solist; trav != NULL; trav = trav->next) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 339 | if (trav == si) |
| 340 | break; |
| 341 | prev = trav; |
| 342 | } |
| 343 | if (trav == NULL) { |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 344 | /* si was not in solist */ |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 345 | DL_ERR("name \"%s\" is not in solist!", si->name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 346 | return; |
| 347 | } |
| 348 | |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 349 | /* prev will never be NULL, because the first entry in solist is |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 350 | always the static libdl_info. |
| 351 | */ |
| 352 | prev->next = si->next; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 353 | if (si == sonext) { |
| 354 | sonext = prev; |
| 355 | } |
Magnus Malmborn | ba98d92 | 2012-09-12 13:00:55 +0200 | [diff] [blame] | 356 | si->next = gSoInfoFreeList; |
| 357 | gSoInfoFreeList = si; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 358 | } |
| 359 | |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 360 | |
| 361 | static void parse_path(const char* path, const char* delimiters, |
| 362 | const char** array, char* buf, size_t buf_size, size_t max_count) { |
| 363 | if (path == NULL) { |
| 364 | return; |
| 365 | } |
| 366 | |
| 367 | size_t len = strlcpy(buf, path, buf_size); |
| 368 | |
| 369 | size_t i = 0; |
| 370 | char* buf_p = buf; |
| 371 | while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) { |
| 372 | if (*array[i] != '\0') { |
| 373 | ++i; |
| 374 | } |
| 375 | } |
| 376 | |
| 377 | // Forget the last path if we had to truncate; this occurs if the 2nd to |
| 378 | // last char isn't '\0' (i.e. wasn't originally a delimiter). |
| 379 | if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') { |
| 380 | array[i - 1] = NULL; |
| 381 | } else { |
| 382 | array[i] = NULL; |
| 383 | } |
| 384 | } |
| 385 | |
| 386 | static void parse_LD_LIBRARY_PATH(const char* path) { |
| 387 | parse_path(path, ":", gLdPaths, |
| 388 | gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX); |
| 389 | } |
| 390 | |
| 391 | static void parse_LD_PRELOAD(const char* path) { |
| 392 | // We have historically supported ':' as well as ' ' in LD_PRELOAD. |
| 393 | parse_path(path, " :", gLdPreloadNames, |
| 394 | gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX); |
| 395 | } |
| 396 | |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 397 | #if defined(__arm__) |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 398 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 399 | /* For a given PC, find the .so that it belongs to. |
| 400 | * Returns the base address of the .ARM.exidx section |
| 401 | * for that .so, and the number of 8-byte entries |
| 402 | * in that section (via *pcount). |
| 403 | * |
| 404 | * Intended to be called by libc's __gnu_Unwind_Find_exidx(). |
| 405 | * |
Elliott Hughes | 3b297c4 | 2012-10-11 16:08:51 -0700 | [diff] [blame] | 406 | * This function is exposed via dlfcn.cpp and libdl.so. |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 407 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 408 | _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount) |
| 409 | { |
| 410 | soinfo *si; |
| 411 | unsigned addr = (unsigned)pc; |
| 412 | |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 413 | for (si = solist; si != 0; si = si->next) { |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 414 | if ((addr >= si->base) && (addr < (si->base + si->size))) { |
| 415 | *pcount = si->ARM_exidx_count; |
Ji-Hwan Lee | f186a18 | 2012-05-31 20:20:36 +0900 | [diff] [blame] | 416 | return (_Unwind_Ptr)si->ARM_exidx; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 417 | } |
| 418 | } |
| 419 | *pcount = 0; |
| 420 | return NULL; |
| 421 | } |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 422 | |
Christopher Ferris | 24053a4 | 2013-08-19 17:45:09 -0700 | [diff] [blame] | 423 | #endif |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 424 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 425 | /* Here, we only have to provide a callback to iterate across all the |
| 426 | * loaded libraries. gcc_eh does the rest. */ |
| 427 | int |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 428 | dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data), |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 429 | void *data) |
| 430 | { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 431 | int rv = 0; |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 432 | for (soinfo* si = solist; si != NULL; si = si->next) { |
| 433 | dl_phdr_info dl_info; |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 434 | dl_info.dlpi_addr = si->link_map.l_addr; |
| 435 | dl_info.dlpi_name = si->link_map.l_name; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 436 | dl_info.dlpi_phdr = si->phdr; |
| 437 | dl_info.dlpi_phnum = si->phnum; |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 438 | rv = cb(&dl_info, sizeof(dl_phdr_info), data); |
| 439 | if (rv != 0) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 440 | break; |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 441 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 442 | } |
| 443 | return rv; |
| 444 | } |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 445 | |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 446 | static Elf_Sym* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) { |
| 447 | Elf_Sym* symtab = si->symtab; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 448 | const char* strtab = si->strtab; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 449 | |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 450 | TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd", |
| 451 | 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] | 452 | |
Christopher Ferris | 6bec5b7 | 2013-06-03 17:27:49 -0700 | [diff] [blame] | 453 | for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) { |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 454 | Elf_Sym* s = symtab + n; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 455 | if (strcmp(strtab + s->st_name, name)) continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 456 | |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 457 | /* only concern ourselves with global and weak symbol definitions */ |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 458 | switch (ELF_ST_BIND(s->st_info)) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 459 | case STB_GLOBAL: |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 460 | case STB_WEAK: |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 461 | if (s->st_shndx == SHN_UNDEF) { |
Robin Burchell | 439fa8e | 2012-07-05 09:21:07 +0200 | [diff] [blame] | 462 | continue; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 463 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 464 | |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 465 | TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd", |
| 466 | name, si->name, reinterpret_cast<void*>(s->st_value), |
| 467 | static_cast<size_t>(s->st_size)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 468 | return s; |
| 469 | } |
| 470 | } |
| 471 | |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 472 | return NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 473 | } |
| 474 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 475 | static unsigned elfhash(const char* _name) { |
| 476 | const unsigned char* name = (const unsigned char*) _name; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 477 | unsigned h = 0, g; |
| 478 | |
| 479 | while(*name) { |
| 480 | h = (h << 4) + *name++; |
| 481 | g = h & 0xf0000000; |
| 482 | h ^= g; |
| 483 | h ^= g >> 24; |
| 484 | } |
| 485 | return h; |
| 486 | } |
| 487 | |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 488 | static Elf_Sym* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) { |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 489 | unsigned elf_hash = elfhash(name); |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 490 | Elf_Sym* s = NULL; |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 491 | |
Pavel Chupin | c77c434 | 2012-10-31 13:55:51 +0400 | [diff] [blame] | 492 | if (si != NULL && somain != NULL) { |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 493 | |
| 494 | /* |
Pavel Chupin | c77c434 | 2012-10-31 13:55:51 +0400 | [diff] [blame] | 495 | * Local scope is executable scope. Just start looking into it right away |
| 496 | * for the shortcut. |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 497 | */ |
| 498 | |
Pavel Chupin | c77c434 | 2012-10-31 13:55:51 +0400 | [diff] [blame] | 499 | if (si == somain) { |
| 500 | s = soinfo_elf_lookup(si, elf_hash, name); |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 501 | if (s != NULL) { |
Pavel Chupin | c77c434 | 2012-10-31 13:55:51 +0400 | [diff] [blame] | 502 | *lsi = si; |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 503 | goto done; |
| 504 | } |
Pavel Chupin | c77c434 | 2012-10-31 13:55:51 +0400 | [diff] [blame] | 505 | } else { |
| 506 | /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */ |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 507 | |
Pavel Chupin | c77c434 | 2012-10-31 13:55:51 +0400 | [diff] [blame] | 508 | /* |
| 509 | * If this object was built with symbolic relocations disabled, the |
| 510 | * first place to look to resolve external references is the main |
| 511 | * executable. |
| 512 | */ |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 513 | |
Pavel Chupin | c77c434 | 2012-10-31 13:55:51 +0400 | [diff] [blame] | 514 | if (!si->has_DT_SYMBOLIC) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 515 | DEBUG("%s: looking up %s in executable %s", |
Elliott Hughes | 61a9ccb | 2012-11-02 12:37:13 -0700 | [diff] [blame] | 516 | si->name, name, somain->name); |
Pavel Chupin | c77c434 | 2012-10-31 13:55:51 +0400 | [diff] [blame] | 517 | s = soinfo_elf_lookup(somain, elf_hash, name); |
| 518 | if (s != NULL) { |
| 519 | *lsi = somain; |
| 520 | goto done; |
| 521 | } |
| 522 | } |
| 523 | |
| 524 | /* Look for symbols in the local scope (the object who is |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 525 | * searching). This happens with C++ templates on x86 for some |
Pavel Chupin | c77c434 | 2012-10-31 13:55:51 +0400 | [diff] [blame] | 526 | * reason. |
| 527 | * |
| 528 | * Notes on weak symbols: |
| 529 | * The ELF specs are ambiguous about treatment of weak definitions in |
| 530 | * dynamic linking. Some systems return the first definition found |
| 531 | * and some the first non-weak definition. This is system dependent. |
| 532 | * Here we return the first definition found for simplicity. */ |
| 533 | |
| 534 | s = soinfo_elf_lookup(si, elf_hash, name); |
| 535 | if (s != NULL) { |
| 536 | *lsi = si; |
| 537 | goto done; |
| 538 | } |
| 539 | |
| 540 | /* |
| 541 | * If this object was built with -Bsymbolic and symbol is not found |
| 542 | * in the local scope, try to find the symbol in the main executable. |
| 543 | */ |
| 544 | |
| 545 | if (si->has_DT_SYMBOLIC) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 546 | DEBUG("%s: looking up %s in executable %s after local scope", |
Elliott Hughes | 61a9ccb | 2012-11-02 12:37:13 -0700 | [diff] [blame] | 547 | si->name, name, somain->name); |
Pavel Chupin | c77c434 | 2012-10-31 13:55:51 +0400 | [diff] [blame] | 548 | s = soinfo_elf_lookup(somain, elf_hash, name); |
| 549 | if (s != NULL) { |
| 550 | *lsi = somain; |
| 551 | goto done; |
| 552 | } |
| 553 | } |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 554 | } |
Nick Kralevich | d39c3ab | 2012-08-24 13:25:51 -0700 | [diff] [blame] | 555 | } |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 556 | |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 557 | /* Next, look for it in the preloads list */ |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 558 | for (int i = 0; gLdPreloads[i] != NULL; i++) { |
| 559 | s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name); |
| 560 | if (s != NULL) { |
| 561 | *lsi = gLdPreloads[i]; |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 562 | goto done; |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 563 | } |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 564 | } |
| 565 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 566 | for (int i = 0; needed[i] != NULL; i++) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 567 | DEBUG("%s: looking up %s in %s", |
Elliott Hughes | 61a9ccb | 2012-11-02 12:37:13 -0700 | [diff] [blame] | 568 | si->name, name, needed[i]->name); |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 569 | s = soinfo_elf_lookup(needed[i], elf_hash, name); |
| 570 | if (s != NULL) { |
| 571 | *lsi = needed[i]; |
Ard Biesheuvel | 12c78bb | 2012-08-14 12:30:09 +0200 | [diff] [blame] | 572 | goto done; |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 573 | } |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 574 | } |
| 575 | |
| 576 | done: |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 577 | if (s != NULL) { |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 578 | TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, " |
| 579 | "found in %s, base = %p, load bias = %p", |
| 580 | si->name, name, reinterpret_cast<void*>(s->st_value), |
| 581 | (*lsi)->name, reinterpret_cast<void*>((*lsi)->base), |
| 582 | reinterpret_cast<void*>((*lsi)->load_bias)); |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 583 | return s; |
| 584 | } |
| 585 | |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 586 | return NULL; |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 587 | } |
| 588 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 589 | /* This is used by dlsym(3). It performs symbol lookup only within the |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 590 | specified soinfo object and not in any of its dependencies. |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 591 | |
| 592 | TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says |
| 593 | that it should do a breadth first search through the dependency |
| 594 | tree. This agrees with the ELF spec (aka System V Application |
| 595 | Binary Interface) where in Chapter 5 it discuss resolving "Shared |
| 596 | Object Dependencies" in breadth first search order. |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 597 | */ |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 598 | Elf_Sym* dlsym_handle_lookup(soinfo* si, const char* name) { |
David 'Digit' Turner | 1608416 | 2012-06-12 16:25:37 +0200 | [diff] [blame] | 599 | return soinfo_elf_lookup(si, elfhash(name), name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 600 | } |
| 601 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 602 | /* This is used by dlsym(3) to performs a global symbol lookup. If the |
| 603 | start value is null (for RTLD_DEFAULT), the search starts at the |
| 604 | beginning of the global solist. Otherwise the search starts at the |
| 605 | specified soinfo (for RTLD_NEXT). |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 606 | */ |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 607 | Elf_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) { |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 608 | unsigned elf_hash = elfhash(name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 609 | |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 610 | if (start == NULL) { |
| 611 | start = solist; |
| 612 | } |
| 613 | |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 614 | Elf_Sym* s = NULL; |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 615 | for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) { |
| 616 | s = soinfo_elf_lookup(si, elf_hash, name); |
| 617 | if (s != NULL) { |
| 618 | *found = si; |
| 619 | break; |
Matt Fischer | 1698d9e | 2009-12-31 12:17:56 -0600 | [diff] [blame] | 620 | } |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 621 | } |
Matt Fischer | 1698d9e | 2009-12-31 12:17:56 -0600 | [diff] [blame] | 622 | |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 623 | if (s != NULL) { |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 624 | TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p", |
| 625 | name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base)); |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 626 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 627 | |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 628 | return s; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 629 | } |
| 630 | |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 631 | soinfo* find_containing_library(const void* p) { |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 632 | Elf_Addr address = reinterpret_cast<Elf_Addr>(p); |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 633 | for (soinfo* si = solist; si != NULL; si = si->next) { |
| 634 | if (address >= si->base && address - si->base < si->size) { |
| 635 | return si; |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 636 | } |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 637 | } |
| 638 | return NULL; |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 639 | } |
| 640 | |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 641 | Elf_Sym* dladdr_find_symbol(soinfo* si, const void* addr) { |
| 642 | Elf_Addr soaddr = reinterpret_cast<Elf_Addr>(addr) - si->base; |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 643 | |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 644 | // Search the library's symbol table for any defined symbol which |
| 645 | // contains this address. |
| 646 | for (size_t i = 0; i < si->nchain; ++i) { |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 647 | Elf_Sym* sym = &si->symtab[i]; |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 648 | if (sym->st_shndx != SHN_UNDEF && |
| 649 | soaddr >= sym->st_value && |
| 650 | soaddr < sym->st_value + sym->st_size) { |
| 651 | return sym; |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 652 | } |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 653 | } |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 654 | |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 655 | return NULL; |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 656 | } |
| 657 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 658 | #if 0 |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 659 | static void dump(soinfo* si) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 660 | { |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 661 | Elf_Sym* s = si->symtab; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 662 | for (unsigned n = 0; n < si->nchain; n++) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 663 | TRACE("%04d> %08x: %02x %04x %08x %08x %s", n, s, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 664 | s->st_info, s->st_shndx, s->st_value, s->st_size, |
| 665 | si->strtab + s->st_name); |
| 666 | s++; |
| 667 | } |
| 668 | } |
| 669 | #endif |
| 670 | |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 671 | static int open_library_on_path(const char* name, const char* const paths[]) { |
| 672 | char buf[512]; |
| 673 | for (size_t i = 0; paths[i] != NULL; ++i) { |
Elliott Hughes | 1e980b6 | 2013-01-17 18:36:06 -0800 | [diff] [blame] | 674 | 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] | 675 | if (n < 0 || n >= static_cast<int>(sizeof(buf))) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 676 | PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name); |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 677 | continue; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 678 | } |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 679 | int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC)); |
| 680 | if (fd != -1) { |
| 681 | return fd; |
| 682 | } |
| 683 | } |
| 684 | return -1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 685 | } |
| 686 | |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 687 | static int open_library(const char* name) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 688 | TRACE("[ opening %s ]", name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 689 | |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 690 | // If the name contains a slash, we should attempt to open it directly and not search the paths. |
| 691 | if (strchr(name, '/') != NULL) { |
Elliott Hughes | 6971fe4 | 2012-11-01 22:59:19 -0700 | [diff] [blame] | 692 | int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC)); |
| 693 | if (fd != -1) { |
| 694 | return fd; |
| 695 | } |
| 696 | // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now. |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 697 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 698 | |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 699 | // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths. |
| 700 | int fd = open_library_on_path(name, gLdPaths); |
| 701 | if (fd == -1) { |
| 702 | fd = open_library_on_path(name, gSoPaths); |
| 703 | } |
| 704 | return fd; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 705 | } |
| 706 | |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 707 | static soinfo* load_library(const char* name) { |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 708 | // Open the file. |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 709 | int fd = open_library(name); |
| 710 | if (fd == -1) { |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 711 | DL_ERR("library \"%s\" not found", name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 712 | return NULL; |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 713 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 714 | |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 715 | // Read the ELF header and load the segments. |
| 716 | ElfReader elf_reader(name, fd); |
| 717 | if (!elf_reader.Load()) { |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 718 | return NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 719 | } |
| 720 | |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 721 | const char* bname = strrchr(name, '/'); |
| 722 | soinfo* si = soinfo_alloc(bname ? bname + 1 : name); |
| 723 | if (si == NULL) { |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 724 | return NULL; |
David 'Digit' Turner | 23363ed | 2012-06-18 18:13:49 +0200 | [diff] [blame] | 725 | } |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 726 | si->base = elf_reader.load_start(); |
| 727 | si->size = elf_reader.load_size(); |
| 728 | si->load_bias = elf_reader.load_bias(); |
| 729 | si->flags = 0; |
| 730 | si->entry = 0; |
| 731 | si->dynamic = NULL; |
| 732 | si->phnum = elf_reader.phdr_count(); |
| 733 | si->phdr = elf_reader.loaded_phdr(); |
| 734 | return si; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 735 | } |
| 736 | |
Ard Biesheuvel | 12c78bb | 2012-08-14 12:30:09 +0200 | [diff] [blame] | 737 | static soinfo *find_loaded_library(const char *name) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 738 | { |
| 739 | soinfo *si; |
David 'Digit' Turner | 6774809 | 2010-07-21 16:18:21 -0700 | [diff] [blame] | 740 | const char *bname; |
| 741 | |
Ard Biesheuvel | 12c78bb | 2012-08-14 12:30:09 +0200 | [diff] [blame] | 742 | // TODO: don't use basename only for determining libraries |
| 743 | // http://code.google.com/p/android/issues/detail?id=6670 |
| 744 | |
| 745 | bname = strrchr(name, '/'); |
| 746 | bname = bname ? bname + 1 : name; |
| 747 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 748 | for (si = solist; si != NULL; si = si->next) { |
| 749 | if (!strcmp(bname, si->name)) { |
Ard Biesheuvel | 12c78bb | 2012-08-14 12:30:09 +0200 | [diff] [blame] | 750 | return si; |
| 751 | } |
| 752 | } |
| 753 | return NULL; |
| 754 | } |
| 755 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 756 | static soinfo* find_library_internal(const char* name) { |
| 757 | if (name == NULL) { |
| 758 | return somain; |
| 759 | } |
Ard Biesheuvel | 12c78bb | 2012-08-14 12:30:09 +0200 | [diff] [blame] | 760 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 761 | soinfo* si = find_loaded_library(name); |
| 762 | if (si != NULL) { |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 763 | if (si->flags & FLAG_LINKED) { |
| 764 | return si; |
| 765 | } |
| 766 | DL_ERR("OOPS: recursive link to \"%s\"", si->name); |
| 767 | return NULL; |
| 768 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 769 | |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 770 | TRACE("[ '%s' has not been loaded yet. Locating...]", name); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 771 | si = load_library(name); |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 772 | if (si == NULL) { |
| 773 | return NULL; |
| 774 | } |
| 775 | |
| 776 | // At this point we know that whatever is loaded @ base is a valid ELF |
| 777 | // shared library whose segments are properly mapped in. |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 778 | TRACE("[ init_library base=%p sz=0x%08x name='%s' ]", |
| 779 | reinterpret_cast<void*>(si->base), si->size, si->name); |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 780 | |
| 781 | if (!soinfo_link_image(si)) { |
| 782 | munmap(reinterpret_cast<void*>(si->base), si->size); |
| 783 | soinfo_free(si); |
| 784 | return NULL; |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 785 | } |
| 786 | |
| 787 | return si; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 788 | } |
| 789 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 790 | static soinfo* find_library(const char* name) { |
| 791 | soinfo* si = find_library_internal(name); |
| 792 | if (si != NULL) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 793 | si->ref_count++; |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 794 | } |
| 795 | return si; |
| 796 | } |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 797 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 798 | static int soinfo_unload(soinfo* si) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 799 | if (si->ref_count == 1) { |
| 800 | TRACE("unloading '%s'", si->name); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 801 | si->CallDestructors(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 802 | |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 803 | for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) { |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 804 | if (d->d_tag == DT_NEEDED) { |
| 805 | const char* library_name = si->strtab + d->d_un.d_val; |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 806 | TRACE("%s needs to unload %s", si->name, library_name); |
| 807 | soinfo_unload(find_loaded_library(library_name)); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 808 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 809 | } |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 810 | |
| 811 | munmap(reinterpret_cast<void*>(si->base), si->size); |
| 812 | notify_gdb_of_unload(si); |
| 813 | soinfo_free(si); |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 814 | si->ref_count = 0; |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 815 | } else { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 816 | si->ref_count--; |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 817 | 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] | 818 | } |
| 819 | return 0; |
| 820 | } |
| 821 | |
Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 822 | void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) { |
| 823 | if (!get_AT_SECURE()) { |
| 824 | parse_LD_LIBRARY_PATH(ld_library_path); |
| 825 | } |
| 826 | } |
| 827 | |
Elliott Hughes | e66190d | 2012-12-18 15:57:55 -0800 | [diff] [blame] | 828 | soinfo* do_dlopen(const char* name, int flags) { |
| 829 | if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) { |
| 830 | DL_ERR("invalid flags to dlopen: %x", flags); |
| 831 | return NULL; |
| 832 | } |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 833 | set_soinfo_pool_protection(PROT_READ | PROT_WRITE); |
| 834 | soinfo* si = find_library(name); |
| 835 | if (si != NULL) { |
| 836 | si->CallConstructors(); |
| 837 | } |
| 838 | set_soinfo_pool_protection(PROT_READ); |
| 839 | return si; |
| 840 | } |
| 841 | |
| 842 | int do_dlclose(soinfo* si) { |
| 843 | set_soinfo_pool_protection(PROT_READ | PROT_WRITE); |
| 844 | int result = soinfo_unload(si); |
| 845 | set_soinfo_pool_protection(PROT_READ); |
| 846 | return result; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 847 | } |
| 848 | |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 849 | #if defined(USE_RELA) |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 850 | static int soinfo_relocate_a(soinfo* si, Elf_Rela* rela, unsigned count, soinfo* needed[]) { |
| 851 | Elf_Sym* symtab = si->symtab; |
| 852 | const char* strtab = si->strtab; |
| 853 | Elf_Sym* s; |
| 854 | Elf_Rela* start = rela; |
| 855 | soinfo* lsi; |
| 856 | |
| 857 | for (size_t idx = 0; idx < count; ++idx, ++rela) { |
| 858 | unsigned type = ELF_R_TYPE(rela->r_info); |
| 859 | unsigned sym = ELF_R_SYM(rela->r_info); |
| 860 | Elf_Addr reloc = static_cast<Elf_Addr>(rela->r_offset + si->load_bias); |
| 861 | Elf_Addr sym_addr = 0; |
| 862 | char* sym_name = NULL; |
| 863 | |
| 864 | DEBUG("Processing '%s' relocation at index %zd", si->name, idx); |
| 865 | if (type == 0) { // R_*_NONE |
| 866 | continue; |
| 867 | } |
| 868 | if (sym != 0) { |
| 869 | sym_name = (char *)(strtab + symtab[sym].st_name); |
| 870 | s = soinfo_do_lookup(si, sym_name, &lsi, needed); |
| 871 | if (s == NULL) { |
| 872 | // We only allow an undefined symbol if this is a weak reference... |
| 873 | s = &symtab[sym]; |
| 874 | if (ELF_ST_BIND(s->st_info) != STB_WEAK) { |
| 875 | DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name); |
| 876 | return -1; |
| 877 | } |
| 878 | |
| 879 | /* IHI0044C AAELF 4.5.1.1: |
| 880 | |
| 881 | Libraries are not searched to resolve weak references. |
| 882 | It is not an error for a weak reference to remain unsatisfied. |
| 883 | |
| 884 | During linking, the value of an undefined weak reference is: |
| 885 | - Zero if the relocation type is absolute |
| 886 | - The address of the place if the relocation is pc-relative |
| 887 | - The address of nominal base address if the relocation |
| 888 | type is base-relative. |
| 889 | */ |
| 890 | |
| 891 | switch (type) { |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 892 | #if defined(__x86_64__) |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 893 | case R_X86_64_JUMP_SLOT: |
| 894 | case R_X86_64_GLOB_DAT: |
| 895 | case R_X86_64_32: |
| 896 | case R_X86_64_RELATIVE: |
| 897 | // No need to do anything. |
| 898 | break; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 899 | case R_X86_64_PC32: |
| 900 | sym_addr = reloc; |
| 901 | break; |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 902 | #endif |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 903 | |
| 904 | default: |
| 905 | DL_ERR("unknown weak reloc type %d @ %p (%d)", type, rela, (int) (rela - start)); |
| 906 | return -1; |
| 907 | } |
| 908 | } else { |
| 909 | // We got a definition. |
| 910 | sym_addr = static_cast<Elf_Addr>(s->st_value + lsi->load_bias); |
| 911 | } |
| 912 | count_relocation(kRelocSymbol); |
| 913 | } else { |
| 914 | s = NULL; |
| 915 | } |
| 916 | |
| 917 | switch (type) { |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 918 | #if defined(__x86_64__) |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 919 | case R_X86_64_JUMP_SLOT: |
| 920 | count_relocation(kRelocAbsolute); |
| 921 | MARK(rela->r_offset); |
| 922 | TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc), |
| 923 | static_cast<size_t>(sym_addr + rela->r_addend), sym_name); |
| 924 | *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend; |
| 925 | break; |
| 926 | case R_X86_64_GLOB_DAT: |
| 927 | count_relocation(kRelocAbsolute); |
| 928 | MARK(rela->r_offset); |
| 929 | TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc), |
| 930 | static_cast<size_t>(sym_addr + rela->r_addend), sym_name); |
| 931 | *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend; |
| 932 | break; |
| 933 | case R_X86_64_RELATIVE: |
| 934 | count_relocation(kRelocRelative); |
| 935 | MARK(rela->r_offset); |
| 936 | if (sym) { |
| 937 | DL_ERR("odd RELATIVE form..."); |
| 938 | return -1; |
| 939 | } |
| 940 | TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc), |
| 941 | static_cast<size_t>(si->base)); |
| 942 | *reinterpret_cast<Elf_Addr*>(reloc) = si->base + rela->r_addend; |
| 943 | break; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 944 | case R_X86_64_32: |
| 945 | count_relocation(kRelocRelative); |
| 946 | MARK(rela->r_offset); |
| 947 | TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc), |
| 948 | static_cast<size_t>(sym_addr), sym_name); |
| 949 | *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend; |
| 950 | break; |
Pavel Chupin | c075c18 | 2013-10-16 19:13:58 +0400 | [diff] [blame] | 951 | case R_X86_64_64: |
| 952 | count_relocation(kRelocRelative); |
| 953 | MARK(rela->r_offset); |
| 954 | TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc), |
| 955 | static_cast<size_t>(sym_addr), sym_name); |
| 956 | *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend; |
| 957 | break; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 958 | case R_X86_64_PC32: |
| 959 | count_relocation(kRelocRelative); |
| 960 | MARK(rela->r_offset); |
| 961 | TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s", |
| 962 | static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc), |
| 963 | static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name); |
| 964 | *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend - reloc; |
| 965 | break; |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 966 | #endif |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 967 | default: |
| 968 | DL_ERR("unknown reloc type %d @ %p (%d)", type, rela, (int) (rela - start)); |
| 969 | return -1; |
| 970 | } |
| 971 | } |
| 972 | return 0; |
| 973 | } |
| 974 | #else |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 975 | static int soinfo_relocate(soinfo* si, Elf_Rel* rel, unsigned count, |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 976 | soinfo* needed[]) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 977 | { |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 978 | Elf_Sym* symtab = si->symtab; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 979 | const char* strtab = si->strtab; |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 980 | Elf_Sym* s; |
| 981 | Elf_Rel* start = rel; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 982 | soinfo* lsi; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 983 | |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 984 | for (size_t idx = 0; idx < count; ++idx, ++rel) { |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 985 | unsigned type = ELF_R_TYPE(rel->r_info); |
| 986 | // TODO: don't use unsigned for 'sym'. Use uint32_t or Elf_Addr instead. |
| 987 | unsigned sym = ELF_R_SYM(rel->r_info); |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 988 | Elf_Addr reloc = static_cast<Elf_Addr>(rel->r_offset + si->load_bias); |
| 989 | Elf_Addr sym_addr = 0; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 990 | char* sym_name = NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 991 | |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 992 | DEBUG("Processing '%s' relocation at index %zd", si->name, idx); |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 993 | if (type == 0) { // R_*_NONE |
| 994 | continue; |
| 995 | } |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 996 | if (sym != 0) { |
Dima Zavin | d1b40d8 | 2009-05-12 10:59:09 -0700 | [diff] [blame] | 997 | sym_name = (char *)(strtab + symtab[sym].st_name); |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 998 | s = soinfo_do_lookup(si, sym_name, &lsi, needed); |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 999 | if (s == NULL) { |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1000 | // We only allow an undefined symbol if this is a weak reference... |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1001 | s = &symtab[sym]; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1002 | if (ELF_ST_BIND(s->st_info) != STB_WEAK) { |
Elliott Hughes | e9b6fc6 | 2012-08-29 13:10:54 -0700 | [diff] [blame] | 1003 | DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name); |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1004 | return -1; |
| 1005 | } |
| 1006 | |
| 1007 | /* IHI0044C AAELF 4.5.1.1: |
| 1008 | |
| 1009 | Libraries are not searched to resolve weak references. |
| 1010 | It is not an error for a weak reference to remain |
| 1011 | unsatisfied. |
| 1012 | |
| 1013 | During linking, the value of an undefined weak reference is: |
| 1014 | - Zero if the relocation type is absolute |
| 1015 | - The address of the place if the relocation is pc-relative |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 1016 | - The address of nominal base address if the relocation |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1017 | type is base-relative. |
| 1018 | */ |
| 1019 | |
| 1020 | switch (type) { |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1021 | #if defined(__arm__) |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1022 | case R_ARM_JUMP_SLOT: |
| 1023 | case R_ARM_GLOB_DAT: |
| 1024 | case R_ARM_ABS32: |
| 1025 | case R_ARM_RELATIVE: /* Don't care. */ |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1026 | // sym_addr was initialized to be zero above or relocation |
| 1027 | // code below does not care about value of sym_addr. |
| 1028 | // No need to do anything. |
| 1029 | break; |
| 1030 | #elif defined(__i386__) |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1031 | case R_386_JMP_SLOT: |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1032 | case R_386_GLOB_DAT: |
| 1033 | case R_386_32: |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1034 | case R_386_RELATIVE: /* Don't care. */ |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1035 | // sym_addr was initialized to be zero above or relocation |
| 1036 | // code below does not care about value of sym_addr. |
| 1037 | // No need to do anything. |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1038 | break; |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1039 | case R_386_PC32: |
| 1040 | sym_addr = reloc; |
| 1041 | break; |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1042 | #endif |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1043 | |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1044 | #if defined(__arm__) |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1045 | case R_ARM_COPY: |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1046 | // Fall through. Can't really copy if weak symbol is not found at run-time. |
| 1047 | #endif |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1048 | default: |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1049 | DL_ERR("unknown weak reloc type %d @ %p (%d)", type, rel, (int) (rel - start)); |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1050 | return -1; |
| 1051 | } |
| 1052 | } else { |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1053 | // We got a definition. |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1054 | sym_addr = static_cast<Elf_Addr>(s->st_value + lsi->load_bias); |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1055 | } |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 1056 | count_relocation(kRelocSymbol); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1057 | } else { |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1058 | s = NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1059 | } |
| 1060 | |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1061 | switch (type) { |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1062 | #if defined(__arm__) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1063 | case R_ARM_JUMP_SLOT: |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 1064 | count_relocation(kRelocAbsolute); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1065 | MARK(rel->r_offset); |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1066 | TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name); |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1067 | *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1068 | break; |
| 1069 | case R_ARM_GLOB_DAT: |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 1070 | count_relocation(kRelocAbsolute); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1071 | MARK(rel->r_offset); |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1072 | TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name); |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1073 | *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1074 | break; |
| 1075 | case R_ARM_ABS32: |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 1076 | count_relocation(kRelocAbsolute); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1077 | MARK(rel->r_offset); |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1078 | TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name); |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1079 | *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1080 | break; |
David 'Digit' Turner | 34ea511 | 2009-11-17 14:56:26 -0800 | [diff] [blame] | 1081 | case R_ARM_REL32: |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 1082 | count_relocation(kRelocRelative); |
David 'Digit' Turner | 34ea511 | 2009-11-17 14:56:26 -0800 | [diff] [blame] | 1083 | MARK(rel->r_offset); |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1084 | TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s", |
David 'Digit' Turner | 34ea511 | 2009-11-17 14:56:26 -0800 | [diff] [blame] | 1085 | reloc, sym_addr, rel->r_offset, sym_name); |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1086 | *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr - rel->r_offset; |
David 'Digit' Turner | 34ea511 | 2009-11-17 14:56:26 -0800 | [diff] [blame] | 1087 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1088 | case R_ARM_COPY: |
Nick Kralevich | d39c3ab | 2012-08-24 13:25:51 -0700 | [diff] [blame] | 1089 | if ((si->flags & FLAG_EXE) == 0) { |
| 1090 | /* |
| 1091 | * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf |
| 1092 | * |
| 1093 | * Section 4.7.1.10 "Dynamic relocations" |
| 1094 | * R_ARM_COPY may only appear in executable objects where e_type is |
| 1095 | * set to ET_EXEC. |
| 1096 | * |
| 1097 | * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables. |
| 1098 | * We should explicitly disallow ET_DYN executables from having |
| 1099 | * R_ARM_COPY relocations. |
| 1100 | */ |
| 1101 | DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name); |
| 1102 | return -1; |
| 1103 | } |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 1104 | count_relocation(kRelocCopy); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1105 | MARK(rel->r_offset); |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1106 | TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s", reloc, s->st_size, sym_addr, sym_name); |
Nick Kralevich | d39c3ab | 2012-08-24 13:25:51 -0700 | [diff] [blame] | 1107 | if (reloc == sym_addr) { |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1108 | Elf_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed); |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 1109 | |
| 1110 | if (src == NULL) { |
| 1111 | DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name); |
| 1112 | return -1; |
| 1113 | } |
| 1114 | if (lsi->has_DT_SYMBOLIC) { |
| 1115 | DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared " |
| 1116 | "library %s (built with -Bsymbolic?)", si->name, lsi->name); |
| 1117 | return -1; |
| 1118 | } |
| 1119 | if (s->st_size < src->st_size) { |
| 1120 | DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)", |
| 1121 | si->name, s->st_size, src->st_size); |
| 1122 | return -1; |
| 1123 | } |
| 1124 | memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size); |
| 1125 | } else { |
| 1126 | DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name); |
Nick Kralevich | d39c3ab | 2012-08-24 13:25:51 -0700 | [diff] [blame] | 1127 | return -1; |
| 1128 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1129 | break; |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1130 | #elif defined(__i386__) |
| 1131 | case R_386_JMP_SLOT: |
| 1132 | count_relocation(kRelocAbsolute); |
| 1133 | MARK(rel->r_offset); |
| 1134 | TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name); |
| 1135 | *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr; |
| 1136 | break; |
| 1137 | case R_386_GLOB_DAT: |
| 1138 | count_relocation(kRelocAbsolute); |
| 1139 | MARK(rel->r_offset); |
| 1140 | TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name); |
| 1141 | *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr; |
| 1142 | break; |
| 1143 | case R_386_32: |
| 1144 | count_relocation(kRelocRelative); |
| 1145 | MARK(rel->r_offset); |
| 1146 | TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name); |
| 1147 | *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr; |
| 1148 | break; |
| 1149 | case R_386_PC32: |
| 1150 | count_relocation(kRelocRelative); |
| 1151 | MARK(rel->r_offset); |
| 1152 | TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s", |
| 1153 | reloc, (sym_addr - reloc), sym_addr, reloc, sym_name); |
| 1154 | *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr - reloc); |
| 1155 | break; |
| 1156 | #elif defined(__mips__) |
| 1157 | case R_MIPS_REL32: |
| 1158 | count_relocation(kRelocAbsolute); |
| 1159 | MARK(rel->r_offset); |
| 1160 | TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s", |
| 1161 | reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*"); |
| 1162 | if (s) { |
| 1163 | *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr; |
| 1164 | } else { |
| 1165 | *reinterpret_cast<Elf_Addr*>(reloc) += si->base; |
| 1166 | } |
| 1167 | break; |
| 1168 | #endif |
| 1169 | |
| 1170 | #if defined(__arm__) |
| 1171 | case R_ARM_RELATIVE: |
| 1172 | #elif defined(__i386__) |
| 1173 | case R_386_RELATIVE: |
| 1174 | #endif |
| 1175 | count_relocation(kRelocRelative); |
| 1176 | MARK(rel->r_offset); |
| 1177 | if (sym) { |
| 1178 | DL_ERR("odd RELATIVE form..."); |
| 1179 | return -1; |
| 1180 | } |
| 1181 | TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p", |
| 1182 | reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base)); |
| 1183 | *reinterpret_cast<Elf_Addr*>(reloc) += si->base; |
| 1184 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1185 | |
| 1186 | default: |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1187 | DL_ERR("unknown reloc type %d @ %p (%d)", type, rel, (int) (rel - start)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1188 | return -1; |
| 1189 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1190 | } |
| 1191 | return 0; |
| 1192 | } |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1193 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1194 | |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1195 | #if defined(__mips__) |
Brian Carlstrom | 87c3585 | 2013-08-20 21:05:44 -0700 | [diff] [blame] | 1196 | static bool mips_relocate_got(soinfo* si, soinfo* needed[]) { |
| 1197 | unsigned* got = si->plt_got; |
| 1198 | if (got == NULL) { |
| 1199 | return true; |
| 1200 | } |
| 1201 | unsigned local_gotno = si->mips_local_gotno; |
| 1202 | unsigned gotsym = si->mips_gotsym; |
| 1203 | unsigned symtabno = si->mips_symtabno; |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1204 | Elf_Sym* symtab = si->symtab; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1205 | |
| 1206 | /* |
| 1207 | * got[0] is address of lazy resolver function |
| 1208 | * got[1] may be used for a GNU extension |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 1209 | * set it to a recognizable address in case someone calls it |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1210 | * (should be _rtld_bind_start) |
| 1211 | * FIXME: maybe this should be in a separate routine |
| 1212 | */ |
| 1213 | |
| 1214 | if ((si->flags & FLAG_LINKER) == 0) { |
Brian Carlstrom | 87c3585 | 2013-08-20 21:05:44 -0700 | [diff] [blame] | 1215 | size_t g = 0; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1216 | got[g++] = 0xdeadbeef; |
| 1217 | if (got[g] & 0x80000000) { |
| 1218 | got[g++] = 0xdeadfeed; |
| 1219 | } |
| 1220 | /* |
| 1221 | * Relocate the local GOT entries need to be relocated |
| 1222 | */ |
| 1223 | for (; g < local_gotno; g++) { |
| 1224 | got[g] += si->load_bias; |
| 1225 | } |
| 1226 | } |
| 1227 | |
| 1228 | /* Now for the global GOT entries */ |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1229 | Elf_Sym* sym = symtab + gotsym; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1230 | got = si->plt_got + local_gotno; |
Brian Carlstrom | 87c3585 | 2013-08-20 21:05:44 -0700 | [diff] [blame] | 1231 | for (size_t g = gotsym; g < symtabno; g++, sym++, got++) { |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1232 | const char* sym_name; |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1233 | Elf_Sym* s; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1234 | soinfo* lsi; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1235 | |
| 1236 | /* This is an undefined reference... try to locate it */ |
| 1237 | sym_name = si->strtab + sym->st_name; |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 1238 | s = soinfo_do_lookup(si, sym_name, &lsi, needed); |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1239 | if (s == NULL) { |
| 1240 | /* We only allow an undefined symbol if this is a weak |
| 1241 | reference.. */ |
| 1242 | s = &symtab[g]; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1243 | if (ELF_ST_BIND(s->st_info) != STB_WEAK) { |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 1244 | DL_ERR("cannot locate \"%s\"...", sym_name); |
Brian Carlstrom | 87c3585 | 2013-08-20 21:05:44 -0700 | [diff] [blame] | 1245 | return false; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1246 | } |
| 1247 | *got = 0; |
| 1248 | } |
| 1249 | else { |
| 1250 | /* FIXME: is this sufficient? |
| 1251 | * For reference see NetBSD link loader |
| 1252 | * 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 |
| 1253 | */ |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 1254 | *got = lsi->load_bias + s->st_value; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1255 | } |
| 1256 | } |
Brian Carlstrom | 87c3585 | 2013-08-20 21:05:44 -0700 | [diff] [blame] | 1257 | return true; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1258 | } |
| 1259 | #endif |
| 1260 | |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1261 | void soinfo::CallArray(const char* array_name UNUSED, linker_function_t* functions, size_t count, bool reverse) { |
| 1262 | if (functions == NULL) { |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1263 | return; |
| 1264 | } |
David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1265 | |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1266 | 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] | 1267 | |
| 1268 | int begin = reverse ? (count - 1) : 0; |
| 1269 | int end = reverse ? -1 : count; |
| 1270 | int step = reverse ? -1 : 1; |
| 1271 | |
| 1272 | for (int i = begin; i != end; i += step) { |
| 1273 | TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]); |
| 1274 | CallFunction("function", functions[i]); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1275 | } |
David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1276 | |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1277 | TRACE("[ Done calling %s for '%s' ]", array_name, name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1278 | } |
| 1279 | |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1280 | void soinfo::CallFunction(const char* function_name UNUSED, linker_function_t function) { |
Elliott Hughes | db492b3 | 2013-01-03 15:44:03 -0800 | [diff] [blame] | 1281 | if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) { |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1282 | return; |
| 1283 | } |
| 1284 | |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1285 | TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1286 | function(); |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1287 | TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name); |
Elliott Hughes | db492b3 | 2013-01-03 15:44:03 -0800 | [diff] [blame] | 1288 | |
| 1289 | // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures |
| 1290 | // are still writable. This happens with our debug malloc (see http://b/7941716). |
| 1291 | set_soinfo_pool_protection(PROT_READ | PROT_WRITE); |
Evgeniy Stepanov | 9181a5d | 2012-08-13 17:58:37 +0400 | [diff] [blame] | 1292 | } |
| 1293 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1294 | void soinfo::CallPreInitConstructors() { |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 1295 | // DT_PREINIT_ARRAY functions are called before any other constructors for executables, |
| 1296 | // but ignored in a shared library. |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1297 | CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false); |
| 1298 | } |
Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 1299 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1300 | void soinfo::CallConstructors() { |
| 1301 | if (constructors_called) { |
| 1302 | return; |
| 1303 | } |
Jesse Hall | f5d1693 | 2012-01-30 15:39:57 -0800 | [diff] [blame] | 1304 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1305 | // We set constructors_called before actually calling the constructors, otherwise it doesn't |
| 1306 | // protect against recursive constructor calls. One simple example of constructor recursion |
| 1307 | // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so: |
| 1308 | // 1. The program depends on libc, so libc's constructor is called here. |
| 1309 | // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so. |
| 1310 | // 3. dlopen() calls the constructors on the newly created |
| 1311 | // soinfo for libc_malloc_debug_leak.so. |
| 1312 | // 4. The debug .so depends on libc, so CallConstructors is |
| 1313 | // called again with the libc soinfo. If it doesn't trigger the early- |
| 1314 | // out above, the libc constructor will be called again (recursively!). |
| 1315 | constructors_called = true; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1316 | |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 1317 | if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) { |
| 1318 | // The GNU dynamic linker silently ignores these, but we warn the developer. |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1319 | PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!", |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 1320 | name, preinit_array_count); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1321 | } |
| 1322 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1323 | if (dynamic != NULL) { |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1324 | for (Elf_Dyn* d = dynamic; d->d_tag != DT_NULL; ++d) { |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1325 | if (d->d_tag == DT_NEEDED) { |
| 1326 | const char* library_name = strtab + d->d_un.d_val; |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 1327 | TRACE("\"%s\": calling constructors in DT_NEEDED \"%s\"", name, library_name); |
| 1328 | find_loaded_library(library_name)->CallConstructors(); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1329 | } |
Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 1330 | } |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1331 | } |
Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 1332 | |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 1333 | TRACE("\"%s\": calling constructors", name); |
| 1334 | |
| 1335 | // 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] | 1336 | CallFunction("DT_INIT", init_func); |
| 1337 | CallArray("DT_INIT_ARRAY", init_array, init_array_count, false); |
Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 1338 | } |
David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1339 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1340 | void soinfo::CallDestructors() { |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 1341 | TRACE("\"%s\": calling destructors", name); |
| 1342 | |
| 1343 | // DT_FINI_ARRAY must be parsed in reverse order. |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1344 | CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true); |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 1345 | |
| 1346 | // 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] | 1347 | CallFunction("DT_FINI", fini_func); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1348 | } |
| 1349 | |
| 1350 | /* Force any of the closed stdin, stdout and stderr to be associated with |
| 1351 | /dev/null. */ |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 1352 | static int nullify_closed_stdio() { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1353 | int dev_null, i, status; |
| 1354 | int return_value = 0; |
| 1355 | |
David 'Digit' Turner | 1608416 | 2012-06-12 16:25:37 +0200 | [diff] [blame] | 1356 | dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1357 | if (dev_null < 0) { |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 1358 | DL_ERR("cannot open /dev/null: %s", strerror(errno)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1359 | return -1; |
| 1360 | } |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1361 | TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1362 | |
| 1363 | /* If any of the stdio file descriptors is valid and not associated |
| 1364 | with /dev/null, dup /dev/null to it. */ |
| 1365 | for (i = 0; i < 3; i++) { |
| 1366 | /* If it is /dev/null already, we are done. */ |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 1367 | if (i == dev_null) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1368 | continue; |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 1369 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1370 | |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1371 | TRACE("[ Nullifying stdio file descriptor %d]", i); |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 1372 | status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1373 | |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 1374 | /* If file is opened, we are good. */ |
| 1375 | if (status != -1) { |
| 1376 | continue; |
| 1377 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1378 | |
| 1379 | /* The only error we allow is that the file descriptor does not |
| 1380 | exist, in which case we dup /dev/null to it. */ |
| 1381 | if (errno != EBADF) { |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 1382 | DL_ERR("fcntl failed: %s", strerror(errno)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1383 | return_value = -1; |
| 1384 | continue; |
| 1385 | } |
| 1386 | |
| 1387 | /* Try dupping /dev/null to this stdio file descriptor and |
| 1388 | repeat if there is a signal. Note that any errors in closing |
| 1389 | the stdio descriptor are lost. */ |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 1390 | status = TEMP_FAILURE_RETRY(dup2(dev_null, i)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1391 | if (status < 0) { |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 1392 | DL_ERR("dup2 failed: %s", strerror(errno)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1393 | return_value = -1; |
| 1394 | continue; |
| 1395 | } |
| 1396 | } |
| 1397 | |
| 1398 | /* If /dev/null is not one of the stdio file descriptors, close it. */ |
| 1399 | if (dev_null > 2) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1400 | TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null); |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 1401 | status = TEMP_FAILURE_RETRY(close(dev_null)); |
| 1402 | if (status == -1) { |
| 1403 | DL_ERR("close failed: %s", strerror(errno)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1404 | return_value = -1; |
| 1405 | } |
| 1406 | } |
| 1407 | |
| 1408 | return return_value; |
| 1409 | } |
| 1410 | |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1411 | static bool soinfo_link_image(soinfo* si) { |
Ji-Hwan Lee | f186a18 | 2012-05-31 20:20:36 +0900 | [diff] [blame] | 1412 | /* "base" might wrap around UINT32_MAX. */ |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1413 | Elf_Addr base = si->load_bias; |
| 1414 | const Elf_Phdr *phdr = si->phdr; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1415 | int phnum = si->phnum; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1416 | bool relocating_linker = (si->flags & FLAG_LINKER) != 0; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1417 | |
David 'Digit' Turner | b52e438 | 2012-06-19 01:24:17 +0200 | [diff] [blame] | 1418 | /* We can't debug anything until the linker is relocated */ |
| 1419 | if (!relocating_linker) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1420 | INFO("[ linking %s ]", si->name); |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1421 | DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags); |
David 'Digit' Turner | b52e438 | 2012-06-19 01:24:17 +0200 | [diff] [blame] | 1422 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1423 | |
David 'Digit' Turner | 63f99f4 | 2012-06-19 00:08:39 +0200 | [diff] [blame] | 1424 | /* Extract dynamic section */ |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1425 | size_t dynamic_count; |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1426 | Elf_Word dynamic_flags; |
Ard Biesheuvel | 12c78bb | 2012-08-14 12:30:09 +0200 | [diff] [blame] | 1427 | phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic, |
Chris Dearman | cf23905 | 2013-01-11 15:32:20 -0800 | [diff] [blame] | 1428 | &dynamic_count, &dynamic_flags); |
David 'Digit' Turner | 63f99f4 | 2012-06-19 00:08:39 +0200 | [diff] [blame] | 1429 | if (si->dynamic == NULL) { |
David 'Digit' Turner | b52e438 | 2012-06-19 01:24:17 +0200 | [diff] [blame] | 1430 | if (!relocating_linker) { |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1431 | DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name); |
David 'Digit' Turner | b52e438 | 2012-06-19 01:24:17 +0200 | [diff] [blame] | 1432 | } |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1433 | return false; |
David 'Digit' Turner | 63f99f4 | 2012-06-19 00:08:39 +0200 | [diff] [blame] | 1434 | } else { |
David 'Digit' Turner | b52e438 | 2012-06-19 01:24:17 +0200 | [diff] [blame] | 1435 | if (!relocating_linker) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1436 | DEBUG("dynamic = %p", si->dynamic); |
David 'Digit' Turner | b52e438 | 2012-06-19 01:24:17 +0200 | [diff] [blame] | 1437 | } |
David 'Digit' Turner | 63f99f4 | 2012-06-19 00:08:39 +0200 | [diff] [blame] | 1438 | } |
| 1439 | |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1440 | #if defined(__arm__) |
David 'Digit' Turner | 63f99f4 | 2012-06-19 00:08:39 +0200 | [diff] [blame] | 1441 | (void) phdr_table_get_arm_exidx(phdr, phnum, base, |
| 1442 | &si->ARM_exidx, &si->ARM_exidx_count); |
| 1443 | #endif |
| 1444 | |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 1445 | // Extract useful information from dynamic section. |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1446 | uint32_t needed_count = 0; |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1447 | for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) { |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1448 | DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p", |
| 1449 | d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val)); |
| 1450 | switch (d->d_tag) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1451 | case DT_HASH: |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1452 | si->nbucket = ((unsigned *) (base + d->d_un.d_ptr))[0]; |
| 1453 | si->nchain = ((unsigned *) (base + d->d_un.d_ptr))[1]; |
| 1454 | si->bucket = (unsigned *) (base + d->d_un.d_ptr + 8); |
| 1455 | si->chain = (unsigned *) (base + d->d_un.d_ptr + 8 + si->nbucket * 4); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1456 | break; |
| 1457 | case DT_STRTAB: |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1458 | si->strtab = (const char *) (base + d->d_un.d_ptr); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1459 | break; |
| 1460 | case DT_SYMTAB: |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1461 | si->symtab = (Elf_Sym *) (base + d->d_un.d_ptr); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1462 | break; |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1463 | #if !defined(__LP64__) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1464 | case DT_PLTREL: |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1465 | if (d->d_un.d_val != DT_REL) { |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1466 | DL_ERR("unsupported DT_RELA in \"%s\"", si->name); |
| 1467 | return false; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1468 | } |
| 1469 | break; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1470 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1471 | case DT_JMPREL: |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1472 | #if defined(USE_RELA) |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1473 | si->plt_rela = (Elf_Rela*) (base + d->d_un.d_ptr); |
| 1474 | #else |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1475 | si->plt_rel = (Elf_Rel*) (base + d->d_un.d_ptr); |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1476 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1477 | break; |
| 1478 | case DT_PLTRELSZ: |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1479 | #if defined(USE_RELA) |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1480 | si->plt_rela_count = d->d_un.d_val / sizeof(Elf_Rela); |
| 1481 | #else |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1482 | si->plt_rel_count = d->d_un.d_val / sizeof(Elf_Rel); |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1483 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1484 | break; |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1485 | #if !defined(__LP64__) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1486 | case DT_PLTGOT: |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1487 | // Used by 32-bit MIPS. |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1488 | si->plt_got = (unsigned *)(base + d->d_un.d_ptr); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1489 | break; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1490 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1491 | case DT_DEBUG: |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 1492 | // Set the DT_DEBUG entry to the address of _r_debug for GDB |
Chris Dearman | cf23905 | 2013-01-11 15:32:20 -0800 | [diff] [blame] | 1493 | // if the dynamic table is writable |
Elliott Hughes | 99c3205 | 2013-01-14 09:56:21 -0800 | [diff] [blame] | 1494 | if ((dynamic_flags & PF_W) != 0) { |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1495 | d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug); |
Elliott Hughes | 99c3205 | 2013-01-14 09:56:21 -0800 | [diff] [blame] | 1496 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1497 | break; |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1498 | #if defined(USE_RELA) |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1499 | case DT_RELA: |
| 1500 | si->rela = (Elf_Rela*) (base + d->d_un.d_ptr); |
| 1501 | break; |
| 1502 | case DT_RELASZ: |
| 1503 | si->rela_count = d->d_un.d_val / sizeof(Elf_Rela); |
| 1504 | break; |
| 1505 | case DT_REL: |
| 1506 | DL_ERR("unsupported DT_REL in \"%s\"", si->name); |
| 1507 | return false; |
| 1508 | case DT_RELSZ: |
| 1509 | DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name); |
| 1510 | return false; |
| 1511 | #else |
| 1512 | case DT_REL: |
| 1513 | si->rel = (Elf_Rel*) (base + d->d_un.d_ptr); |
| 1514 | break; |
| 1515 | case DT_RELSZ: |
| 1516 | si->rel_count = d->d_un.d_val / sizeof(Elf_Rel); |
| 1517 | break; |
Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1518 | case DT_RELA: |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1519 | DL_ERR("unsupported DT_RELA in \"%s\"", si->name); |
| 1520 | return false; |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1521 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1522 | case DT_INIT: |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1523 | si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr); |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 1524 | DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1525 | break; |
| 1526 | case DT_FINI: |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1527 | si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr); |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 1528 | DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1529 | break; |
| 1530 | case DT_INIT_ARRAY: |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1531 | si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr); |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 1532 | DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1533 | break; |
| 1534 | case DT_INIT_ARRAYSZ: |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1535 | si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1536 | break; |
| 1537 | case DT_FINI_ARRAY: |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1538 | si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr); |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 1539 | DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1540 | break; |
| 1541 | case DT_FINI_ARRAYSZ: |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1542 | si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1543 | break; |
| 1544 | case DT_PREINIT_ARRAY: |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1545 | si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr); |
Elliott Hughes | 8147d3c | 2013-05-09 14:19:58 -0700 | [diff] [blame] | 1546 | DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1547 | break; |
| 1548 | case DT_PREINIT_ARRAYSZ: |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1549 | si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1550 | break; |
| 1551 | case DT_TEXTREL: |
Elliott Hughes | e4d792a | 2013-10-28 14:19:05 -0700 | [diff] [blame] | 1552 | #if defined(__LP64__) |
| 1553 | DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name); |
| 1554 | return false; |
| 1555 | #else |
Nick Kralevich | 5135b3a | 2012-08-10 21:08:42 -0700 | [diff] [blame] | 1556 | si->has_text_relocations = true; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1557 | break; |
Elliott Hughes | e4d792a | 2013-10-28 14:19:05 -0700 | [diff] [blame] | 1558 | #endif |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 1559 | case DT_SYMBOLIC: |
| 1560 | si->has_DT_SYMBOLIC = true; |
| 1561 | break; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1562 | case DT_NEEDED: |
| 1563 | ++needed_count; |
| 1564 | break; |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 1565 | case DT_FLAGS: |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1566 | if (d->d_un.d_val & DF_TEXTREL) { |
Elliott Hughes | e4d792a | 2013-10-28 14:19:05 -0700 | [diff] [blame] | 1567 | #if defined(__LP64__) |
| 1568 | DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name); |
| 1569 | return false; |
| 1570 | #else |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 1571 | si->has_text_relocations = true; |
Elliott Hughes | e4d792a | 2013-10-28 14:19:05 -0700 | [diff] [blame] | 1572 | #endif |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 1573 | } |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1574 | if (d->d_un.d_val & DF_SYMBOLIC) { |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 1575 | si->has_DT_SYMBOLIC = true; |
| 1576 | } |
| 1577 | break; |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1578 | #if defined(__mips__) |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1579 | case DT_STRSZ: |
| 1580 | case DT_SYMENT: |
| 1581 | case DT_RELENT: |
| 1582 | break; |
| 1583 | case DT_MIPS_RLD_MAP: |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 1584 | // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB. |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1585 | { |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1586 | r_debug** dp = (r_debug**) d->d_un.d_ptr; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1587 | *dp = &_r_debug; |
| 1588 | } |
| 1589 | break; |
| 1590 | case DT_MIPS_RLD_VERSION: |
| 1591 | case DT_MIPS_FLAGS: |
| 1592 | case DT_MIPS_BASE_ADDRESS: |
| 1593 | case DT_MIPS_UNREFEXTNO: |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1594 | break; |
| 1595 | |
| 1596 | case DT_MIPS_SYMTABNO: |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1597 | si->mips_symtabno = d->d_un.d_val; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1598 | break; |
| 1599 | |
| 1600 | case DT_MIPS_LOCAL_GOTNO: |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1601 | si->mips_local_gotno = d->d_un.d_val; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1602 | break; |
| 1603 | |
| 1604 | case DT_MIPS_GOTSYM: |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1605 | si->mips_gotsym = d->d_un.d_val; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1606 | break; |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1607 | #endif |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1608 | |
| 1609 | default: |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1610 | DEBUG("Unused DT entry: type %p arg %p", |
| 1611 | reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val)); |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1612 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1613 | } |
| 1614 | } |
| 1615 | |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1616 | DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p", |
| 1617 | reinterpret_cast<void*>(si->base), si->strtab, si->symtab); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1618 | |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1619 | // Sanity checks. |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1620 | if (relocating_linker && needed_count != 0) { |
| 1621 | DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries"); |
| 1622 | return false; |
| 1623 | } |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1624 | if (si->nbucket == 0) { |
| 1625 | DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name); |
| 1626 | return false; |
| 1627 | } |
| 1628 | if (si->strtab == 0) { |
| 1629 | DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name); |
| 1630 | return false; |
| 1631 | } |
| 1632 | if (si->symtab == 0) { |
| 1633 | DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name); |
| 1634 | return false; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1635 | } |
| 1636 | |
Elliott Hughes | 7e5a8cc | 2013-06-18 13:15:00 -0700 | [diff] [blame] | 1637 | // If this is the main executable, then load all of the libraries from LD_PRELOAD now. |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1638 | if (si->flags & FLAG_EXE) { |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1639 | memset(gLdPreloads, 0, sizeof(gLdPreloads)); |
Elliott Hughes | 7e5a8cc | 2013-06-18 13:15:00 -0700 | [diff] [blame] | 1640 | size_t preload_count = 0; |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1641 | for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) { |
| 1642 | soinfo* lsi = find_library(gLdPreloadNames[i]); |
Elliott Hughes | 7e5a8cc | 2013-06-18 13:15:00 -0700 | [diff] [blame] | 1643 | if (lsi != NULL) { |
| 1644 | gLdPreloads[preload_count++] = lsi; |
| 1645 | } else { |
| 1646 | // As with glibc, failure to load an LD_PRELOAD library is just a warning. |
| 1647 | DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s", |
| 1648 | gLdPreloadNames[i], si->name, linker_get_error_buffer()); |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 1649 | } |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 1650 | } |
| 1651 | } |
| 1652 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1653 | soinfo** needed = (soinfo**) alloca((1 + needed_count) * sizeof(soinfo*)); |
| 1654 | soinfo** pneeded = needed; |
Ard Biesheuvel | 12c78bb | 2012-08-14 12:30:09 +0200 | [diff] [blame] | 1655 | |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1656 | for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) { |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1657 | if (d->d_tag == DT_NEEDED) { |
| 1658 | const char* library_name = si->strtab + d->d_un.d_val; |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1659 | DEBUG("%s needs %s", si->name, library_name); |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1660 | soinfo* lsi = find_library(library_name); |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1661 | if (lsi == NULL) { |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 1662 | strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf)); |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 1663 | DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s", |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1664 | library_name, si->name, tmp_err_buf); |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1665 | return false; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1666 | } |
Ard Biesheuvel | 12c78bb | 2012-08-14 12:30:09 +0200 | [diff] [blame] | 1667 | *pneeded++ = lsi; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1668 | } |
| 1669 | } |
Ard Biesheuvel | 12c78bb | 2012-08-14 12:30:09 +0200 | [diff] [blame] | 1670 | *pneeded = NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1671 | |
Elliott Hughes | e4d792a | 2013-10-28 14:19:05 -0700 | [diff] [blame] | 1672 | #if !defined(__LP64__) |
Nick Kralevich | 5135b3a | 2012-08-10 21:08:42 -0700 | [diff] [blame] | 1673 | if (si->has_text_relocations) { |
Elliott Hughes | e4d792a | 2013-10-28 14:19:05 -0700 | [diff] [blame] | 1674 | // Make segments writable to allow text relocations to work properly. We will later call |
| 1675 | // phdr_table_protect_segments() after all of them are applied and all constructors are run. |
Nick Kralevich | 3d4470c | 2013-10-22 12:06:36 -0700 | [diff] [blame] | 1676 | DL_WARN("%s has text relocations. This is wasting memory and prevents " |
| 1677 | "security hardening. Please fix.", si->name); |
Nick Kralevich | 5135b3a | 2012-08-10 21:08:42 -0700 | [diff] [blame] | 1678 | if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) { |
| 1679 | DL_ERR("can't unprotect loadable segments for \"%s\": %s", |
| 1680 | si->name, strerror(errno)); |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1681 | return false; |
Nick Kralevich | 5135b3a | 2012-08-10 21:08:42 -0700 | [diff] [blame] | 1682 | } |
| 1683 | } |
Elliott Hughes | e4d792a | 2013-10-28 14:19:05 -0700 | [diff] [blame] | 1684 | #endif |
Nick Kralevich | 5135b3a | 2012-08-10 21:08:42 -0700 | [diff] [blame] | 1685 | |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1686 | #if defined(USE_RELA) |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1687 | if (si->plt_rela != NULL) { |
| 1688 | DEBUG("[ relocating %s plt ]\n", si->name ); |
| 1689 | if (soinfo_relocate_a(si, si->plt_rela, si->plt_rela_count, needed)) { |
| 1690 | return false; |
| 1691 | } |
| 1692 | } |
| 1693 | if (si->rela != NULL) { |
| 1694 | DEBUG("[ relocating %s ]\n", si->name ); |
| 1695 | if (soinfo_relocate_a(si, si->rela, si->rela_count, needed)) { |
| 1696 | return false; |
| 1697 | } |
| 1698 | } |
| 1699 | #else |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1700 | if (si->plt_rel != NULL) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1701 | DEBUG("[ relocating %s plt ]", si->name ); |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1702 | if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) { |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1703 | return false; |
| 1704 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1705 | } |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1706 | if (si->rel != NULL) { |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1707 | DEBUG("[ relocating %s ]", si->name ); |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1708 | if (soinfo_relocate(si, si->rel, si->rel_count, needed)) { |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1709 | return false; |
| 1710 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1711 | } |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1712 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1713 | |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1714 | #if defined(__mips__) |
Brian Carlstrom | 87c3585 | 2013-08-20 21:05:44 -0700 | [diff] [blame] | 1715 | if (!mips_relocate_got(si, needed)) { |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1716 | return false; |
Raghu Gandham | d7daacb | 2012-07-31 12:07:22 -0700 | [diff] [blame] | 1717 | } |
| 1718 | #endif |
| 1719 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1720 | si->flags |= FLAG_LINKED; |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1721 | DEBUG("[ finished linking %s ]", si->name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1722 | |
Elliott Hughes | e4d792a | 2013-10-28 14:19:05 -0700 | [diff] [blame] | 1723 | #if !defined(__LP64__) |
Nick Kralevich | 5135b3a | 2012-08-10 21:08:42 -0700 | [diff] [blame] | 1724 | if (si->has_text_relocations) { |
Elliott Hughes | e4d792a | 2013-10-28 14:19:05 -0700 | [diff] [blame] | 1725 | // All relocations are done, we can protect our segments back to read-only. |
Nick Kralevich | 5135b3a | 2012-08-10 21:08:42 -0700 | [diff] [blame] | 1726 | if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) { |
| 1727 | DL_ERR("can't protect segments for \"%s\": %s", |
| 1728 | si->name, strerror(errno)); |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1729 | return false; |
Nick Kralevich | 5135b3a | 2012-08-10 21:08:42 -0700 | [diff] [blame] | 1730 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1731 | } |
Elliott Hughes | e4d792a | 2013-10-28 14:19:05 -0700 | [diff] [blame] | 1732 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1733 | |
David 'Digit' Turner | b52e438 | 2012-06-19 01:24:17 +0200 | [diff] [blame] | 1734 | /* We can also turn on GNU RELRO protection */ |
| 1735 | if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) { |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 1736 | DL_ERR("can't enable GNU RELRO protection for \"%s\": %s", |
| 1737 | si->name, strerror(errno)); |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1738 | return false; |
Nick Kralevich | 9ec0f03 | 2012-02-28 10:40:00 -0800 | [diff] [blame] | 1739 | } |
| 1740 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1741 | notify_gdb_of_load(si); |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1742 | return true; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1743 | } |
| 1744 | |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 1745 | /* |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 1746 | * This function add vdso to internal dso list. |
| 1747 | * It helps to stack unwinding through signal handlers. |
| 1748 | * Also, it makes bionic more like glibc. |
| 1749 | */ |
| 1750 | static void add_vdso(KernelArgumentBlock& args UNUSED) { |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1751 | #if defined(AT_SYSINFO_EHDR) |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1752 | Elf_Ehdr* ehdr_vdso = reinterpret_cast<Elf_Ehdr*>(args.getauxval(AT_SYSINFO_EHDR)); |
Pavel Chupin | 5407eed | 2013-12-09 18:08:48 +0400 | [diff] [blame] | 1753 | if (ehdr_vdso == NULL) { |
| 1754 | return; |
| 1755 | } |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 1756 | |
| 1757 | soinfo* si = soinfo_alloc("[vdso]"); |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 1758 | |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1759 | si->phdr = reinterpret_cast<Elf_Phdr*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff); |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 1760 | si->phnum = ehdr_vdso->e_phnum; |
Sergey Melnikov | ebd506c | 2013-10-31 18:02:12 +0400 | [diff] [blame] | 1761 | si->base = reinterpret_cast<Elf_Addr>(ehdr_vdso); |
| 1762 | si->size = phdr_table_get_load_size(si->phdr, si->phnum); |
| 1763 | si->flags = 0; |
| 1764 | si->load_bias = get_elf_exec_load_bias(ehdr_vdso); |
| 1765 | |
| 1766 | soinfo_link_image(si); |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 1767 | #endif |
| 1768 | } |
| 1769 | |
| 1770 | /* |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 1771 | * This code is called after the linker has linked itself and |
| 1772 | * fixed it's own GOT. It is safe to make references to externs |
| 1773 | * and other non-local data at this point. |
| 1774 | */ |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1775 | static Elf_Addr __linker_init_post_relocation(KernelArgumentBlock& args, Elf_Addr linker_base) { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 1776 | /* NOTE: we store the args pointer on a special location |
David 'Digit' Turner | ef0bd18 | 2009-07-17 17:55:01 +0200 | [diff] [blame] | 1777 | * of the temporary TLS area in order to pass it to |
| 1778 | * the C Library's runtime initializer. |
| 1779 | * |
| 1780 | * The initializer must clear the slot and reset the TLS |
| 1781 | * to point to a different location to ensure that no other |
| 1782 | * shared library constructor can access it. |
| 1783 | */ |
Elliott Hughes | d3920b3 | 2013-02-07 18:39:34 -0800 | [diff] [blame] | 1784 | __libc_init_tls(args); |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 1785 | |
Evgeniy Stepanov | 1a78fbb | 2012-03-22 18:01:53 +0400 | [diff] [blame] | 1786 | #if TIMING |
| 1787 | struct timeval t0, t1; |
| 1788 | gettimeofday(&t0, 0); |
| 1789 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1790 | |
Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 1791 | // Initialize environment functions, and get to the ELF aux vectors table. |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 1792 | linker_env_init(args); |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 1793 | |
Nick Kralevich | 8d3e91d | 2013-04-25 13:15:24 -0700 | [diff] [blame] | 1794 | // If this is a setuid/setgid program, close the security hole described in |
| 1795 | // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc |
| 1796 | if (get_AT_SECURE()) { |
| 1797 | nullify_closed_stdio(); |
| 1798 | } |
| 1799 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1800 | debuggerd_init(); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1801 | |
Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 1802 | // Get a few environment variables. |
Elliott Hughes | 61a9ccb | 2012-11-02 12:37:13 -0700 | [diff] [blame] | 1803 | const char* LD_DEBUG = linker_env_get("LD_DEBUG"); |
| 1804 | if (LD_DEBUG != NULL) { |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 1805 | gLdDebugVerbosity = atoi(LD_DEBUG); |
Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 1806 | } |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 1807 | |
Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 1808 | // Normally, these are cleaned by linker_env_init, but the test |
| 1809 | // doesn't cost us anything. |
| 1810 | const char* ldpath_env = NULL; |
| 1811 | const char* ldpreload_env = NULL; |
| 1812 | if (!get_AT_SECURE()) { |
| 1813 | ldpath_env = linker_env_get("LD_LIBRARY_PATH"); |
| 1814 | ldpreload_env = linker_env_get("LD_PRELOAD"); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1815 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1816 | |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1817 | INFO("[ android linker & debugger ]"); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1818 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 1819 | soinfo* si = soinfo_alloc(args.argv[0]); |
Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 1820 | if (si == NULL) { |
| 1821 | exit(EXIT_FAILURE); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1822 | } |
| 1823 | |
Nick Kralevich | d39c3ab | 2012-08-24 13:25:51 -0700 | [diff] [blame] | 1824 | /* bootstrap the link map, the main exe always needs to be first */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1825 | si->flags |= FLAG_EXE; |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1826 | link_map_t* map = &(si->link_map); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1827 | |
| 1828 | map->l_addr = 0; |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 1829 | map->l_name = args.argv[0]; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1830 | map->l_prev = NULL; |
| 1831 | map->l_next = NULL; |
| 1832 | |
| 1833 | _r_debug.r_map = map; |
| 1834 | r_debug_tail = map; |
| 1835 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1836 | /* gdb expects the linker to be in the debug shared object list. |
| 1837 | * Without this, gdb has trouble locating the linker's ".text" |
| 1838 | * and ".plt" sections. Gdb could also potentially use this to |
| 1839 | * relocate the offset of our exported 'rtld_db_dlactivity' symbol. |
| 1840 | * Don't use soinfo_alloc(), because the linker shouldn't |
| 1841 | * be on the soinfo list. |
Ben Cheng | 06f0e74 | 2012-08-10 16:07:02 -0700 | [diff] [blame] | 1842 | */ |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1843 | { |
| 1844 | static soinfo linker_soinfo; |
Elliott Hughes | 4eeb1f1 | 2013-10-25 17:38:02 -0700 | [diff] [blame] | 1845 | #if defined(__LP64__) |
Pavel Chupin | 1a57f9f | 2013-02-06 19:21:46 +0400 | [diff] [blame] | 1846 | strlcpy(linker_soinfo.name, "/system/bin/linker64", sizeof(linker_soinfo.name)); |
| 1847 | #else |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1848 | strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name)); |
Pavel Chupin | 1a57f9f | 2013-02-06 19:21:46 +0400 | [diff] [blame] | 1849 | #endif |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1850 | linker_soinfo.flags = 0; |
| 1851 | linker_soinfo.base = linker_base; |
| 1852 | |
| 1853 | /* |
| 1854 | * Set the dynamic field in the link map otherwise gdb will complain with |
| 1855 | * the following: |
| 1856 | * warning: .dynamic section for "/system/bin/linker" is not at the |
| 1857 | * expected address (wrong library or version mismatch?) |
| 1858 | */ |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1859 | Elf_Ehdr *elf_hdr = (Elf_Ehdr *) linker_base; |
| 1860 | Elf_Phdr *phdr = (Elf_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff); |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1861 | phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base, |
| 1862 | &linker_soinfo.dynamic, NULL, NULL); |
| 1863 | insert_soinfo_into_debug_map(&linker_soinfo); |
| 1864 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1865 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 1866 | // Extract information passed from the kernel. |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1867 | si->phdr = reinterpret_cast<Elf_Phdr*>(args.getauxval(AT_PHDR)); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 1868 | si->phnum = args.getauxval(AT_PHNUM); |
| 1869 | si->entry = args.getauxval(AT_ENTRY); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1870 | |
David 'Digit' Turner | 8180b08 | 2011-11-15 17:17:28 +0100 | [diff] [blame] | 1871 | /* Compute the value of si->base. We can't rely on the fact that |
| 1872 | * the first entry is the PHDR because this will not be true |
| 1873 | * for certain executables (e.g. some in the NDK unit test suite) |
| 1874 | */ |
David 'Digit' Turner | 8180b08 | 2011-11-15 17:17:28 +0100 | [diff] [blame] | 1875 | si->base = 0; |
David 'Digit' Turner | b52e438 | 2012-06-19 01:24:17 +0200 | [diff] [blame] | 1876 | si->size = phdr_table_get_load_size(si->phdr, si->phnum); |
David 'Digit' Turner | bea23e5 | 2012-06-18 23:38:46 +0200 | [diff] [blame] | 1877 | si->load_bias = 0; |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1878 | for (size_t i = 0; i < si->phnum; ++i) { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 1879 | if (si->phdr[i].p_type == PT_PHDR) { |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1880 | si->load_bias = reinterpret_cast<Elf_Addr>(si->phdr) - si->phdr[i].p_vaddr; |
| 1881 | si->base = reinterpret_cast<Elf_Addr>(si->phdr) - si->phdr[i].p_offset; |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 1882 | break; |
| 1883 | } |
David 'Digit' Turner | 8180b08 | 2011-11-15 17:17:28 +0100 | [diff] [blame] | 1884 | } |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1885 | si->dynamic = NULL; |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1886 | si->ref_count = 1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1887 | |
Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 1888 | // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid). |
| 1889 | parse_LD_LIBRARY_PATH(ldpath_env); |
| 1890 | parse_LD_PRELOAD(ldpreload_env); |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 1891 | |
Ard Biesheuvel | 5ae44f3 | 2012-08-30 12:48:32 +0200 | [diff] [blame] | 1892 | somain = si; |
| 1893 | |
Elliott Hughes | 124fae9 | 2012-10-31 14:20:03 -0700 | [diff] [blame] | 1894 | if (!soinfo_link_image(si)) { |
Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 1895 | __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer()); |
Elliott Hughes | 18a206c | 2012-10-29 17:37:13 -0700 | [diff] [blame] | 1896 | exit(EXIT_FAILURE); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1897 | } |
| 1898 | |
Sergey Melnikov | c45087b | 2013-01-25 16:40:13 +0400 | [diff] [blame] | 1899 | add_vdso(args); |
| 1900 | |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1901 | si->CallPreInitConstructors(); |
Evgeniy Stepanov | 9181a5d | 2012-08-13 17:58:37 +0400 | [diff] [blame] | 1902 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1903 | for (size_t i = 0; gLdPreloads[i] != NULL; ++i) { |
| 1904 | gLdPreloads[i]->CallConstructors(); |
Kito Cheng | 326e85e | 2012-07-15 00:49:27 +0800 | [diff] [blame] | 1905 | } |
| 1906 | |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1907 | /* After the link_image, the si->load_bias is initialized. |
| 1908 | * For so lib, the map->l_addr will be updated in notify_gdb_of_load. |
| 1909 | * We need to update this value for so exe here. So Unwind_Backtrace |
| 1910 | * for some arch like x86 could work correctly within so exe. |
Xiaokang Qin | 9c3449e | 2012-09-13 18:07:24 +0800 | [diff] [blame] | 1911 | */ |
Chao-Ying Fu | c5db969 | 2012-11-15 02:00:17 -0800 | [diff] [blame] | 1912 | map->l_addr = si->load_bias; |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 1913 | si->CallConstructors(); |
Evgeniy Stepanov | e83c56d | 2011-12-21 13:03:54 +0400 | [diff] [blame] | 1914 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1915 | #if TIMING |
| 1916 | gettimeofday(&t1,NULL); |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1917 | PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) ( |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1918 | (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) - |
| 1919 | (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec) |
| 1920 | )); |
| 1921 | #endif |
| 1922 | #if STATS |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1923 | PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0], |
Elliott Hughes | bedfe38 | 2012-08-14 14:07:59 -0700 | [diff] [blame] | 1924 | linker_stats.count[kRelocAbsolute], |
| 1925 | linker_stats.count[kRelocRelative], |
| 1926 | linker_stats.count[kRelocCopy], |
| 1927 | linker_stats.count[kRelocSymbol]); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1928 | #endif |
| 1929 | #if COUNT_PAGES |
| 1930 | { |
| 1931 | unsigned n; |
| 1932 | unsigned i; |
| 1933 | unsigned count = 0; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1934 | for (n = 0; n < 4096; n++) { |
| 1935 | if (bitmask[n]) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1936 | unsigned x = bitmask[n]; |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 1937 | for (i = 0; i < 8; i++) { |
| 1938 | if (x & 1) { |
| 1939 | count++; |
| 1940 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1941 | x >>= 1; |
| 1942 | } |
| 1943 | } |
| 1944 | } |
Elliott Hughes | ca0c11b | 2013-03-12 10:40:45 -0700 | [diff] [blame] | 1945 | PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1946 | } |
| 1947 | #endif |
| 1948 | |
| 1949 | #if TIMING || STATS || COUNT_PAGES |
| 1950 | fflush(stdout); |
| 1951 | #endif |
| 1952 | |
Elliott Hughes | c00f2cb | 2013-10-04 17:01:33 -0700 | [diff] [blame] | 1953 | TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1954 | return si->entry; |
| 1955 | } |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 1956 | |
David 'Digit' Turner | bea23e5 | 2012-06-18 23:38:46 +0200 | [diff] [blame] | 1957 | /* Compute the load-bias of an existing executable. This shall only |
| 1958 | * be used to compute the load bias of an executable or shared library |
| 1959 | * that was loaded by the kernel itself. |
| 1960 | * |
| 1961 | * Input: |
| 1962 | * elf -> address of ELF header, assumed to be at the start of the file. |
| 1963 | * Return: |
| 1964 | * load bias, i.e. add the value of any p_vaddr in the file to get |
| 1965 | * the corresponding address in memory. |
| 1966 | */ |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1967 | static Elf_Addr get_elf_exec_load_bias(const Elf_Ehdr* elf) { |
| 1968 | Elf_Addr offset = elf->e_phoff; |
| 1969 | const Elf_Phdr* phdr_table = (const Elf_Phdr*)((char*)elf + offset); |
| 1970 | const Elf_Phdr* phdr_end = phdr_table + elf->e_phnum; |
David 'Digit' Turner | bea23e5 | 2012-06-18 23:38:46 +0200 | [diff] [blame] | 1971 | |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1972 | for (const Elf_Phdr* phdr = phdr_table; phdr < phdr_end; phdr++) { |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1973 | if (phdr->p_type == PT_LOAD) { |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1974 | return reinterpret_cast<Elf_Addr>(elf) + phdr->p_offset - phdr->p_vaddr; |
David 'Digit' Turner | bea23e5 | 2012-06-18 23:38:46 +0200 | [diff] [blame] | 1975 | } |
Kito Cheng | fa8c05d | 2013-03-12 14:58:06 +0800 | [diff] [blame] | 1976 | } |
| 1977 | return 0; |
David 'Digit' Turner | bea23e5 | 2012-06-18 23:38:46 +0200 | [diff] [blame] | 1978 | } |
| 1979 | |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 1980 | /* |
| 1981 | * This is the entry point for the linker, called from begin.S. This |
| 1982 | * method is responsible for fixing the linker's own relocations, and |
| 1983 | * then calling __linker_init_post_relocation(). |
| 1984 | * |
| 1985 | * Because this method is called before the linker has fixed it's own |
| 1986 | * relocations, any attempt to reference an extern variable, extern |
| 1987 | * function, or other GOT reference will generate a segfault. |
| 1988 | */ |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1989 | extern "C" Elf_Addr __linker_init(void* raw_args) { |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 1990 | KernelArgumentBlock args(raw_args); |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 1991 | |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1992 | Elf_Addr linker_addr = args.getauxval(AT_BASE); |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 1993 | |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 1994 | Elf_Ehdr* elf_hdr = reinterpret_cast<Elf_Ehdr*>(linker_addr); |
| 1995 | Elf_Phdr* phdr = (Elf_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff); |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 1996 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 1997 | soinfo linker_so; |
| 1998 | memset(&linker_so, 0, sizeof(soinfo)); |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 1999 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 2000 | linker_so.base = linker_addr; |
| 2001 | linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum); |
| 2002 | linker_so.load_bias = get_elf_exec_load_bias(elf_hdr); |
Brian Carlstrom | d4ee82d | 2013-02-28 15:58:45 -0800 | [diff] [blame] | 2003 | linker_so.dynamic = NULL; |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 2004 | linker_so.phdr = phdr; |
| 2005 | linker_so.phnum = elf_hdr->e_phnum; |
| 2006 | linker_so.flags |= FLAG_LINKER; |
Elliott Hughes | 5419b94 | 2012-10-16 15:54:46 -0700 | [diff] [blame] | 2007 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 2008 | if (!soinfo_link_image(&linker_so)) { |
| 2009 | // It would be nice to print an error message, but if the linker |
| 2010 | // can't link itself, there's no guarantee that we'll be able to |
| 2011 | // call write() (because it involves a GOT reference). |
| 2012 | // |
| 2013 | // This situation should never occur unless the linker itself |
| 2014 | // is corrupt. |
| 2015 | exit(EXIT_FAILURE); |
| 2016 | } |
Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 2017 | |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 2018 | // We have successfully fixed our own relocations. It's safe to run |
| 2019 | // the main part of the linker now. |
Elliott Hughes | 0d787c1 | 2013-04-04 13:46:46 -0700 | [diff] [blame] | 2020 | args.abort_message_ptr = &gAbortMessage; |
Elliott Hughes | c620059 | 2013-09-30 18:43:46 -0700 | [diff] [blame] | 2021 | Elf_Addr start_address = __linker_init_post_relocation(args, linker_addr); |
Elliott Hughes | 42b2c6a | 2013-02-07 10:14:39 -0800 | [diff] [blame] | 2022 | |
| 2023 | set_soinfo_pool_protection(PROT_READ); |
| 2024 | |
| 2025 | // Return the address that the calling assembly stub should jump to. |
| 2026 | return start_address; |
Nick Kralevich | 468319c | 2011-11-11 15:53:17 -0800 | [diff] [blame] | 2027 | } |