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