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