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