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