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