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