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