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