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" |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 53 | |
| 54 | #include "ba.h" |
| 55 | |
Iliyan Malchev | 4a9afcb | 2009-09-29 11:43:20 -0700 | [diff] [blame] | 56 | #define ALLOW_SYMBOLS_FROM_MAIN 1 |
James Dong | ba52b30 | 2009-04-30 20:37:36 -0700 | [diff] [blame] | 57 | #define SO_MAX 96 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 58 | |
David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 59 | /* Assume average path length of 64 and max 8 paths */ |
| 60 | #define LDPATH_BUFSIZE 512 |
| 61 | #define LDPATH_MAX 8 |
| 62 | |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 63 | #define LDPRELOAD_BUFSIZE 512 |
| 64 | #define LDPRELOAD_MAX 8 |
| 65 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 66 | /* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<< |
| 67 | * |
| 68 | * Do NOT use malloc() and friends or pthread_*() code here. |
| 69 | * Don't use printf() either; it's caused mysterious memory |
| 70 | * corruption in the past. |
| 71 | * The linker runs before we bring up libc and it's easiest |
| 72 | * to make sure it does not depend on any complex libc features |
| 73 | * |
| 74 | * open issues / todo: |
| 75 | * |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 76 | * - are we doing everything we should for ARM_COPY relocations? |
| 77 | * - cleaner error reporting |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 78 | * - after linking, set as much stuff as possible to READONLY |
| 79 | * and NOEXEC |
| 80 | * - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel |
| 81 | * headers provide versions that are negative... |
| 82 | * - allocate space for soinfo structs dynamically instead of |
| 83 | * having a hard limit (64) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 84 | */ |
| 85 | |
| 86 | |
| 87 | static int link_image(soinfo *si, unsigned wr_offset); |
| 88 | |
| 89 | static int socount = 0; |
| 90 | static soinfo sopool[SO_MAX]; |
| 91 | static soinfo *freelist = NULL; |
| 92 | static soinfo *solist = &libdl_info; |
| 93 | static soinfo *sonext = &libdl_info; |
Iliyan Malchev | 4a9afcb | 2009-09-29 11:43:20 -0700 | [diff] [blame] | 94 | #if ALLOW_SYMBOLS_FROM_MAIN |
| 95 | static soinfo *somain; /* main process, always the one after libdl_info */ |
| 96 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 97 | |
Iliyan Malchev | af7315a | 2009-10-16 17:50:42 -0700 | [diff] [blame] | 98 | |
Iliyan Malchev | e100f52 | 2010-02-10 15:19:37 -0800 | [diff] [blame] | 99 | /* Set up for the buddy allocator managing the non-prelinked libraries. */ |
| 100 | static struct ba_bits ba_nonprelink_bitmap[(LIBLAST - LIBBASE) / LIBINC]; |
| 101 | static struct ba ba_nonprelink = { |
Iliyan Malchev | af7315a | 2009-10-16 17:50:42 -0700 | [diff] [blame] | 102 | .base = LIBBASE, |
| 103 | .size = LIBLAST - LIBBASE, |
| 104 | .min_alloc = LIBINC, |
Iliyan Malchev | bb9eede | 2009-10-19 14:25:17 -0700 | [diff] [blame] | 105 | /* max_order will be determined automatically */ |
Iliyan Malchev | e100f52 | 2010-02-10 15:19:37 -0800 | [diff] [blame] | 106 | .bitmap = ba_nonprelink_bitmap, |
| 107 | .num_entries = sizeof(ba_nonprelink_bitmap)/sizeof(ba_nonprelink_bitmap[0]), |
Iliyan Malchev | af7315a | 2009-10-16 17:50:42 -0700 | [diff] [blame] | 108 | }; |
| 109 | |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 110 | static inline int validate_soinfo(soinfo *si) |
| 111 | { |
| 112 | return (si >= sopool && si < sopool + SO_MAX) || |
| 113 | si == &libdl_info; |
| 114 | } |
| 115 | |
David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 116 | static char ldpaths_buf[LDPATH_BUFSIZE]; |
| 117 | static const char *ldpaths[LDPATH_MAX + 1]; |
| 118 | |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 119 | static char ldpreloads_buf[LDPRELOAD_BUFSIZE]; |
| 120 | static const char *ldpreload_names[LDPRELOAD_MAX + 1]; |
| 121 | |
| 122 | static soinfo *preloads[LDPRELOAD_MAX + 1]; |
| 123 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 124 | int debug_verbosity; |
| 125 | static int pid; |
| 126 | |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 127 | /* This boolean is set if the program being loaded is setuid */ |
| 128 | static int program_is_setuid; |
| 129 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 130 | #if STATS |
| 131 | struct _link_stats linker_stats; |
| 132 | #endif |
| 133 | |
| 134 | #if COUNT_PAGES |
| 135 | unsigned bitmask[4096]; |
| 136 | #endif |
| 137 | |
| 138 | #ifndef PT_ARM_EXIDX |
| 139 | #define PT_ARM_EXIDX 0x70000001 /* .ARM.exidx segment */ |
| 140 | #endif |
| 141 | |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 142 | #define HOODLUM(name, ret, ...) \ |
| 143 | ret name __VA_ARGS__ \ |
| 144 | { \ |
| 145 | char errstr[] = "ERROR: " #name " called from the dynamic linker!\n"; \ |
| 146 | write(2, errstr, sizeof(errstr)); \ |
| 147 | abort(); \ |
| 148 | } |
| 149 | HOODLUM(malloc, void *, (size_t size)); |
| 150 | HOODLUM(free, void, (void *ptr)); |
| 151 | HOODLUM(realloc, void *, (void *ptr, size_t size)); |
| 152 | HOODLUM(calloc, void *, (size_t cnt, size_t size)); |
| 153 | |
Dima Zavin | 0353195 | 2009-05-29 17:30:25 -0700 | [diff] [blame] | 154 | static char tmp_err_buf[768]; |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 155 | static char __linker_dl_err_buf[768]; |
| 156 | #define DL_ERR(fmt, x...) \ |
| 157 | do { \ |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 158 | format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), \ |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 159 | "%s[%d]: " fmt, __func__, __LINE__, ##x); \ |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 160 | ERROR(fmt "\n", ##x); \ |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 161 | } while(0) |
| 162 | |
| 163 | const char *linker_get_error(void) |
| 164 | { |
| 165 | return (const char *)&__linker_dl_err_buf[0]; |
| 166 | } |
| 167 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 168 | /* |
| 169 | * This function is an empty stub where GDB locates a breakpoint to get notified |
| 170 | * about linker activity. |
| 171 | */ |
| 172 | extern void __attribute__((noinline)) rtld_db_dlactivity(void); |
| 173 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 174 | static struct r_debug _r_debug = {1, NULL, &rtld_db_dlactivity, |
| 175 | RT_CONSISTENT, 0}; |
| 176 | static struct link_map *r_debug_tail = 0; |
| 177 | |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 178 | 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] | 179 | |
| 180 | static void insert_soinfo_into_debug_map(soinfo * info) |
| 181 | { |
| 182 | struct link_map * map; |
| 183 | |
| 184 | /* Copy the necessary fields into the debug structure. |
| 185 | */ |
| 186 | map = &(info->linkmap); |
| 187 | map->l_addr = info->base; |
| 188 | map->l_name = (char*) info->name; |
Thinker K.F Li | 5cf640c | 2009-07-03 19:40:32 +0800 | [diff] [blame] | 189 | map->l_ld = (uintptr_t)info->dynamic; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 190 | |
| 191 | /* Stick the new library at the end of the list. |
| 192 | * gdb tends to care more about libc than it does |
| 193 | * about leaf libraries, and ordering it this way |
| 194 | * reduces the back-and-forth over the wire. |
| 195 | */ |
| 196 | if (r_debug_tail) { |
| 197 | r_debug_tail->l_next = map; |
| 198 | map->l_prev = r_debug_tail; |
| 199 | map->l_next = 0; |
| 200 | } else { |
| 201 | _r_debug.r_map = map; |
| 202 | map->l_prev = 0; |
| 203 | map->l_next = 0; |
| 204 | } |
| 205 | r_debug_tail = map; |
| 206 | } |
| 207 | |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 208 | static void remove_soinfo_from_debug_map(soinfo * info) |
| 209 | { |
| 210 | struct link_map * map = &(info->linkmap); |
| 211 | |
| 212 | if (r_debug_tail == map) |
| 213 | r_debug_tail = map->l_prev; |
| 214 | |
| 215 | if (map->l_prev) map->l_prev->l_next = map->l_next; |
| 216 | if (map->l_next) map->l_next->l_prev = map->l_prev; |
| 217 | } |
| 218 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 219 | void notify_gdb_of_load(soinfo * info) |
| 220 | { |
| 221 | if (info->flags & FLAG_EXE) { |
| 222 | // GDB already knows about the main executable |
| 223 | return; |
| 224 | } |
| 225 | |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 226 | pthread_mutex_lock(&_r_debug_lock); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 227 | |
| 228 | _r_debug.r_state = RT_ADD; |
| 229 | rtld_db_dlactivity(); |
| 230 | |
| 231 | insert_soinfo_into_debug_map(info); |
| 232 | |
| 233 | _r_debug.r_state = RT_CONSISTENT; |
| 234 | rtld_db_dlactivity(); |
| 235 | |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 236 | pthread_mutex_unlock(&_r_debug_lock); |
| 237 | } |
| 238 | |
| 239 | void notify_gdb_of_unload(soinfo * info) |
| 240 | { |
| 241 | if (info->flags & FLAG_EXE) { |
| 242 | // GDB already knows about the main executable |
| 243 | return; |
| 244 | } |
| 245 | |
| 246 | pthread_mutex_lock(&_r_debug_lock); |
| 247 | |
| 248 | _r_debug.r_state = RT_DELETE; |
| 249 | rtld_db_dlactivity(); |
| 250 | |
| 251 | remove_soinfo_from_debug_map(info); |
| 252 | |
| 253 | _r_debug.r_state = RT_CONSISTENT; |
| 254 | rtld_db_dlactivity(); |
| 255 | |
| 256 | pthread_mutex_unlock(&_r_debug_lock); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 257 | } |
| 258 | |
| 259 | void notify_gdb_of_libraries() |
| 260 | { |
| 261 | _r_debug.r_state = RT_ADD; |
| 262 | rtld_db_dlactivity(); |
| 263 | _r_debug.r_state = RT_CONSISTENT; |
| 264 | rtld_db_dlactivity(); |
| 265 | } |
| 266 | |
| 267 | static soinfo *alloc_info(const char *name) |
| 268 | { |
| 269 | soinfo *si; |
| 270 | |
| 271 | if(strlen(name) >= SOINFO_NAME_LEN) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 272 | DL_ERR("%5d library name %s too long", pid, name); |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 273 | return NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 274 | } |
| 275 | |
| 276 | /* The freelist is populated when we call free_info(), which in turn is |
| 277 | done only by dlclose(), which is not likely to be used. |
| 278 | */ |
| 279 | if (!freelist) { |
| 280 | if(socount == SO_MAX) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 281 | 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] | 282 | return NULL; |
| 283 | } |
| 284 | freelist = sopool + socount++; |
| 285 | freelist->next = NULL; |
| 286 | } |
| 287 | |
| 288 | si = freelist; |
| 289 | freelist = freelist->next; |
| 290 | |
| 291 | /* Make sure we get a clean block of soinfo */ |
| 292 | memset(si, 0, sizeof(soinfo)); |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 293 | strlcpy((char*) si->name, name, sizeof(si->name)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 294 | sonext->next = si; |
| 295 | si->ba_index = -1; /* by default, prelinked */ |
| 296 | si->next = NULL; |
| 297 | si->refcount = 0; |
| 298 | sonext = si; |
| 299 | |
| 300 | TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si); |
| 301 | return si; |
| 302 | } |
| 303 | |
| 304 | static void free_info(soinfo *si) |
| 305 | { |
| 306 | soinfo *prev = NULL, *trav; |
| 307 | |
| 308 | TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si); |
| 309 | |
| 310 | for(trav = solist; trav != NULL; trav = trav->next){ |
| 311 | if (trav == si) |
| 312 | break; |
| 313 | prev = trav; |
| 314 | } |
| 315 | if (trav == NULL) { |
| 316 | /* si was not ni solist */ |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 317 | 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] | 318 | return; |
| 319 | } |
| 320 | |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 321 | /* 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] | 322 | always the static libdl_info. |
| 323 | */ |
| 324 | prev->next = si->next; |
| 325 | if (si == sonext) sonext = prev; |
| 326 | si->next = freelist; |
| 327 | freelist = si; |
| 328 | } |
| 329 | |
| 330 | #ifndef LINKER_TEXT_BASE |
| 331 | #error "linker's makefile must define LINKER_TEXT_BASE" |
| 332 | #endif |
| 333 | #ifndef LINKER_AREA_SIZE |
| 334 | #error "linker's makefile must define LINKER_AREA_SIZE" |
| 335 | #endif |
| 336 | #define LINKER_BASE ((LINKER_TEXT_BASE) & 0xfff00000) |
| 337 | #define LINKER_TOP (LINKER_BASE + (LINKER_AREA_SIZE)) |
| 338 | |
| 339 | const char *addr_to_name(unsigned addr) |
| 340 | { |
| 341 | soinfo *si; |
| 342 | |
| 343 | for(si = solist; si != 0; si = si->next){ |
| 344 | if((addr >= si->base) && (addr < (si->base + si->size))) { |
| 345 | return si->name; |
| 346 | } |
| 347 | } |
| 348 | |
| 349 | if((addr >= LINKER_BASE) && (addr < LINKER_TOP)){ |
| 350 | return "linker"; |
| 351 | } |
| 352 | |
| 353 | return ""; |
| 354 | } |
| 355 | |
| 356 | /* For a given PC, find the .so that it belongs to. |
| 357 | * Returns the base address of the .ARM.exidx section |
| 358 | * for that .so, and the number of 8-byte entries |
| 359 | * in that section (via *pcount). |
| 360 | * |
| 361 | * Intended to be called by libc's __gnu_Unwind_Find_exidx(). |
| 362 | * |
| 363 | * This function is exposed via dlfcn.c and libdl.so. |
| 364 | */ |
| 365 | #ifdef ANDROID_ARM_LINKER |
| 366 | _Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount) |
| 367 | { |
| 368 | soinfo *si; |
| 369 | unsigned addr = (unsigned)pc; |
| 370 | |
| 371 | if ((addr < LINKER_BASE) || (addr >= LINKER_TOP)) { |
| 372 | for (si = solist; si != 0; si = si->next){ |
| 373 | if ((addr >= si->base) && (addr < (si->base + si->size))) { |
| 374 | *pcount = si->ARM_exidx_count; |
| 375 | return (_Unwind_Ptr)(si->base + (unsigned long)si->ARM_exidx); |
| 376 | } |
| 377 | } |
| 378 | } |
| 379 | *pcount = 0; |
| 380 | return NULL; |
| 381 | } |
Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 382 | #elif defined(ANDROID_X86_LINKER) || defined(ANDROID_SH_LINKER) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 383 | /* Here, we only have to provide a callback to iterate across all the |
| 384 | * loaded libraries. gcc_eh does the rest. */ |
| 385 | int |
| 386 | dl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data), |
| 387 | void *data) |
| 388 | { |
| 389 | soinfo *si; |
| 390 | struct dl_phdr_info dl_info; |
| 391 | int rv = 0; |
| 392 | |
| 393 | for (si = solist; si != NULL; si = si->next) { |
| 394 | dl_info.dlpi_addr = si->linkmap.l_addr; |
| 395 | dl_info.dlpi_name = si->linkmap.l_name; |
| 396 | dl_info.dlpi_phdr = si->phdr; |
| 397 | dl_info.dlpi_phnum = si->phnum; |
| 398 | rv = cb(&dl_info, sizeof (struct dl_phdr_info), data); |
| 399 | if (rv != 0) |
| 400 | break; |
| 401 | } |
| 402 | return rv; |
| 403 | } |
| 404 | #endif |
| 405 | |
| 406 | static Elf32_Sym *_elf_lookup(soinfo *si, unsigned hash, const char *name) |
| 407 | { |
| 408 | Elf32_Sym *s; |
| 409 | Elf32_Sym *symtab = si->symtab; |
| 410 | const char *strtab = si->strtab; |
| 411 | unsigned n; |
| 412 | |
| 413 | TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid, |
| 414 | name, si->name, si->base, hash, hash % si->nbucket); |
| 415 | n = hash % si->nbucket; |
| 416 | |
| 417 | for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){ |
| 418 | s = symtab + n; |
| 419 | if(strcmp(strtab + s->st_name, name)) continue; |
| 420 | |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 421 | /* only concern ourselves with global and weak symbol definitions */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 422 | switch(ELF32_ST_BIND(s->st_info)){ |
| 423 | case STB_GLOBAL: |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 424 | case STB_WEAK: |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 425 | /* no section == undefined */ |
| 426 | if(s->st_shndx == 0) continue; |
| 427 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 428 | TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid, |
| 429 | name, si->name, s->st_value, s->st_size); |
| 430 | return s; |
| 431 | } |
| 432 | } |
| 433 | |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 434 | return NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 435 | } |
| 436 | |
| 437 | static unsigned elfhash(const char *_name) |
| 438 | { |
| 439 | const unsigned char *name = (const unsigned char *) _name; |
| 440 | unsigned h = 0, g; |
| 441 | |
| 442 | while(*name) { |
| 443 | h = (h << 4) + *name++; |
| 444 | g = h & 0xf0000000; |
| 445 | h ^= g; |
| 446 | h ^= g >> 24; |
| 447 | } |
| 448 | return h; |
| 449 | } |
| 450 | |
| 451 | static Elf32_Sym * |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 452 | _do_lookup(soinfo *si, const char *name, unsigned *base) |
| 453 | { |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 454 | unsigned elf_hash = elfhash(name); |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 455 | Elf32_Sym *s; |
| 456 | unsigned *d; |
| 457 | soinfo *lsi = si; |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 458 | int i; |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 459 | |
| 460 | /* Look for symbols in the local scope first (the object who is |
| 461 | * searching). This happens with C++ templates on i386 for some |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 462 | * reason. |
| 463 | * |
| 464 | * Notes on weak symbols: |
| 465 | * The ELF specs are ambigious about treatment of weak definitions in |
| 466 | * dynamic linking. Some systems return the first definition found |
| 467 | * and some the first non-weak definition. This is system dependent. |
| 468 | * Here we return the first definition found for simplicity. */ |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 469 | s = _elf_lookup(si, elf_hash, name); |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 470 | if(s != NULL) |
| 471 | goto done; |
| 472 | |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 473 | /* Next, look for it in the preloads list */ |
| 474 | for(i = 0; preloads[i] != NULL; i++) { |
| 475 | lsi = preloads[i]; |
Jean-Baptiste Queru | f439445 | 2010-05-12 10:05:59 -0700 | [diff] [blame] | 476 | s = _elf_lookup(lsi, elf_hash, name); |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 477 | if(s != NULL) |
| 478 | goto done; |
| 479 | } |
| 480 | |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 481 | for(d = si->dynamic; *d; d += 2) { |
| 482 | if(d[0] == DT_NEEDED){ |
| 483 | lsi = (soinfo *)d[1]; |
| 484 | if (!validate_soinfo(lsi)) { |
| 485 | DL_ERR("%5d bad DT_NEEDED pointer in %s", |
| 486 | pid, si->name); |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 487 | return NULL; |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 488 | } |
| 489 | |
| 490 | DEBUG("%5d %s: looking up %s in %s\n", |
| 491 | pid, si->name, name, lsi->name); |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 492 | s = _elf_lookup(lsi, elf_hash, name); |
Min-su, Kim | 3cab22c | 2010-01-19 10:05:33 +0900 | [diff] [blame] | 493 | if ((s != NULL) && (s->st_shndx != SHN_UNDEF)) |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 494 | goto done; |
| 495 | } |
| 496 | } |
| 497 | |
Iliyan Malchev | 4a9afcb | 2009-09-29 11:43:20 -0700 | [diff] [blame] | 498 | #if ALLOW_SYMBOLS_FROM_MAIN |
| 499 | /* If we are resolving relocations while dlopen()ing a library, it's OK for |
| 500 | * the library to resolve a symbol that's defined in the executable itself, |
| 501 | * although this is rare and is generally a bad idea. |
| 502 | */ |
| 503 | if (somain) { |
| 504 | lsi = somain; |
| 505 | DEBUG("%5d %s: looking up %s in executable %s\n", |
| 506 | pid, si->name, name, lsi->name); |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 507 | s = _elf_lookup(lsi, elf_hash, name); |
Iliyan Malchev | 4a9afcb | 2009-09-29 11:43:20 -0700 | [diff] [blame] | 508 | } |
| 509 | #endif |
| 510 | |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 511 | done: |
| 512 | if(s != NULL) { |
| 513 | TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, " |
| 514 | "found in %s, base = 0x%08x\n", |
| 515 | pid, si->name, name, s->st_value, lsi->name, lsi->base); |
| 516 | *base = lsi->base; |
| 517 | return s; |
| 518 | } |
| 519 | |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 520 | return NULL; |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 521 | } |
| 522 | |
| 523 | /* This is used by dl_sym(). It performs symbol lookup only within the |
| 524 | specified soinfo object and not in any of its dependencies. |
| 525 | */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 526 | Elf32_Sym *lookup_in_library(soinfo *si, const char *name) |
| 527 | { |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 528 | return _elf_lookup(si, elfhash(name), name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 529 | } |
| 530 | |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 531 | /* This is used by dl_sym(). It performs a global symbol lookup. |
| 532 | */ |
Matt Fischer | 1698d9e | 2009-12-31 12:17:56 -0600 | [diff] [blame] | 533 | 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] | 534 | { |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 535 | unsigned elf_hash = elfhash(name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 536 | Elf32_Sym *s = NULL; |
| 537 | soinfo *si; |
| 538 | |
Matt Fischer | 1698d9e | 2009-12-31 12:17:56 -0600 | [diff] [blame] | 539 | if(start == NULL) { |
| 540 | start = solist; |
| 541 | } |
| 542 | |
| 543 | 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] | 544 | { |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 545 | if(si->flags & FLAG_ERROR) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 546 | continue; |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 547 | s = _elf_lookup(si, elf_hash, name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 548 | if (s != NULL) { |
Iliyan Malchev | 9ea64da | 2009-09-28 18:21:30 -0700 | [diff] [blame] | 549 | *found = si; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 550 | break; |
| 551 | } |
| 552 | } |
| 553 | |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 554 | if(s != NULL) { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 555 | TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, " |
| 556 | "si->base = 0x%08x\n", pid, name, s->st_value, si->base); |
| 557 | return s; |
| 558 | } |
| 559 | |
Doug Kwan | 9430435 | 2009-10-23 18:11:40 -0700 | [diff] [blame] | 560 | return NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 561 | } |
| 562 | |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 563 | soinfo *find_containing_library(void *addr) |
| 564 | { |
| 565 | soinfo *si; |
| 566 | |
| 567 | for(si = solist; si != NULL; si = si->next) |
| 568 | { |
| 569 | if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) { |
| 570 | return si; |
| 571 | } |
| 572 | } |
| 573 | |
| 574 | return NULL; |
| 575 | } |
| 576 | |
| 577 | Elf32_Sym *find_containing_symbol(void *addr, soinfo *si) |
| 578 | { |
| 579 | unsigned int i; |
| 580 | unsigned soaddr = (unsigned)addr - si->base; |
| 581 | |
| 582 | /* Search the library's symbol table for any defined symbol which |
| 583 | * contains this address */ |
| 584 | for(i=0; i<si->nchain; i++) { |
| 585 | Elf32_Sym *sym = &si->symtab[i]; |
| 586 | |
| 587 | if(sym->st_shndx != SHN_UNDEF && |
| 588 | soaddr >= sym->st_value && |
| 589 | soaddr < sym->st_value + sym->st_size) { |
| 590 | return sym; |
| 591 | } |
| 592 | } |
| 593 | |
| 594 | return NULL; |
| 595 | } |
| 596 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 597 | #if 0 |
| 598 | static void dump(soinfo *si) |
| 599 | { |
| 600 | Elf32_Sym *s = si->symtab; |
| 601 | unsigned n; |
| 602 | |
| 603 | for(n = 0; n < si->nchain; n++) { |
| 604 | TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s, |
| 605 | s->st_info, s->st_shndx, s->st_value, s->st_size, |
| 606 | si->strtab + s->st_name); |
| 607 | s++; |
| 608 | } |
| 609 | } |
| 610 | #endif |
| 611 | |
| 612 | static const char *sopaths[] = { |
Brian Swetland | fedbcde | 2010-09-19 03:39:13 -0700 | [diff] [blame] | 613 | "/vendor/lib", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 614 | "/system/lib", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 615 | 0 |
| 616 | }; |
| 617 | |
| 618 | static int _open_lib(const char *name) |
| 619 | { |
| 620 | int fd; |
| 621 | struct stat filestat; |
| 622 | |
| 623 | if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) { |
| 624 | if ((fd = open(name, O_RDONLY)) >= 0) |
| 625 | return fd; |
| 626 | } |
| 627 | |
| 628 | return -1; |
| 629 | } |
| 630 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 631 | static int open_library(const char *name) |
| 632 | { |
| 633 | int fd; |
| 634 | char buf[512]; |
| 635 | const char **path; |
David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 636 | int n; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 637 | |
| 638 | TRACE("[ %5d opening %s ]\n", pid, name); |
| 639 | |
| 640 | if(name == 0) return -1; |
| 641 | if(strlen(name) > 256) return -1; |
| 642 | |
| 643 | if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0)) |
| 644 | return fd; |
| 645 | |
David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 646 | for (path = ldpaths; *path; path++) { |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 647 | n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name); |
David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 648 | if (n < 0 || n >= (int)sizeof(buf)) { |
| 649 | WARN("Ignoring very long library path: %s/%s\n", *path, name); |
| 650 | continue; |
| 651 | } |
| 652 | if ((fd = _open_lib(buf)) >= 0) |
| 653 | return fd; |
| 654 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 655 | for (path = sopaths; *path; path++) { |
David 'Digit' Turner | 5c73464 | 2010-01-20 12:36:51 -0800 | [diff] [blame] | 656 | n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name); |
David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 657 | if (n < 0 || n >= (int)sizeof(buf)) { |
| 658 | WARN("Ignoring very long library path: %s/%s\n", *path, name); |
| 659 | continue; |
| 660 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 661 | if ((fd = _open_lib(buf)) >= 0) |
| 662 | return fd; |
| 663 | } |
| 664 | |
| 665 | return -1; |
| 666 | } |
| 667 | |
| 668 | /* temporary space for holding the first page of the shared lib |
| 669 | * which contains the elf header (with the pht). */ |
| 670 | static unsigned char __header[PAGE_SIZE]; |
| 671 | |
| 672 | typedef struct { |
| 673 | long mmap_addr; |
| 674 | char tag[4]; /* 'P', 'R', 'E', ' ' */ |
| 675 | } prelink_info_t; |
| 676 | |
| 677 | /* Returns the requested base address if the library is prelinked, |
| 678 | * and 0 otherwise. */ |
| 679 | static unsigned long |
| 680 | is_prelinked(int fd, const char *name) |
| 681 | { |
| 682 | off_t sz; |
| 683 | prelink_info_t info; |
| 684 | |
| 685 | sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END); |
| 686 | if (sz < 0) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 687 | DL_ERR("lseek() failed!"); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 688 | return 0; |
| 689 | } |
| 690 | |
| 691 | if (read(fd, &info, sizeof(info)) != sizeof(info)) { |
| 692 | WARN("Could not read prelink_info_t structure for `%s`\n", name); |
| 693 | return 0; |
| 694 | } |
| 695 | |
| 696 | if (strncmp(info.tag, "PRE ", 4)) { |
| 697 | WARN("`%s` is not a prelinked library\n", name); |
| 698 | return 0; |
| 699 | } |
| 700 | |
| 701 | return (unsigned long)info.mmap_addr; |
| 702 | } |
| 703 | |
| 704 | /* verify_elf_object |
| 705 | * Verifies if the object @ base is a valid ELF object |
| 706 | * |
| 707 | * Args: |
| 708 | * |
| 709 | * Returns: |
| 710 | * 0 on success |
| 711 | * -1 if no valid ELF object is found @ base. |
| 712 | */ |
| 713 | static int |
| 714 | verify_elf_object(void *base, const char *name) |
| 715 | { |
| 716 | Elf32_Ehdr *hdr = (Elf32_Ehdr *) base; |
| 717 | |
| 718 | if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1; |
| 719 | if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1; |
| 720 | if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1; |
| 721 | if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1; |
| 722 | |
| 723 | /* TODO: Should we verify anything else in the header? */ |
| 724 | |
| 725 | return 0; |
| 726 | } |
| 727 | |
| 728 | |
| 729 | /* get_lib_extents |
| 730 | * Retrieves the base (*base) address where the ELF object should be |
| 731 | * mapped and its overall memory size (*total_sz). |
| 732 | * |
| 733 | * Args: |
| 734 | * fd: Opened file descriptor for the library |
| 735 | * name: The name of the library |
| 736 | * _hdr: Pointer to the header page of the library |
| 737 | * total_sz: Total size of the memory that should be allocated for |
| 738 | * this library |
| 739 | * |
| 740 | * Returns: |
| 741 | * -1 if there was an error while trying to get the lib extents. |
| 742 | * The possible reasons are: |
| 743 | * - Could not determine if the library was prelinked. |
| 744 | * - The library provided is not a valid ELF object |
| 745 | * 0 if the library did not request a specific base offset (normal |
| 746 | * for non-prelinked libs) |
| 747 | * > 0 if the library requests a specific address to be mapped to. |
| 748 | * This indicates a pre-linked library. |
| 749 | */ |
| 750 | static unsigned |
| 751 | get_lib_extents(int fd, const char *name, void *__hdr, unsigned *total_sz) |
| 752 | { |
| 753 | unsigned req_base; |
| 754 | unsigned min_vaddr = 0xffffffff; |
| 755 | unsigned max_vaddr = 0; |
| 756 | unsigned char *_hdr = (unsigned char *)__hdr; |
| 757 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *)_hdr; |
| 758 | Elf32_Phdr *phdr; |
| 759 | int cnt; |
| 760 | |
| 761 | TRACE("[ %5d Computing extents for '%s'. ]\n", pid, name); |
| 762 | if (verify_elf_object(_hdr, name) < 0) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 763 | DL_ERR("%5d - %s is not a valid ELF object", pid, name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 764 | return (unsigned)-1; |
| 765 | } |
| 766 | |
| 767 | req_base = (unsigned) is_prelinked(fd, name); |
| 768 | if (req_base == (unsigned)-1) |
| 769 | return -1; |
| 770 | else if (req_base != 0) { |
| 771 | TRACE("[ %5d - Prelinked library '%s' requesting base @ 0x%08x ]\n", |
| 772 | pid, name, req_base); |
| 773 | } else { |
| 774 | TRACE("[ %5d - Non-prelinked library '%s' found. ]\n", pid, name); |
| 775 | } |
| 776 | |
| 777 | phdr = (Elf32_Phdr *)(_hdr + ehdr->e_phoff); |
| 778 | |
| 779 | /* find the min/max p_vaddrs from all the PT_LOAD segments so we can |
| 780 | * get the range. */ |
| 781 | for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) { |
| 782 | if (phdr->p_type == PT_LOAD) { |
| 783 | if ((phdr->p_vaddr + phdr->p_memsz) > max_vaddr) |
| 784 | max_vaddr = phdr->p_vaddr + phdr->p_memsz; |
| 785 | if (phdr->p_vaddr < min_vaddr) |
| 786 | min_vaddr = phdr->p_vaddr; |
| 787 | } |
| 788 | } |
| 789 | |
| 790 | if ((min_vaddr == 0xffffffff) && (max_vaddr == 0)) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 791 | DL_ERR("%5d - No loadable segments found in %s.", pid, name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 792 | return (unsigned)-1; |
| 793 | } |
| 794 | |
| 795 | /* truncate min_vaddr down to page boundary */ |
| 796 | min_vaddr &= ~PAGE_MASK; |
| 797 | |
| 798 | /* round max_vaddr up to the next page */ |
| 799 | max_vaddr = (max_vaddr + PAGE_SIZE - 1) & ~PAGE_MASK; |
| 800 | |
| 801 | *total_sz = (max_vaddr - min_vaddr); |
| 802 | return (unsigned)req_base; |
| 803 | } |
| 804 | |
| 805 | /* alloc_mem_region |
| 806 | * |
| 807 | * This function reserves a chunk of memory to be used for mapping in |
| 808 | * the shared library. We reserve the entire memory region here, and |
| 809 | * then the rest of the linker will relocate the individual loadable |
| 810 | * segments into the correct locations within this memory range. |
| 811 | * |
| 812 | * Args: |
| 813 | * si->base: The requested base of the allocation. If 0, a sane one will be |
| 814 | * chosen in the range LIBBASE <= base < LIBLAST. |
| 815 | * si->size: The size of the allocation. |
| 816 | * |
| 817 | * Returns: |
| 818 | * -1 on failure, and 0 on success. On success, si->base will contain |
| 819 | * the virtual address at which the library will be mapped. |
| 820 | */ |
| 821 | |
| 822 | static int reserve_mem_region(soinfo *si) |
| 823 | { |
| 824 | void *base = mmap((void *)si->base, si->size, PROT_READ | PROT_EXEC, |
Chris Dearman | db4bce0 | 2011-03-10 10:48:14 -0800 | [diff] [blame^] | 825 | MAP_FIXED | MAP_PRIVATE | MAP_ANONYMOUS, -1, 0); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 826 | if (base == MAP_FAILED) { |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 827 | DL_ERR("%5d can NOT map (%sprelinked) library '%s' at 0x%08x " |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 828 | "as requested, will try general pool: %d (%s)", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 829 | pid, (si->base ? "" : "non-"), si->name, si->base, |
| 830 | errno, strerror(errno)); |
| 831 | return -1; |
| 832 | } else if (base != (void *)si->base) { |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 833 | DL_ERR("OOPS: %5d %sprelinked library '%s' mapped at 0x%08x, " |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 834 | "not at 0x%08x", pid, (si->base ? "" : "non-"), |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 835 | si->name, (unsigned)base, si->base); |
| 836 | munmap(base, si->size); |
| 837 | return -1; |
| 838 | } |
| 839 | return 0; |
| 840 | } |
| 841 | |
| 842 | static int |
| 843 | alloc_mem_region(soinfo *si) |
| 844 | { |
| 845 | if (si->base) { |
| 846 | /* Attempt to mmap a prelinked library. */ |
| 847 | si->ba_index = -1; |
| 848 | return reserve_mem_region(si); |
| 849 | } |
| 850 | |
| 851 | /* This is not a prelinked library, so we attempt to allocate space |
| 852 | for it from the buddy allocator, which manages the area between |
| 853 | LIBBASE and LIBLAST. |
| 854 | */ |
Iliyan Malchev | e100f52 | 2010-02-10 15:19:37 -0800 | [diff] [blame] | 855 | si->ba_index = ba_allocate(&ba_nonprelink, si->size); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 856 | if(si->ba_index >= 0) { |
Iliyan Malchev | e100f52 | 2010-02-10 15:19:37 -0800 | [diff] [blame] | 857 | si->base = ba_start_addr(&ba_nonprelink, si->ba_index); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 858 | PRINT("%5d mapping library '%s' at %08x (index %d) " \ |
| 859 | "through buddy allocator.\n", |
| 860 | pid, si->name, si->base, si->ba_index); |
| 861 | if (reserve_mem_region(si) < 0) { |
Iliyan Malchev | e100f52 | 2010-02-10 15:19:37 -0800 | [diff] [blame] | 862 | ba_free(&ba_nonprelink, si->ba_index); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 863 | si->ba_index = -1; |
| 864 | si->base = 0; |
| 865 | goto err; |
| 866 | } |
| 867 | return 0; |
| 868 | } |
| 869 | |
| 870 | err: |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 871 | DL_ERR("OOPS: %5d cannot map library '%s'. no vspace available.", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 872 | pid, si->name); |
| 873 | return -1; |
| 874 | } |
| 875 | |
| 876 | #define MAYBE_MAP_FLAG(x,from,to) (((x) & (from)) ? (to) : 0) |
| 877 | #define PFLAGS_TO_PROT(x) (MAYBE_MAP_FLAG((x), PF_X, PROT_EXEC) | \ |
| 878 | MAYBE_MAP_FLAG((x), PF_R, PROT_READ) | \ |
| 879 | MAYBE_MAP_FLAG((x), PF_W, PROT_WRITE)) |
| 880 | /* load_segments |
| 881 | * |
| 882 | * This function loads all the loadable (PT_LOAD) segments into memory |
| 883 | * at their appropriate memory offsets off the base address. |
| 884 | * |
| 885 | * Args: |
| 886 | * fd: Open file descriptor to the library to load. |
| 887 | * header: Pointer to a header page that contains the ELF header. |
| 888 | * This is needed since we haven't mapped in the real file yet. |
| 889 | * si: ptr to soinfo struct describing the shared object. |
| 890 | * |
| 891 | * Returns: |
| 892 | * 0 on success, -1 on failure. |
| 893 | */ |
| 894 | static int |
| 895 | load_segments(int fd, void *header, soinfo *si) |
| 896 | { |
| 897 | Elf32_Ehdr *ehdr = (Elf32_Ehdr *)header; |
| 898 | Elf32_Phdr *phdr = (Elf32_Phdr *)((unsigned char *)header + ehdr->e_phoff); |
| 899 | unsigned char *base = (unsigned char *)si->base; |
| 900 | int cnt; |
| 901 | unsigned len; |
| 902 | unsigned char *tmp; |
| 903 | unsigned char *pbase; |
| 904 | unsigned char *extra_base; |
| 905 | unsigned extra_len; |
| 906 | unsigned total_sz = 0; |
| 907 | |
| 908 | si->wrprotect_start = 0xffffffff; |
| 909 | si->wrprotect_end = 0; |
| 910 | |
| 911 | TRACE("[ %5d - Begin loading segments for '%s' @ 0x%08x ]\n", |
| 912 | pid, si->name, (unsigned)si->base); |
| 913 | /* Now go through all the PT_LOAD segments and map them into memory |
| 914 | * at the appropriate locations. */ |
| 915 | for (cnt = 0; cnt < ehdr->e_phnum; ++cnt, ++phdr) { |
| 916 | if (phdr->p_type == PT_LOAD) { |
| 917 | DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid); |
| 918 | /* we want to map in the segment on a page boundary */ |
| 919 | tmp = base + (phdr->p_vaddr & (~PAGE_MASK)); |
| 920 | /* add the # of bytes we masked off above to the total length. */ |
| 921 | len = phdr->p_filesz + (phdr->p_vaddr & PAGE_MASK); |
| 922 | |
| 923 | TRACE("[ %d - Trying to load segment from '%s' @ 0x%08x " |
| 924 | "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x ]\n", pid, si->name, |
| 925 | (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset); |
| 926 | pbase = mmap(tmp, len, PFLAGS_TO_PROT(phdr->p_flags), |
| 927 | MAP_PRIVATE | MAP_FIXED, fd, |
| 928 | phdr->p_offset & (~PAGE_MASK)); |
| 929 | if (pbase == MAP_FAILED) { |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 930 | DL_ERR("%d failed to map segment from '%s' @ 0x%08x (0x%08x). " |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 931 | "p_vaddr=0x%08x p_offset=0x%08x", pid, si->name, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 932 | (unsigned)tmp, len, phdr->p_vaddr, phdr->p_offset); |
| 933 | goto fail; |
| 934 | } |
| 935 | |
| 936 | /* If 'len' didn't end on page boundary, and it's a writable |
| 937 | * segment, zero-fill the rest. */ |
| 938 | if ((len & PAGE_MASK) && (phdr->p_flags & PF_W)) |
| 939 | memset((void *)(pbase + len), 0, PAGE_SIZE - (len & PAGE_MASK)); |
| 940 | |
| 941 | /* Check to see if we need to extend the map for this segment to |
| 942 | * cover the diff between filesz and memsz (i.e. for bss). |
| 943 | * |
| 944 | * base _+---------------------+ page boundary |
| 945 | * . . |
| 946 | * | | |
| 947 | * . . |
| 948 | * pbase _+---------------------+ page boundary |
| 949 | * | | |
| 950 | * . . |
| 951 | * base + p_vaddr _| | |
| 952 | * . \ \ . |
| 953 | * . | filesz | . |
| 954 | * pbase + len _| / | | |
| 955 | * <0 pad> . . . |
| 956 | * extra_base _+------------|--------+ page boundary |
| 957 | * / . . . |
| 958 | * | . . . |
| 959 | * | +------------|--------+ page boundary |
| 960 | * extra_len-> | | | | |
| 961 | * | . | memsz . |
| 962 | * | . | . |
| 963 | * \ _| / | |
| 964 | * . . |
| 965 | * | | |
| 966 | * _+---------------------+ page boundary |
| 967 | */ |
| 968 | tmp = (unsigned char *)(((unsigned)pbase + len + PAGE_SIZE - 1) & |
| 969 | (~PAGE_MASK)); |
| 970 | if (tmp < (base + phdr->p_vaddr + phdr->p_memsz)) { |
| 971 | extra_len = base + phdr->p_vaddr + phdr->p_memsz - tmp; |
| 972 | TRACE("[ %5d - Need to extend segment from '%s' @ 0x%08x " |
| 973 | "(0x%08x) ]\n", pid, si->name, (unsigned)tmp, extra_len); |
| 974 | /* map in the extra page(s) as anonymous into the range. |
| 975 | * This is probably not necessary as we already mapped in |
| 976 | * the entire region previously, but we just want to be |
| 977 | * sure. This will also set the right flags on the region |
| 978 | * (though we can probably accomplish the same thing with |
| 979 | * mprotect). |
| 980 | */ |
| 981 | extra_base = mmap((void *)tmp, extra_len, |
| 982 | PFLAGS_TO_PROT(phdr->p_flags), |
| 983 | MAP_PRIVATE | MAP_FIXED | MAP_ANONYMOUS, |
| 984 | -1, 0); |
| 985 | if (extra_base == MAP_FAILED) { |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 986 | DL_ERR("[ %5d - failed to extend segment from '%s' @ 0x%08x" |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 987 | " (0x%08x) ]", pid, si->name, (unsigned)tmp, |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 988 | extra_len); |
| 989 | goto fail; |
| 990 | } |
| 991 | /* TODO: Check if we need to memset-0 this region. |
| 992 | * Anonymous mappings are zero-filled copy-on-writes, so we |
| 993 | * shouldn't need to. */ |
| 994 | TRACE("[ %5d - Segment from '%s' extended @ 0x%08x " |
| 995 | "(0x%08x)\n", pid, si->name, (unsigned)extra_base, |
| 996 | extra_len); |
| 997 | } |
| 998 | /* set the len here to show the full extent of the segment we |
| 999 | * just loaded, mostly for debugging */ |
| 1000 | len = (((unsigned)base + phdr->p_vaddr + phdr->p_memsz + |
| 1001 | PAGE_SIZE - 1) & (~PAGE_MASK)) - (unsigned)pbase; |
| 1002 | TRACE("[ %5d - Successfully loaded segment from '%s' @ 0x%08x " |
| 1003 | "(0x%08x). p_vaddr=0x%08x p_offset=0x%08x\n", pid, si->name, |
| 1004 | (unsigned)pbase, len, phdr->p_vaddr, phdr->p_offset); |
| 1005 | total_sz += len; |
| 1006 | /* Make the section writable just in case we'll have to write to |
| 1007 | * it during relocation (i.e. text segment). However, we will |
| 1008 | * remember what range of addresses should be write protected. |
| 1009 | * |
| 1010 | */ |
| 1011 | if (!(phdr->p_flags & PF_W)) { |
| 1012 | if ((unsigned)pbase < si->wrprotect_start) |
| 1013 | si->wrprotect_start = (unsigned)pbase; |
| 1014 | if (((unsigned)pbase + len) > si->wrprotect_end) |
| 1015 | si->wrprotect_end = (unsigned)pbase + len; |
| 1016 | mprotect(pbase, len, |
| 1017 | PFLAGS_TO_PROT(phdr->p_flags) | PROT_WRITE); |
| 1018 | } |
| 1019 | } else if (phdr->p_type == PT_DYNAMIC) { |
| 1020 | DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid); |
| 1021 | /* this segment contains the dynamic linking information */ |
| 1022 | si->dynamic = (unsigned *)(base + phdr->p_vaddr); |
| 1023 | } else { |
| 1024 | #ifdef ANDROID_ARM_LINKER |
| 1025 | if (phdr->p_type == PT_ARM_EXIDX) { |
| 1026 | DEBUG_DUMP_PHDR(phdr, "PT_ARM_EXIDX", pid); |
| 1027 | /* exidx entries (used for stack unwinding) are 8 bytes each. |
| 1028 | */ |
| 1029 | si->ARM_exidx = (unsigned *)phdr->p_vaddr; |
| 1030 | si->ARM_exidx_count = phdr->p_memsz / 8; |
| 1031 | } |
| 1032 | #endif |
| 1033 | } |
| 1034 | |
| 1035 | } |
| 1036 | |
| 1037 | /* Sanity check */ |
| 1038 | if (total_sz > si->size) { |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1039 | DL_ERR("%5d - Total length (0x%08x) of mapped segments from '%s' is " |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1040 | "greater than what was allocated (0x%08x). THIS IS BAD!", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1041 | pid, total_sz, si->name, si->size); |
| 1042 | goto fail; |
| 1043 | } |
| 1044 | |
| 1045 | TRACE("[ %5d - Finish loading segments for '%s' @ 0x%08x. " |
| 1046 | "Total memory footprint: 0x%08x bytes ]\n", pid, si->name, |
| 1047 | (unsigned)si->base, si->size); |
| 1048 | return 0; |
| 1049 | |
| 1050 | fail: |
| 1051 | /* We can just blindly unmap the entire region even though some things |
| 1052 | * were mapped in originally with anonymous and others could have been |
| 1053 | * been mapped in from the file before we failed. The kernel will unmap |
| 1054 | * all the pages in the range, irrespective of how they got there. |
| 1055 | */ |
| 1056 | munmap((void *)si->base, si->size); |
| 1057 | si->flags |= FLAG_ERROR; |
| 1058 | return -1; |
| 1059 | } |
| 1060 | |
| 1061 | /* TODO: Implement this to take care of the fact that Android ARM |
| 1062 | * ELF objects shove everything into a single loadable segment that has the |
| 1063 | * write bit set. wr_offset is then used to set non-(data|bss) pages to be |
| 1064 | * non-writable. |
| 1065 | */ |
| 1066 | #if 0 |
| 1067 | static unsigned |
| 1068 | get_wr_offset(int fd, const char *name, Elf32_Ehdr *ehdr) |
| 1069 | { |
| 1070 | Elf32_Shdr *shdr_start; |
| 1071 | Elf32_Shdr *shdr; |
| 1072 | int shdr_sz = ehdr->e_shnum * sizeof(Elf32_Shdr); |
| 1073 | int cnt; |
| 1074 | unsigned wr_offset = 0xffffffff; |
| 1075 | |
| 1076 | shdr_start = mmap(0, shdr_sz, PROT_READ, MAP_PRIVATE, fd, |
| 1077 | ehdr->e_shoff & (~PAGE_MASK)); |
| 1078 | if (shdr_start == MAP_FAILED) { |
| 1079 | WARN("%5d - Could not read section header info from '%s'. Will not " |
| 1080 | "not be able to determine write-protect offset.\n", pid, name); |
| 1081 | return (unsigned)-1; |
| 1082 | } |
| 1083 | |
| 1084 | for(cnt = 0, shdr = shdr_start; cnt < ehdr->e_shnum; ++cnt, ++shdr) { |
| 1085 | if ((shdr->sh_type != SHT_NULL) && (shdr->sh_flags & SHF_WRITE) && |
| 1086 | (shdr->sh_addr < wr_offset)) { |
| 1087 | wr_offset = shdr->sh_addr; |
| 1088 | } |
| 1089 | } |
| 1090 | |
| 1091 | munmap(shdr_start, shdr_sz); |
| 1092 | return wr_offset; |
| 1093 | } |
| 1094 | #endif |
| 1095 | |
| 1096 | static soinfo * |
| 1097 | load_library(const char *name) |
| 1098 | { |
| 1099 | int fd = open_library(name); |
| 1100 | int cnt; |
| 1101 | unsigned ext_sz; |
| 1102 | unsigned req_base; |
Erik Gilling | fde8642 | 2009-07-28 20:28:19 -0700 | [diff] [blame] | 1103 | const char *bname; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1104 | soinfo *si = NULL; |
| 1105 | Elf32_Ehdr *hdr; |
| 1106 | |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1107 | if(fd == -1) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1108 | DL_ERR("Library '%s' not found", name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1109 | return NULL; |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1110 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1111 | |
| 1112 | /* We have to read the ELF header to figure out what to do with this image |
| 1113 | */ |
| 1114 | if (lseek(fd, 0, SEEK_SET) < 0) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1115 | DL_ERR("lseek() failed!"); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1116 | goto fail; |
| 1117 | } |
| 1118 | |
| 1119 | if ((cnt = read(fd, &__header[0], PAGE_SIZE)) < 0) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1120 | DL_ERR("read() failed!"); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1121 | goto fail; |
| 1122 | } |
| 1123 | |
| 1124 | /* Parse the ELF header and get the size of the memory footprint for |
| 1125 | * the library */ |
| 1126 | req_base = get_lib_extents(fd, name, &__header[0], &ext_sz); |
| 1127 | if (req_base == (unsigned)-1) |
| 1128 | goto fail; |
| 1129 | TRACE("[ %5d - '%s' (%s) wants base=0x%08x sz=0x%08x ]\n", pid, name, |
| 1130 | (req_base ? "prelinked" : "not pre-linked"), req_base, ext_sz); |
| 1131 | |
| 1132 | /* Now configure the soinfo struct where we'll store all of our data |
| 1133 | * for the ELF object. If the loading fails, we waste the entry, but |
| 1134 | * same thing would happen if we failed during linking. Configuring the |
| 1135 | * soinfo struct here is a lot more convenient. |
| 1136 | */ |
Erik Gilling | fde8642 | 2009-07-28 20:28:19 -0700 | [diff] [blame] | 1137 | bname = strrchr(name, '/'); |
| 1138 | si = alloc_info(bname ? bname + 1 : name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1139 | if (si == NULL) |
| 1140 | goto fail; |
| 1141 | |
| 1142 | /* Carve out a chunk of memory where we will map in the individual |
| 1143 | * segments */ |
| 1144 | si->base = req_base; |
| 1145 | si->size = ext_sz; |
| 1146 | si->flags = 0; |
| 1147 | si->entry = 0; |
| 1148 | si->dynamic = (unsigned *)-1; |
| 1149 | if (alloc_mem_region(si) < 0) |
| 1150 | goto fail; |
| 1151 | |
| 1152 | TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n", |
| 1153 | pid, name, (void *)si->base, (unsigned) ext_sz); |
| 1154 | |
| 1155 | /* Now actually load the library's segments into right places in memory */ |
| 1156 | if (load_segments(fd, &__header[0], si) < 0) { |
| 1157 | if (si->ba_index >= 0) { |
Iliyan Malchev | e100f52 | 2010-02-10 15:19:37 -0800 | [diff] [blame] | 1158 | ba_free(&ba_nonprelink, si->ba_index); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1159 | si->ba_index = -1; |
| 1160 | } |
| 1161 | goto fail; |
| 1162 | } |
| 1163 | |
| 1164 | /* this might not be right. Technically, we don't even need this info |
| 1165 | * once we go through 'load_segments'. */ |
| 1166 | hdr = (Elf32_Ehdr *)si->base; |
| 1167 | si->phdr = (Elf32_Phdr *)((unsigned char *)si->base + hdr->e_phoff); |
| 1168 | si->phnum = hdr->e_phnum; |
| 1169 | /**/ |
| 1170 | |
| 1171 | close(fd); |
| 1172 | return si; |
| 1173 | |
| 1174 | fail: |
| 1175 | if (si) free_info(si); |
| 1176 | close(fd); |
| 1177 | return NULL; |
| 1178 | } |
| 1179 | |
| 1180 | static soinfo * |
| 1181 | init_library(soinfo *si) |
| 1182 | { |
| 1183 | unsigned wr_offset = 0xffffffff; |
| 1184 | |
| 1185 | /* At this point we know that whatever is loaded @ base is a valid ELF |
| 1186 | * shared library whose segments are properly mapped in. */ |
| 1187 | TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n", |
| 1188 | pid, si->base, si->size, si->name); |
| 1189 | |
| 1190 | if (si->base < LIBBASE || si->base >= LIBLAST) |
| 1191 | si->flags |= FLAG_PRELINKED; |
| 1192 | |
| 1193 | if(link_image(si, wr_offset)) { |
| 1194 | /* We failed to link. However, we can only restore libbase |
| 1195 | ** if no additional libraries have moved it since we updated it. |
| 1196 | */ |
| 1197 | munmap((void *)si->base, si->size); |
| 1198 | return NULL; |
| 1199 | } |
| 1200 | |
| 1201 | return si; |
| 1202 | } |
| 1203 | |
| 1204 | soinfo *find_library(const char *name) |
| 1205 | { |
| 1206 | soinfo *si; |
David 'Digit' Turner | 6774809 | 2010-07-21 16:18:21 -0700 | [diff] [blame] | 1207 | const char *bname; |
| 1208 | |
| 1209 | #if ALLOW_SYMBOLS_FROM_MAIN |
| 1210 | if (name == NULL) |
| 1211 | return somain; |
| 1212 | #else |
| 1213 | if (name == NULL) |
| 1214 | return NULL; |
| 1215 | #endif |
| 1216 | |
| 1217 | bname = strrchr(name, '/'); |
Erik Gilling | fde8642 | 2009-07-28 20:28:19 -0700 | [diff] [blame] | 1218 | bname = bname ? bname + 1 : name; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1219 | |
| 1220 | for(si = solist; si != 0; si = si->next){ |
Erik Gilling | fde8642 | 2009-07-28 20:28:19 -0700 | [diff] [blame] | 1221 | if(!strcmp(bname, si->name)) { |
Erik Gilling | 30eb402 | 2009-08-13 16:05:30 -0700 | [diff] [blame] | 1222 | if(si->flags & FLAG_ERROR) { |
| 1223 | DL_ERR("%5d '%s' failed to load previously", pid, bname); |
| 1224 | return NULL; |
| 1225 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1226 | if(si->flags & FLAG_LINKED) return si; |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1227 | DL_ERR("OOPS: %5d recursive link to '%s'", pid, si->name); |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1228 | return NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1229 | } |
| 1230 | } |
| 1231 | |
| 1232 | TRACE("[ %5d '%s' has not been loaded yet. Locating...]\n", pid, name); |
| 1233 | si = load_library(name); |
| 1234 | if(si == NULL) |
| 1235 | return NULL; |
| 1236 | return init_library(si); |
| 1237 | } |
| 1238 | |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 1239 | /* TODO: |
| 1240 | * notify gdb of unload |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1241 | * for non-prelinked libraries, find a way to decrement libbase |
| 1242 | */ |
| 1243 | static void call_destructors(soinfo *si); |
| 1244 | unsigned unload_library(soinfo *si) |
| 1245 | { |
| 1246 | unsigned *d; |
| 1247 | if (si->refcount == 1) { |
| 1248 | TRACE("%5d unloading '%s'\n", pid, si->name); |
| 1249 | call_destructors(si); |
| 1250 | |
| 1251 | for(d = si->dynamic; *d; d += 2) { |
| 1252 | if(d[0] == DT_NEEDED){ |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 1253 | soinfo *lsi = (soinfo *)d[1]; |
| 1254 | d[1] = 0; |
| 1255 | if (validate_soinfo(lsi)) { |
| 1256 | TRACE("%5d %s needs to unload %s\n", pid, |
| 1257 | si->name, lsi->name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1258 | unload_library(lsi); |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 1259 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1260 | else |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 1261 | DL_ERR("%5d %s: could not unload dependent library", |
| 1262 | pid, si->name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1263 | } |
| 1264 | } |
| 1265 | |
| 1266 | munmap((char *)si->base, si->size); |
| 1267 | if (si->ba_index >= 0) { |
| 1268 | PRINT("%5d releasing library '%s' address space at %08x "\ |
| 1269 | "through buddy allocator.\n", |
| 1270 | pid, si->name, si->base); |
Iliyan Malchev | e100f52 | 2010-02-10 15:19:37 -0800 | [diff] [blame] | 1271 | ba_free(&ba_nonprelink, si->ba_index); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1272 | } |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 1273 | notify_gdb_of_unload(si); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1274 | free_info(si); |
| 1275 | si->refcount = 0; |
| 1276 | } |
| 1277 | else { |
| 1278 | si->refcount--; |
| 1279 | PRINT("%5d not unloading '%s', decrementing refcount to %d\n", |
| 1280 | pid, si->name, si->refcount); |
| 1281 | } |
| 1282 | return si->refcount; |
| 1283 | } |
| 1284 | |
| 1285 | /* TODO: don't use unsigned for addrs below. It works, but is not |
| 1286 | * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned |
| 1287 | * long. |
| 1288 | */ |
| 1289 | static int reloc_library(soinfo *si, Elf32_Rel *rel, unsigned count) |
| 1290 | { |
| 1291 | Elf32_Sym *symtab = si->symtab; |
| 1292 | const char *strtab = si->strtab; |
| 1293 | Elf32_Sym *s; |
| 1294 | unsigned base; |
| 1295 | Elf32_Rel *start = rel; |
| 1296 | unsigned idx; |
| 1297 | |
| 1298 | for (idx = 0; idx < count; ++idx) { |
| 1299 | unsigned type = ELF32_R_TYPE(rel->r_info); |
| 1300 | unsigned sym = ELF32_R_SYM(rel->r_info); |
| 1301 | unsigned reloc = (unsigned)(rel->r_offset + si->base); |
| 1302 | unsigned sym_addr = 0; |
| 1303 | char *sym_name = NULL; |
| 1304 | |
| 1305 | DEBUG("%5d Processing '%s' relocation at index %d\n", pid, |
| 1306 | si->name, idx); |
| 1307 | if(sym != 0) { |
Dima Zavin | d1b40d8 | 2009-05-12 10:59:09 -0700 | [diff] [blame] | 1308 | sym_name = (char *)(strtab + symtab[sym].st_name); |
| 1309 | s = _do_lookup(si, sym_name, &base); |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1310 | if(s == NULL) { |
| 1311 | /* We only allow an undefined symbol if this is a weak |
| 1312 | reference.. */ |
| 1313 | s = &symtab[sym]; |
| 1314 | if (ELF32_ST_BIND(s->st_info) != STB_WEAK) { |
| 1315 | DL_ERR("%5d cannot locate '%s'...\n", pid, sym_name); |
| 1316 | return -1; |
| 1317 | } |
| 1318 | |
| 1319 | /* IHI0044C AAELF 4.5.1.1: |
| 1320 | |
| 1321 | Libraries are not searched to resolve weak references. |
| 1322 | It is not an error for a weak reference to remain |
| 1323 | unsatisfied. |
| 1324 | |
| 1325 | During linking, the value of an undefined weak reference is: |
| 1326 | - Zero if the relocation type is absolute |
| 1327 | - The address of the place if the relocation is pc-relative |
| 1328 | - The address of nominial base address if the relocation |
| 1329 | type is base-relative. |
| 1330 | */ |
| 1331 | |
| 1332 | switch (type) { |
| 1333 | #if defined(ANDROID_ARM_LINKER) |
| 1334 | case R_ARM_JUMP_SLOT: |
| 1335 | case R_ARM_GLOB_DAT: |
| 1336 | case R_ARM_ABS32: |
| 1337 | case R_ARM_RELATIVE: /* Don't care. */ |
| 1338 | case R_ARM_NONE: /* Don't care. */ |
| 1339 | #elif defined(ANDROID_X86_LINKER) |
| 1340 | case R_386_JUMP_SLOT: |
| 1341 | case R_386_GLOB_DAT: |
| 1342 | case R_386_32: |
| 1343 | case R_386_RELATIVE: /* Dont' care. */ |
| 1344 | #endif /* ANDROID_*_LINKER */ |
| 1345 | /* sym_addr was initialized to be zero above or relocation |
| 1346 | code below does not care about value of sym_addr. |
| 1347 | No need to do anything. */ |
| 1348 | break; |
| 1349 | |
| 1350 | #if defined(ANDROID_X86_LINKER) |
| 1351 | case R_386_PC32: |
| 1352 | sym_addr = reloc; |
| 1353 | break; |
| 1354 | #endif /* ANDROID_X86_LINKER */ |
| 1355 | |
| 1356 | #if defined(ANDROID_ARM_LINKER) |
| 1357 | case R_ARM_COPY: |
| 1358 | /* Fall through. Can't really copy if weak symbol is |
| 1359 | not found in run-time. */ |
| 1360 | #endif /* ANDROID_ARM_LINKER */ |
| 1361 | default: |
| 1362 | DL_ERR("%5d unknown weak reloc type %d @ %p (%d)\n", |
| 1363 | pid, type, rel, (int) (rel - start)); |
| 1364 | return -1; |
| 1365 | } |
| 1366 | } else { |
| 1367 | /* We got a definition. */ |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1368 | #if 0 |
| 1369 | if((base == 0) && (si->base != 0)){ |
| 1370 | /* linking from libraries to main image is bad */ |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1371 | DL_ERR("%5d cannot locate '%s'...", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1372 | pid, strtab + symtab[sym].st_name); |
| 1373 | return -1; |
| 1374 | } |
| 1375 | #endif |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1376 | sym_addr = (unsigned)(s->st_value + base); |
| 1377 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1378 | COUNT_RELOC(RELOC_SYMBOL); |
| 1379 | } else { |
Doug Kwan | e823807 | 2009-10-26 12:05:23 -0700 | [diff] [blame] | 1380 | s = NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1381 | } |
| 1382 | |
| 1383 | /* TODO: This is ugly. Split up the relocations by arch into |
| 1384 | * different files. |
| 1385 | */ |
| 1386 | switch(type){ |
| 1387 | #if defined(ANDROID_ARM_LINKER) |
| 1388 | case R_ARM_JUMP_SLOT: |
| 1389 | COUNT_RELOC(RELOC_ABSOLUTE); |
| 1390 | MARK(rel->r_offset); |
| 1391 | TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid, |
| 1392 | reloc, sym_addr, sym_name); |
| 1393 | *((unsigned*)reloc) = sym_addr; |
| 1394 | break; |
| 1395 | case R_ARM_GLOB_DAT: |
| 1396 | COUNT_RELOC(RELOC_ABSOLUTE); |
| 1397 | MARK(rel->r_offset); |
| 1398 | TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid, |
| 1399 | reloc, sym_addr, sym_name); |
| 1400 | *((unsigned*)reloc) = sym_addr; |
| 1401 | break; |
| 1402 | case R_ARM_ABS32: |
| 1403 | COUNT_RELOC(RELOC_ABSOLUTE); |
| 1404 | MARK(rel->r_offset); |
| 1405 | TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid, |
| 1406 | reloc, sym_addr, sym_name); |
| 1407 | *((unsigned*)reloc) += sym_addr; |
| 1408 | break; |
David 'Digit' Turner | 34ea511 | 2009-11-17 14:56:26 -0800 | [diff] [blame] | 1409 | case R_ARM_REL32: |
| 1410 | COUNT_RELOC(RELOC_RELATIVE); |
| 1411 | MARK(rel->r_offset); |
| 1412 | TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid, |
| 1413 | reloc, sym_addr, rel->r_offset, sym_name); |
| 1414 | *((unsigned*)reloc) += sym_addr - rel->r_offset; |
| 1415 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1416 | #elif defined(ANDROID_X86_LINKER) |
| 1417 | case R_386_JUMP_SLOT: |
| 1418 | COUNT_RELOC(RELOC_ABSOLUTE); |
| 1419 | MARK(rel->r_offset); |
| 1420 | TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid, |
| 1421 | reloc, sym_addr, sym_name); |
| 1422 | *((unsigned*)reloc) = sym_addr; |
| 1423 | break; |
| 1424 | case R_386_GLOB_DAT: |
| 1425 | COUNT_RELOC(RELOC_ABSOLUTE); |
| 1426 | MARK(rel->r_offset); |
| 1427 | TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid, |
| 1428 | reloc, sym_addr, sym_name); |
| 1429 | *((unsigned*)reloc) = sym_addr; |
| 1430 | break; |
| 1431 | #endif /* ANDROID_*_LINKER */ |
| 1432 | |
| 1433 | #if defined(ANDROID_ARM_LINKER) |
| 1434 | case R_ARM_RELATIVE: |
| 1435 | #elif defined(ANDROID_X86_LINKER) |
| 1436 | case R_386_RELATIVE: |
| 1437 | #endif /* ANDROID_*_LINKER */ |
| 1438 | COUNT_RELOC(RELOC_RELATIVE); |
| 1439 | MARK(rel->r_offset); |
| 1440 | if(sym){ |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1441 | DL_ERR("%5d odd RELATIVE form...", pid); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1442 | return -1; |
| 1443 | } |
| 1444 | TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid, |
| 1445 | reloc, si->base); |
| 1446 | *((unsigned*)reloc) += si->base; |
| 1447 | break; |
| 1448 | |
| 1449 | #if defined(ANDROID_X86_LINKER) |
| 1450 | case R_386_32: |
| 1451 | COUNT_RELOC(RELOC_RELATIVE); |
| 1452 | MARK(rel->r_offset); |
| 1453 | |
| 1454 | TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid, |
| 1455 | reloc, sym_addr, sym_name); |
| 1456 | *((unsigned *)reloc) += (unsigned)sym_addr; |
| 1457 | break; |
| 1458 | |
| 1459 | case R_386_PC32: |
| 1460 | COUNT_RELOC(RELOC_RELATIVE); |
| 1461 | MARK(rel->r_offset); |
| 1462 | TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- " |
| 1463 | "+%08x (%08x - %08x) %s\n", pid, reloc, |
| 1464 | (sym_addr - reloc), sym_addr, reloc, sym_name); |
| 1465 | *((unsigned *)reloc) += (unsigned)(sym_addr - reloc); |
| 1466 | break; |
| 1467 | #endif /* ANDROID_X86_LINKER */ |
| 1468 | |
| 1469 | #ifdef ANDROID_ARM_LINKER |
| 1470 | case R_ARM_COPY: |
| 1471 | COUNT_RELOC(RELOC_COPY); |
| 1472 | MARK(rel->r_offset); |
| 1473 | TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid, |
| 1474 | reloc, s->st_size, sym_addr, sym_name); |
| 1475 | memcpy((void*)reloc, (void*)sym_addr, s->st_size); |
| 1476 | break; |
Iliyan Malchev | 5e12d7e | 2009-03-24 19:02:00 -0700 | [diff] [blame] | 1477 | case R_ARM_NONE: |
| 1478 | break; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1479 | #endif /* ANDROID_ARM_LINKER */ |
| 1480 | |
| 1481 | default: |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1482 | DL_ERR("%5d unknown reloc type %d @ %p (%d)", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1483 | pid, type, rel, (int) (rel - start)); |
| 1484 | return -1; |
| 1485 | } |
| 1486 | rel++; |
| 1487 | } |
| 1488 | return 0; |
| 1489 | } |
| 1490 | |
Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1491 | #if defined(ANDROID_SH_LINKER) |
| 1492 | static int reloc_library_a(soinfo *si, Elf32_Rela *rela, unsigned count) |
| 1493 | { |
| 1494 | Elf32_Sym *symtab = si->symtab; |
| 1495 | const char *strtab = si->strtab; |
| 1496 | Elf32_Sym *s; |
| 1497 | unsigned base; |
| 1498 | Elf32_Rela *start = rela; |
| 1499 | unsigned idx; |
| 1500 | |
| 1501 | for (idx = 0; idx < count; ++idx) { |
| 1502 | unsigned type = ELF32_R_TYPE(rela->r_info); |
| 1503 | unsigned sym = ELF32_R_SYM(rela->r_info); |
| 1504 | unsigned reloc = (unsigned)(rela->r_offset + si->base); |
| 1505 | unsigned sym_addr = 0; |
| 1506 | char *sym_name = NULL; |
| 1507 | |
| 1508 | DEBUG("%5d Processing '%s' relocation at index %d\n", pid, |
| 1509 | si->name, idx); |
| 1510 | if(sym != 0) { |
| 1511 | sym_name = (char *)(strtab + symtab[sym].st_name); |
| 1512 | s = _do_lookup(si, sym_name, &base); |
| 1513 | if(s == 0) { |
| 1514 | DL_ERR("%5d cannot locate '%s'...", pid, sym_name); |
| 1515 | return -1; |
| 1516 | } |
| 1517 | #if 0 |
| 1518 | if((base == 0) && (si->base != 0)){ |
| 1519 | /* linking from libraries to main image is bad */ |
| 1520 | DL_ERR("%5d cannot locate '%s'...", |
| 1521 | pid, strtab + symtab[sym].st_name); |
| 1522 | return -1; |
| 1523 | } |
| 1524 | #endif |
| 1525 | if ((s->st_shndx == SHN_UNDEF) && (s->st_value != 0)) { |
| 1526 | DL_ERR("%5d In '%s', shndx=%d && value=0x%08x. We do not " |
| 1527 | "handle this yet", pid, si->name, s->st_shndx, |
| 1528 | s->st_value); |
| 1529 | return -1; |
| 1530 | } |
| 1531 | sym_addr = (unsigned)(s->st_value + base); |
| 1532 | COUNT_RELOC(RELOC_SYMBOL); |
| 1533 | } else { |
| 1534 | s = 0; |
| 1535 | } |
| 1536 | |
| 1537 | /* TODO: This is ugly. Split up the relocations by arch into |
| 1538 | * different files. |
| 1539 | */ |
| 1540 | switch(type){ |
| 1541 | case R_SH_JUMP_SLOT: |
| 1542 | COUNT_RELOC(RELOC_ABSOLUTE); |
| 1543 | MARK(rela->r_offset); |
| 1544 | TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid, |
| 1545 | reloc, sym_addr, sym_name); |
| 1546 | *((unsigned*)reloc) = sym_addr; |
| 1547 | break; |
| 1548 | case R_SH_GLOB_DAT: |
| 1549 | COUNT_RELOC(RELOC_ABSOLUTE); |
| 1550 | MARK(rela->r_offset); |
| 1551 | TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid, |
| 1552 | reloc, sym_addr, sym_name); |
| 1553 | *((unsigned*)reloc) = sym_addr; |
| 1554 | break; |
| 1555 | case R_SH_DIR32: |
| 1556 | COUNT_RELOC(RELOC_ABSOLUTE); |
| 1557 | MARK(rela->r_offset); |
| 1558 | TRACE_TYPE(RELO, "%5d RELO DIR32 %08x <- %08x %s\n", pid, |
| 1559 | reloc, sym_addr, sym_name); |
| 1560 | *((unsigned*)reloc) += sym_addr; |
| 1561 | break; |
| 1562 | case R_SH_RELATIVE: |
| 1563 | COUNT_RELOC(RELOC_RELATIVE); |
| 1564 | MARK(rela->r_offset); |
| 1565 | if(sym){ |
| 1566 | DL_ERR("%5d odd RELATIVE form...", pid); |
| 1567 | return -1; |
| 1568 | } |
| 1569 | TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid, |
| 1570 | reloc, si->base); |
| 1571 | *((unsigned*)reloc) += si->base; |
| 1572 | break; |
| 1573 | |
| 1574 | default: |
| 1575 | DL_ERR("%5d unknown reloc type %d @ %p (%d)", |
| 1576 | pid, type, rela, (int) (rela - start)); |
| 1577 | return -1; |
| 1578 | } |
| 1579 | rela++; |
| 1580 | } |
| 1581 | return 0; |
| 1582 | } |
| 1583 | #endif /* ANDROID_SH_LINKER */ |
| 1584 | |
David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1585 | |
| 1586 | /* Please read the "Initialization and Termination functions" functions. |
| 1587 | * of the linker design note in bionic/linker/README.TXT to understand |
| 1588 | * what the following code is doing. |
| 1589 | * |
| 1590 | * The important things to remember are: |
| 1591 | * |
| 1592 | * DT_PREINIT_ARRAY must be called first for executables, and should |
| 1593 | * not appear in shared libraries. |
| 1594 | * |
| 1595 | * DT_INIT should be called before DT_INIT_ARRAY if both are present |
| 1596 | * |
| 1597 | * DT_FINI should be called after DT_FINI_ARRAY if both are present |
| 1598 | * |
| 1599 | * DT_FINI_ARRAY must be parsed in reverse order. |
| 1600 | */ |
| 1601 | |
| 1602 | 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] | 1603 | { |
David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1604 | int n, inc = 1; |
| 1605 | |
| 1606 | if (reverse) { |
| 1607 | ctor += (count-1); |
| 1608 | inc = -1; |
| 1609 | } |
| 1610 | |
| 1611 | for(n = count; n > 0; n--) { |
| 1612 | TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid, |
| 1613 | reverse ? "dtor" : "ctor", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1614 | (unsigned)ctor, (unsigned)*ctor); |
David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1615 | void (*func)() = (void (*)()) *ctor; |
| 1616 | ctor += inc; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1617 | if(((int) func == 0) || ((int) func == -1)) continue; |
| 1618 | TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func); |
| 1619 | func(); |
| 1620 | } |
| 1621 | } |
| 1622 | |
| 1623 | static void call_constructors(soinfo *si) |
| 1624 | { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1625 | if (si->flags & FLAG_EXE) { |
| 1626 | TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n", |
| 1627 | pid, (unsigned)si->preinit_array, si->preinit_array_count, |
| 1628 | si->name); |
David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1629 | 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] | 1630 | TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name); |
| 1631 | } else { |
| 1632 | if (si->preinit_array) { |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1633 | 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] | 1634 | " This is INVALID.", pid, si->name, |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1635 | (unsigned)si->preinit_array); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1636 | } |
| 1637 | } |
| 1638 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1639 | if (si->init_func) { |
| 1640 | TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid, |
| 1641 | (unsigned)si->init_func, si->name); |
| 1642 | si->init_func(); |
| 1643 | TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name); |
| 1644 | } |
| 1645 | |
| 1646 | if (si->init_array) { |
| 1647 | TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid, |
| 1648 | (unsigned)si->init_array, si->init_array_count, si->name); |
David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1649 | 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] | 1650 | TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name); |
| 1651 | } |
| 1652 | } |
| 1653 | |
David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1654 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1655 | static void call_destructors(soinfo *si) |
| 1656 | { |
| 1657 | if (si->fini_array) { |
| 1658 | TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid, |
| 1659 | (unsigned)si->fini_array, si->fini_array_count, si->name); |
David 'Digit' Turner | 8215679 | 2009-05-18 14:37:41 +0200 | [diff] [blame] | 1660 | 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] | 1661 | TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name); |
| 1662 | } |
| 1663 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1664 | if (si->fini_func) { |
| 1665 | TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid, |
| 1666 | (unsigned)si->fini_func, si->name); |
| 1667 | si->fini_func(); |
| 1668 | TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name); |
| 1669 | } |
| 1670 | } |
| 1671 | |
| 1672 | /* Force any of the closed stdin, stdout and stderr to be associated with |
| 1673 | /dev/null. */ |
| 1674 | static int nullify_closed_stdio (void) |
| 1675 | { |
| 1676 | int dev_null, i, status; |
| 1677 | int return_value = 0; |
| 1678 | |
| 1679 | dev_null = open("/dev/null", O_RDWR); |
| 1680 | if (dev_null < 0) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1681 | DL_ERR("Cannot open /dev/null."); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1682 | return -1; |
| 1683 | } |
| 1684 | TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null); |
| 1685 | |
| 1686 | /* If any of the stdio file descriptors is valid and not associated |
| 1687 | with /dev/null, dup /dev/null to it. */ |
| 1688 | for (i = 0; i < 3; i++) { |
| 1689 | /* If it is /dev/null already, we are done. */ |
| 1690 | if (i == dev_null) |
| 1691 | continue; |
| 1692 | |
| 1693 | TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i); |
| 1694 | /* The man page of fcntl does not say that fcntl(..,F_GETFL) |
| 1695 | can be interrupted but we do this just to be safe. */ |
| 1696 | do { |
| 1697 | status = fcntl(i, F_GETFL); |
| 1698 | } while (status < 0 && errno == EINTR); |
| 1699 | |
| 1700 | /* If file is openned, we are good. */ |
| 1701 | if (status >= 0) |
| 1702 | continue; |
| 1703 | |
| 1704 | /* The only error we allow is that the file descriptor does not |
| 1705 | exist, in which case we dup /dev/null to it. */ |
| 1706 | if (errno != EBADF) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1707 | DL_ERR("nullify_stdio: unhandled error %s", strerror(errno)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1708 | return_value = -1; |
| 1709 | continue; |
| 1710 | } |
| 1711 | |
| 1712 | /* Try dupping /dev/null to this stdio file descriptor and |
| 1713 | repeat if there is a signal. Note that any errors in closing |
| 1714 | the stdio descriptor are lost. */ |
| 1715 | do { |
| 1716 | status = dup2(dev_null, i); |
| 1717 | } while (status < 0 && errno == EINTR); |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1718 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1719 | if (status < 0) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1720 | DL_ERR("nullify_stdio: dup2 error %s", strerror(errno)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1721 | return_value = -1; |
| 1722 | continue; |
| 1723 | } |
| 1724 | } |
| 1725 | |
| 1726 | /* If /dev/null is not one of the stdio file descriptors, close it. */ |
| 1727 | if (dev_null > 2) { |
| 1728 | TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null); |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1729 | do { |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1730 | status = close(dev_null); |
| 1731 | } while (status < 0 && errno == EINTR); |
| 1732 | |
| 1733 | if (status < 0) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1734 | DL_ERR("nullify_stdio: close error %s", strerror(errno)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1735 | return_value = -1; |
| 1736 | } |
| 1737 | } |
| 1738 | |
| 1739 | return return_value; |
| 1740 | } |
| 1741 | |
| 1742 | static int link_image(soinfo *si, unsigned wr_offset) |
| 1743 | { |
| 1744 | unsigned *d; |
| 1745 | Elf32_Phdr *phdr = si->phdr; |
| 1746 | int phnum = si->phnum; |
| 1747 | |
| 1748 | INFO("[ %5d linking %s ]\n", pid, si->name); |
| 1749 | DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid, |
| 1750 | si->base, si->flags); |
| 1751 | |
| 1752 | if (si->flags & FLAG_EXE) { |
| 1753 | /* Locate the needed program segments (DYNAMIC/ARM_EXIDX) for |
| 1754 | * linkage info if this is the executable. If this was a |
| 1755 | * dynamic lib, that would have been done at load time. |
| 1756 | * |
| 1757 | * TODO: It's unfortunate that small pieces of this are |
| 1758 | * repeated from the load_library routine. Refactor this just |
| 1759 | * slightly to reuse these bits. |
| 1760 | */ |
| 1761 | si->size = 0; |
| 1762 | for(; phnum > 0; --phnum, ++phdr) { |
| 1763 | #ifdef ANDROID_ARM_LINKER |
| 1764 | if(phdr->p_type == PT_ARM_EXIDX) { |
| 1765 | /* exidx entries (used for stack unwinding) are 8 bytes each. |
| 1766 | */ |
| 1767 | si->ARM_exidx = (unsigned *)phdr->p_vaddr; |
| 1768 | si->ARM_exidx_count = phdr->p_memsz / 8; |
| 1769 | } |
| 1770 | #endif |
| 1771 | if (phdr->p_type == PT_LOAD) { |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 1772 | /* For the executable, we use the si->size field only in |
| 1773 | dl_unwind_find_exidx(), so the meaning of si->size |
| 1774 | is not the size of the executable; it is the last |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1775 | virtual address of the loadable part of the executable; |
| 1776 | since si->base == 0 for an executable, we use the |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 1777 | range [0, si->size) to determine whether a PC value |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1778 | falls within the executable section. Of course, if |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 1779 | a value is below phdr->p_vaddr, it's not in the |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1780 | executable section, but a) we shouldn't be asking for |
| 1781 | such a value anyway, and b) if we have to provide |
| 1782 | an EXIDX for such a value, then the executable's |
| 1783 | EXIDX is probably the better choice. |
| 1784 | */ |
| 1785 | DEBUG_DUMP_PHDR(phdr, "PT_LOAD", pid); |
| 1786 | if (phdr->p_vaddr + phdr->p_memsz > si->size) |
| 1787 | si->size = phdr->p_vaddr + phdr->p_memsz; |
| 1788 | /* try to remember what range of addresses should be write |
| 1789 | * protected */ |
| 1790 | if (!(phdr->p_flags & PF_W)) { |
| 1791 | unsigned _end; |
| 1792 | |
| 1793 | if (phdr->p_vaddr < si->wrprotect_start) |
| 1794 | si->wrprotect_start = phdr->p_vaddr; |
| 1795 | _end = (((phdr->p_vaddr + phdr->p_memsz + PAGE_SIZE - 1) & |
| 1796 | (~PAGE_MASK))); |
| 1797 | if (_end > si->wrprotect_end) |
| 1798 | si->wrprotect_end = _end; |
| 1799 | } |
| 1800 | } else if (phdr->p_type == PT_DYNAMIC) { |
| 1801 | if (si->dynamic != (unsigned *)-1) { |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 1802 | DL_ERR("%5d multiple PT_DYNAMIC segments found in '%s'. " |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1803 | "Segment at 0x%08x, previously one found at 0x%08x", |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1804 | pid, si->name, si->base + phdr->p_vaddr, |
| 1805 | (unsigned)si->dynamic); |
| 1806 | goto fail; |
| 1807 | } |
| 1808 | DEBUG_DUMP_PHDR(phdr, "PT_DYNAMIC", pid); |
| 1809 | si->dynamic = (unsigned *) (si->base + phdr->p_vaddr); |
| 1810 | } |
| 1811 | } |
| 1812 | } |
| 1813 | |
| 1814 | if (si->dynamic == (unsigned *)-1) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1815 | DL_ERR("%5d missing PT_DYNAMIC?!", pid); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1816 | goto fail; |
| 1817 | } |
| 1818 | |
| 1819 | DEBUG("%5d dynamic = %p\n", pid, si->dynamic); |
| 1820 | |
| 1821 | /* extract useful information from dynamic section */ |
| 1822 | for(d = si->dynamic; *d; d++){ |
| 1823 | DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]); |
| 1824 | switch(*d++){ |
| 1825 | case DT_HASH: |
| 1826 | si->nbucket = ((unsigned *) (si->base + *d))[0]; |
| 1827 | si->nchain = ((unsigned *) (si->base + *d))[1]; |
| 1828 | si->bucket = (unsigned *) (si->base + *d + 8); |
| 1829 | si->chain = (unsigned *) (si->base + *d + 8 + si->nbucket * 4); |
| 1830 | break; |
| 1831 | case DT_STRTAB: |
| 1832 | si->strtab = (const char *) (si->base + *d); |
| 1833 | break; |
| 1834 | case DT_SYMTAB: |
| 1835 | si->symtab = (Elf32_Sym *) (si->base + *d); |
| 1836 | break; |
Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1837 | #if !defined(ANDROID_SH_LINKER) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1838 | case DT_PLTREL: |
| 1839 | if(*d != DT_REL) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1840 | DL_ERR("DT_RELA not supported"); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1841 | goto fail; |
| 1842 | } |
| 1843 | break; |
Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1844 | #endif |
| 1845 | #ifdef ANDROID_SH_LINKER |
| 1846 | case DT_JMPREL: |
| 1847 | si->plt_rela = (Elf32_Rela*) (si->base + *d); |
| 1848 | break; |
| 1849 | case DT_PLTRELSZ: |
| 1850 | si->plt_rela_count = *d / sizeof(Elf32_Rela); |
| 1851 | break; |
| 1852 | #else |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1853 | case DT_JMPREL: |
| 1854 | si->plt_rel = (Elf32_Rel*) (si->base + *d); |
| 1855 | break; |
| 1856 | case DT_PLTRELSZ: |
| 1857 | si->plt_rel_count = *d / 8; |
| 1858 | break; |
Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1859 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1860 | case DT_REL: |
| 1861 | si->rel = (Elf32_Rel*) (si->base + *d); |
| 1862 | break; |
| 1863 | case DT_RELSZ: |
| 1864 | si->rel_count = *d / 8; |
| 1865 | break; |
Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1866 | #ifdef ANDROID_SH_LINKER |
| 1867 | case DT_RELASZ: |
| 1868 | si->rela_count = *d / sizeof(Elf32_Rela); |
| 1869 | break; |
| 1870 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1871 | case DT_PLTGOT: |
| 1872 | /* Save this in case we decide to do lazy binding. We don't yet. */ |
| 1873 | si->plt_got = (unsigned *)(si->base + *d); |
| 1874 | break; |
| 1875 | case DT_DEBUG: |
| 1876 | // Set the DT_DEBUG entry to the addres of _r_debug for GDB |
| 1877 | *d = (int) &_r_debug; |
| 1878 | break; |
Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1879 | #ifdef ANDROID_SH_LINKER |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1880 | case DT_RELA: |
Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1881 | si->rela = (Elf32_Rela *) (si->base + *d); |
| 1882 | break; |
| 1883 | #else |
| 1884 | case DT_RELA: |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1885 | DL_ERR("%5d DT_RELA not supported", pid); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1886 | goto fail; |
Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1887 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1888 | case DT_INIT: |
| 1889 | si->init_func = (void (*)(void))(si->base + *d); |
| 1890 | DEBUG("%5d %s constructors (init func) found at %p\n", |
| 1891 | pid, si->name, si->init_func); |
| 1892 | break; |
| 1893 | case DT_FINI: |
| 1894 | si->fini_func = (void (*)(void))(si->base + *d); |
| 1895 | DEBUG("%5d %s destructors (fini func) found at %p\n", |
| 1896 | pid, si->name, si->fini_func); |
| 1897 | break; |
| 1898 | case DT_INIT_ARRAY: |
| 1899 | si->init_array = (unsigned *)(si->base + *d); |
| 1900 | DEBUG("%5d %s constructors (init_array) found at %p\n", |
| 1901 | pid, si->name, si->init_array); |
| 1902 | break; |
| 1903 | case DT_INIT_ARRAYSZ: |
| 1904 | si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr); |
| 1905 | break; |
| 1906 | case DT_FINI_ARRAY: |
| 1907 | si->fini_array = (unsigned *)(si->base + *d); |
| 1908 | DEBUG("%5d %s destructors (fini_array) found at %p\n", |
| 1909 | pid, si->name, si->fini_array); |
| 1910 | break; |
| 1911 | case DT_FINI_ARRAYSZ: |
| 1912 | si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr); |
| 1913 | break; |
| 1914 | case DT_PREINIT_ARRAY: |
| 1915 | si->preinit_array = (unsigned *)(si->base + *d); |
| 1916 | DEBUG("%5d %s constructors (preinit_array) found at %p\n", |
| 1917 | pid, si->name, si->preinit_array); |
| 1918 | break; |
| 1919 | case DT_PREINIT_ARRAYSZ: |
| 1920 | si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr); |
| 1921 | break; |
| 1922 | case DT_TEXTREL: |
| 1923 | /* TODO: make use of this. */ |
| 1924 | /* this means that we might have to write into where the text |
| 1925 | * segment was loaded during relocation... Do something with |
| 1926 | * it. |
| 1927 | */ |
| 1928 | DEBUG("%5d Text segment should be writable during relocation.\n", |
| 1929 | pid); |
| 1930 | break; |
| 1931 | } |
| 1932 | } |
| 1933 | |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 1934 | 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] | 1935 | pid, si->base, si->strtab, si->symtab); |
| 1936 | |
| 1937 | if((si->strtab == 0) || (si->symtab == 0)) { |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1938 | DL_ERR("%5d missing essential tables", pid); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1939 | goto fail; |
| 1940 | } |
| 1941 | |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 1942 | /* if this is the main executable, then load all of the preloads now */ |
| 1943 | if(si->flags & FLAG_EXE) { |
| 1944 | int i; |
| 1945 | memset(preloads, 0, sizeof(preloads)); |
| 1946 | for(i = 0; ldpreload_names[i] != NULL; i++) { |
| 1947 | soinfo *lsi = find_library(ldpreload_names[i]); |
| 1948 | if(lsi == 0) { |
| 1949 | strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf)); |
| 1950 | DL_ERR("%5d could not load needed library '%s' for '%s' (%s)", |
| 1951 | pid, ldpreload_names[i], si->name, tmp_err_buf); |
| 1952 | goto fail; |
| 1953 | } |
| 1954 | lsi->refcount++; |
| 1955 | preloads[i] = lsi; |
| 1956 | } |
| 1957 | } |
| 1958 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1959 | for(d = si->dynamic; *d; d += 2) { |
| 1960 | if(d[0] == DT_NEEDED){ |
| 1961 | 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] | 1962 | soinfo *lsi = find_library(si->strtab + d[1]); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1963 | if(lsi == 0) { |
Dima Zavin | 0353195 | 2009-05-29 17:30:25 -0700 | [diff] [blame] | 1964 | strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf)); |
Erik Gilling | d00d23a | 2009-07-22 17:06:11 -0700 | [diff] [blame] | 1965 | DL_ERR("%5d could not load needed library '%s' for '%s' (%s)", |
Dima Zavin | 0353195 | 2009-05-29 17:30:25 -0700 | [diff] [blame] | 1966 | 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] | 1967 | goto fail; |
| 1968 | } |
Iliyan Malchev | 6ed80c8 | 2009-09-28 19:38:04 -0700 | [diff] [blame] | 1969 | /* Save the soinfo of the loaded DT_NEEDED library in the payload |
| 1970 | of the DT_NEEDED entry itself, so that we can retrieve the |
| 1971 | soinfo directly later from the dynamic segment. This is a hack, |
| 1972 | but it allows us to map from DT_NEEDED to soinfo efficiently |
| 1973 | later on when we resolve relocations, trying to look up a symgol |
| 1974 | with dlsym(). |
| 1975 | */ |
| 1976 | d[1] = (unsigned)lsi; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 1977 | lsi->refcount++; |
| 1978 | } |
| 1979 | } |
| 1980 | |
| 1981 | if(si->plt_rel) { |
| 1982 | DEBUG("[ %5d relocating %s plt ]\n", pid, si->name ); |
| 1983 | if(reloc_library(si, si->plt_rel, si->plt_rel_count)) |
| 1984 | goto fail; |
| 1985 | } |
| 1986 | if(si->rel) { |
| 1987 | DEBUG("[ %5d relocating %s ]\n", pid, si->name ); |
| 1988 | if(reloc_library(si, si->rel, si->rel_count)) |
| 1989 | goto fail; |
| 1990 | } |
| 1991 | |
Shin-ichiro KAWASAKI | ad13c57 | 2009-11-06 10:36:37 +0900 | [diff] [blame] | 1992 | #ifdef ANDROID_SH_LINKER |
| 1993 | if(si->plt_rela) { |
| 1994 | DEBUG("[ %5d relocating %s plt ]\n", pid, si->name ); |
| 1995 | if(reloc_library_a(si, si->plt_rela, si->plt_rela_count)) |
| 1996 | goto fail; |
| 1997 | } |
| 1998 | if(si->rela) { |
| 1999 | DEBUG("[ %5d relocating %s ]\n", pid, si->name ); |
| 2000 | if(reloc_library_a(si, si->rela, si->rela_count)) |
| 2001 | goto fail; |
| 2002 | } |
| 2003 | #endif /* ANDROID_SH_LINKER */ |
| 2004 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2005 | si->flags |= FLAG_LINKED; |
| 2006 | DEBUG("[ %5d finished linking %s ]\n", pid, si->name); |
| 2007 | |
| 2008 | #if 0 |
| 2009 | /* This is the way that the old dynamic linker did protection of |
| 2010 | * non-writable areas. It would scan section headers and find where |
| 2011 | * .text ended (rather where .data/.bss began) and assume that this is |
| 2012 | * the upper range of the non-writable area. This is too coarse, |
| 2013 | * and is kept here for reference until we fully move away from single |
| 2014 | * segment elf objects. See the code in get_wr_offset (also #if'd 0) |
| 2015 | * that made this possible. |
| 2016 | */ |
| 2017 | if(wr_offset < 0xffffffff){ |
| 2018 | mprotect((void*) si->base, wr_offset, PROT_READ | PROT_EXEC); |
| 2019 | } |
| 2020 | #else |
| 2021 | /* TODO: Verify that this does the right thing in all cases, as it |
| 2022 | * presently probably does not. It is possible that an ELF image will |
| 2023 | * come with multiple read-only segments. What we ought to do is scan |
| 2024 | * the program headers again and mprotect all the read-only segments. |
| 2025 | * To prevent re-scanning the program header, we would have to build a |
| 2026 | * list of loadable segments in si, and then scan that instead. */ |
| 2027 | if (si->wrprotect_start != 0xffffffff && si->wrprotect_end != 0) { |
| 2028 | mprotect((void *)si->wrprotect_start, |
| 2029 | si->wrprotect_end - si->wrprotect_start, |
| 2030 | PROT_READ | PROT_EXEC); |
| 2031 | } |
| 2032 | #endif |
| 2033 | |
| 2034 | /* If this is a SET?ID program, dup /dev/null to opened stdin, |
| 2035 | stdout and stderr to close a security hole described in: |
| 2036 | |
| 2037 | ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc |
| 2038 | |
| 2039 | */ |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2040 | if (program_is_setuid) |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2041 | nullify_closed_stdio (); |
| 2042 | call_constructors(si); |
| 2043 | notify_gdb_of_load(si); |
| 2044 | return 0; |
| 2045 | |
| 2046 | fail: |
Dima Zavin | a716190 | 2010-08-17 15:56:40 -0700 | [diff] [blame] | 2047 | ERROR("failed to link %s\n", si->name); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2048 | si->flags |= FLAG_ERROR; |
| 2049 | return -1; |
| 2050 | } |
| 2051 | |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2052 | static void parse_library_path(const char *path, char *delim) |
David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 2053 | { |
| 2054 | size_t len; |
| 2055 | char *ldpaths_bufp = ldpaths_buf; |
| 2056 | int i = 0; |
| 2057 | |
| 2058 | len = strlcpy(ldpaths_buf, path, sizeof(ldpaths_buf)); |
| 2059 | |
| 2060 | while (i < LDPATH_MAX && (ldpaths[i] = strsep(&ldpaths_bufp, delim))) { |
| 2061 | if (*ldpaths[i] != '\0') |
| 2062 | ++i; |
| 2063 | } |
| 2064 | |
| 2065 | /* Forget the last path if we had to truncate; this occurs if the 2nd to |
| 2066 | * last char isn't '\0' (i.e. not originally a delim). */ |
| 2067 | if (i > 0 && len >= sizeof(ldpaths_buf) && |
| 2068 | ldpaths_buf[sizeof(ldpaths_buf) - 2] != '\0') { |
| 2069 | ldpaths[i - 1] = NULL; |
| 2070 | } else { |
| 2071 | ldpaths[i] = NULL; |
| 2072 | } |
| 2073 | } |
| 2074 | |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2075 | static void parse_preloads(const char *path, char *delim) |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 2076 | { |
| 2077 | size_t len; |
| 2078 | char *ldpreloads_bufp = ldpreloads_buf; |
| 2079 | int i = 0; |
| 2080 | |
| 2081 | len = strlcpy(ldpreloads_buf, path, sizeof(ldpreloads_buf)); |
| 2082 | |
| 2083 | while (i < LDPRELOAD_MAX && (ldpreload_names[i] = strsep(&ldpreloads_bufp, delim))) { |
| 2084 | if (*ldpreload_names[i] != '\0') { |
| 2085 | ++i; |
| 2086 | } |
| 2087 | } |
| 2088 | |
| 2089 | /* Forget the last path if we had to truncate; this occurs if the 2nd to |
| 2090 | * last char isn't '\0' (i.e. not originally a delim). */ |
| 2091 | if (i > 0 && len >= sizeof(ldpreloads_buf) && |
| 2092 | ldpreloads_buf[sizeof(ldpreloads_buf) - 2] != '\0') { |
| 2093 | ldpreload_names[i - 1] = NULL; |
| 2094 | } else { |
| 2095 | ldpreload_names[i] = NULL; |
| 2096 | } |
| 2097 | } |
| 2098 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2099 | int main(int argc, char **argv) |
| 2100 | { |
| 2101 | return 0; |
| 2102 | } |
| 2103 | |
| 2104 | #define ANDROID_TLS_SLOTS BIONIC_TLS_SLOTS |
| 2105 | |
| 2106 | static void * __tls_area[ANDROID_TLS_SLOTS]; |
| 2107 | |
| 2108 | unsigned __linker_init(unsigned **elfdata) |
| 2109 | { |
| 2110 | static soinfo linker_soinfo; |
| 2111 | |
| 2112 | int argc = (int) *elfdata; |
| 2113 | char **argv = (char**) (elfdata + 1); |
| 2114 | unsigned *vecs = (unsigned*) (argv + argc + 1); |
| 2115 | soinfo *si; |
| 2116 | struct link_map * map; |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2117 | const char *ldpath_env = NULL; |
| 2118 | const char *ldpreload_env = NULL; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2119 | |
David 'Digit' Turner | ef0bd18 | 2009-07-17 17:55:01 +0200 | [diff] [blame] | 2120 | /* Setup a temporary TLS area that is used to get a working |
| 2121 | * errno for system calls. |
| 2122 | */ |
| 2123 | __set_tls(__tls_area); |
| 2124 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2125 | pid = getpid(); |
| 2126 | |
| 2127 | #if TIMING |
| 2128 | struct timeval t0, t1; |
| 2129 | gettimeofday(&t0, 0); |
| 2130 | #endif |
| 2131 | |
David 'Digit' Turner | ef0bd18 | 2009-07-17 17:55:01 +0200 | [diff] [blame] | 2132 | /* NOTE: we store the elfdata pointer on a special location |
| 2133 | * of the temporary TLS area in order to pass it to |
| 2134 | * the C Library's runtime initializer. |
| 2135 | * |
| 2136 | * The initializer must clear the slot and reset the TLS |
| 2137 | * to point to a different location to ensure that no other |
| 2138 | * shared library constructor can access it. |
| 2139 | */ |
| 2140 | __tls_area[TLS_SLOT_BIONIC_PREINIT] = elfdata; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2141 | |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2142 | /* Are we setuid? */ |
| 2143 | program_is_setuid = (getuid() != geteuid()) || (getgid() != getegid()); |
| 2144 | |
| 2145 | /* Initialize environment functions, and get to the ELF aux vectors table */ |
| 2146 | vecs = linker_env_init(vecs); |
| 2147 | |
| 2148 | /* Sanitize environment if we're loading a setuid program */ |
| 2149 | if (program_is_setuid) |
| 2150 | linker_env_secure(); |
| 2151 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2152 | debugger_init(); |
| 2153 | |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2154 | /* Get a few environment variables */ |
| 2155 | { |
| 2156 | const char* env; |
| 2157 | env = linker_env_get("DEBUG"); /* XXX: TODO: Change to LD_DEBUG */ |
| 2158 | if (env) |
| 2159 | debug_verbosity = atoi(env); |
| 2160 | |
| 2161 | /* Normally, these are cleaned by linker_env_secure, but the test |
| 2162 | * against program_is_setuid doesn't cost us anything */ |
| 2163 | if (!program_is_setuid) { |
| 2164 | ldpath_env = linker_env_get("LD_LIBRARY_PATH"); |
| 2165 | ldpreload_env = linker_env_get("LD_PRELOAD"); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2166 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2167 | } |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2168 | |
| 2169 | INFO("[ android linker & debugger ]\n"); |
| 2170 | DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata); |
| 2171 | |
| 2172 | si = alloc_info(argv[0]); |
| 2173 | if(si == 0) { |
| 2174 | exit(-1); |
| 2175 | } |
| 2176 | |
| 2177 | /* bootstrap the link map, the main exe always needs to be first */ |
| 2178 | si->flags |= FLAG_EXE; |
| 2179 | map = &(si->linkmap); |
| 2180 | |
| 2181 | map->l_addr = 0; |
| 2182 | map->l_name = argv[0]; |
| 2183 | map->l_prev = NULL; |
| 2184 | map->l_next = NULL; |
| 2185 | |
| 2186 | _r_debug.r_map = map; |
| 2187 | r_debug_tail = map; |
| 2188 | |
| 2189 | /* gdb expects the linker to be in the debug shared object list, |
| 2190 | * and we need to make sure that the reported load address is zero. |
| 2191 | * Without this, gdb gets the wrong idea of where rtld_db_dlactivity() |
| 2192 | * is. Don't use alloc_info(), because the linker shouldn't |
| 2193 | * be on the soinfo list. |
| 2194 | */ |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2195 | 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] | 2196 | linker_soinfo.flags = 0; |
| 2197 | linker_soinfo.base = 0; // This is the important part; must be zero. |
| 2198 | insert_soinfo_into_debug_map(&linker_soinfo); |
| 2199 | |
| 2200 | /* extract information passed from the kernel */ |
| 2201 | while(vecs[0] != 0){ |
| 2202 | switch(vecs[0]){ |
| 2203 | case AT_PHDR: |
| 2204 | si->phdr = (Elf32_Phdr*) vecs[1]; |
| 2205 | break; |
| 2206 | case AT_PHNUM: |
| 2207 | si->phnum = (int) vecs[1]; |
| 2208 | break; |
| 2209 | case AT_ENTRY: |
| 2210 | si->entry = vecs[1]; |
| 2211 | break; |
| 2212 | } |
| 2213 | vecs += 2; |
| 2214 | } |
| 2215 | |
Iliyan Malchev | e100f52 | 2010-02-10 15:19:37 -0800 | [diff] [blame] | 2216 | ba_init(&ba_nonprelink); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2217 | |
| 2218 | si->base = 0; |
| 2219 | si->dynamic = (unsigned *)-1; |
| 2220 | si->wrprotect_start = 0xffffffff; |
| 2221 | si->wrprotect_end = 0; |
David 'Digit' Turner | 6774809 | 2010-07-21 16:18:21 -0700 | [diff] [blame] | 2222 | si->refcount = 1; |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2223 | |
David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 2224 | /* Use LD_LIBRARY_PATH if we aren't setuid/setgid */ |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2225 | if (ldpath_env) |
David Bartley | bc3a5c2 | 2009-06-02 18:27:28 -0700 | [diff] [blame] | 2226 | parse_library_path(ldpath_env, ":"); |
| 2227 | |
David 'Digit' Turner | be57559 | 2010-12-16 19:52:02 +0100 | [diff] [blame] | 2228 | if (ldpreload_env) { |
Matt Fischer | 4fd42c1 | 2009-12-31 12:09:10 -0600 | [diff] [blame] | 2229 | parse_preloads(ldpreload_env, " :"); |
| 2230 | } |
| 2231 | |
Dima Zavin | 2e85579 | 2009-05-20 18:28:09 -0700 | [diff] [blame] | 2232 | if(link_image(si, 0)) { |
| 2233 | char errmsg[] = "CANNOT LINK EXECUTABLE\n"; |
| 2234 | write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf)); |
| 2235 | write(2, errmsg, sizeof(errmsg)); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2236 | exit(-1); |
| 2237 | } |
| 2238 | |
Iliyan Malchev | 4a9afcb | 2009-09-29 11:43:20 -0700 | [diff] [blame] | 2239 | #if ALLOW_SYMBOLS_FROM_MAIN |
| 2240 | /* Set somain after we've loaded all the libraries in order to prevent |
| 2241 | * linking of symbols back to the main image, which is not set up at that |
| 2242 | * point yet. |
| 2243 | */ |
| 2244 | somain = si; |
| 2245 | #endif |
| 2246 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 2247 | #if TIMING |
| 2248 | gettimeofday(&t1,NULL); |
| 2249 | PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) ( |
| 2250 | (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) - |
| 2251 | (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec) |
| 2252 | )); |
| 2253 | #endif |
| 2254 | #if STATS |
| 2255 | PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0], |
| 2256 | linker_stats.reloc[RELOC_ABSOLUTE], |
| 2257 | linker_stats.reloc[RELOC_RELATIVE], |
| 2258 | linker_stats.reloc[RELOC_COPY], |
| 2259 | linker_stats.reloc[RELOC_SYMBOL]); |
| 2260 | #endif |
| 2261 | #if COUNT_PAGES |
| 2262 | { |
| 2263 | unsigned n; |
| 2264 | unsigned i; |
| 2265 | unsigned count = 0; |
| 2266 | for(n = 0; n < 4096; n++){ |
| 2267 | if(bitmask[n]){ |
| 2268 | unsigned x = bitmask[n]; |
| 2269 | for(i = 0; i < 8; i++){ |
| 2270 | if(x & 1) count++; |
| 2271 | x >>= 1; |
| 2272 | } |
| 2273 | } |
| 2274 | } |
| 2275 | PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4); |
| 2276 | } |
| 2277 | #endif |
| 2278 | |
| 2279 | #if TIMING || STATS || COUNT_PAGES |
| 2280 | fflush(stdout); |
| 2281 | #endif |
| 2282 | |
| 2283 | TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name, |
| 2284 | si->entry); |
| 2285 | return si->entry; |
| 2286 | } |