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