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