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