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