linker.cpp revision 24053a461e7a20f34002262c1bb122023134989d
1/*
2 * Copyright (C) 2008, 2009 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 <dlfcn.h>
30#include <errno.h>
31#include <fcntl.h>
32#include <linux/auxvec.h>
33#include <pthread.h>
34#include <stdio.h>
35#include <stdlib.h>
36#include <string.h>
37#include <sys/atomics.h>
38#include <sys/mman.h>
39#include <sys/stat.h>
40#include <unistd.h>
41
42// Private C library headers.
43#include <private/bionic_tls.h>
44#include <private/KernelArgumentBlock.h>
45#include <private/ScopedPthreadMutexLocker.h>
46
47#include "linker.h"
48#include "linker_debug.h"
49#include "linker_environ.h"
50#include "linker_phdr.h"
51
52/* Assume average path length of 64 and max 8 paths */
53#define LDPATH_BUFSIZE 512
54#define LDPATH_MAX 8
55
56#define LDPRELOAD_BUFSIZE 512
57#define LDPRELOAD_MAX 8
58
59/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
60 *
61 * Do NOT use malloc() and friends or pthread_*() code here.
62 * Don't use printf() either; it's caused mysterious memory
63 * corruption in the past.
64 * The linker runs before we bring up libc and it's easiest
65 * to make sure it does not depend on any complex libc features
66 *
67 * open issues / todo:
68 *
69 * - are we doing everything we should for ARM_COPY relocations?
70 * - cleaner error reporting
71 * - after linking, set as much stuff as possible to READONLY
72 *   and NOEXEC
73 */
74
75static bool soinfo_link_image(soinfo* si);
76
77// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
78// maps, each a single page in size. The pages are broken up into as many struct soinfo
79// objects as will fit, and they're all threaded together on a free list.
80#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
81struct soinfo_pool_t {
82  soinfo_pool_t* next;
83  soinfo info[SOINFO_PER_POOL];
84};
85static struct soinfo_pool_t* gSoInfoPools = NULL;
86static soinfo* gSoInfoFreeList = NULL;
87
88static soinfo* solist = &libdl_info;
89static soinfo* sonext = &libdl_info;
90static soinfo* somain; /* main process, always the one after libdl_info */
91
92static const char* const gSoPaths[] = {
93  "/vendor/lib",
94  "/system/lib",
95  NULL
96};
97
98static char gLdPathsBuffer[LDPATH_BUFSIZE];
99static const char* gLdPaths[LDPATH_MAX + 1];
100
101static char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
102static const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
103
104static soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
105
106__LIBC_HIDDEN__ int gLdDebugVerbosity;
107
108__LIBC_HIDDEN__ abort_msg_t* gAbortMessage = NULL; // For debuggerd.
109
110enum RelocationKind {
111    kRelocAbsolute = 0,
112    kRelocRelative,
113    kRelocCopy,
114    kRelocSymbol,
115    kRelocMax
116};
117
118#if STATS
119struct linker_stats_t {
120    int count[kRelocMax];
121};
122
123static linker_stats_t linker_stats;
124
125static void count_relocation(RelocationKind kind) {
126    ++linker_stats.count[kind];
127}
128#else
129static void count_relocation(RelocationKind) {
130}
131#endif
132
133#if COUNT_PAGES
134static unsigned bitmask[4096];
135#define MARK(offset) \
136    do { \
137        bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
138    } while(0)
139#else
140#define MARK(x) do {} while (0)
141#endif
142
143// You shouldn't try to call memory-allocating functions in the dynamic linker.
144// Guard against the most obvious ones.
145#define DISALLOW_ALLOCATION(return_type, name, ...) \
146    return_type name __VA_ARGS__ \
147    { \
148        const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
149        __libc_format_log(ANDROID_LOG_FATAL, "linker", "%s", msg); \
150        write(2, msg, strlen(msg)); \
151        abort(); \
152    }
153#define UNUSED __attribute__((unused))
154DISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
155DISALLOW_ALLOCATION(void, free, (void* u UNUSED));
156DISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
157DISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
158
159static char tmp_err_buf[768];
160static char __linker_dl_err_buf[768];
161
162char* linker_get_error_buffer() {
163  return &__linker_dl_err_buf[0];
164}
165
166size_t linker_get_error_buffer_size() {
167  return sizeof(__linker_dl_err_buf);
168}
169
170/*
171 * This function is an empty stub where GDB locates a breakpoint to get notified
172 * about linker activity.
173 */
174extern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
175
176static r_debug _r_debug = {1, NULL, &rtld_db_dlactivity, RT_CONSISTENT, 0};
177static link_map_t* r_debug_tail = 0;
178
179static pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
180
181static void insert_soinfo_into_debug_map(soinfo * info) {
182    // Copy the necessary fields into the debug structure.
183    link_map_t* map = &(info->link_map);
184    map->l_addr = info->base;
185    map->l_name = (char*) info->name;
186    map->l_ld = (uintptr_t)info->dynamic;
187
188    /* Stick the new library at the end of the list.
189     * gdb tends to care more about libc than it does
190     * about leaf libraries, and ordering it this way
191     * reduces the back-and-forth over the wire.
192     */
193    if (r_debug_tail) {
194        r_debug_tail->l_next = map;
195        map->l_prev = r_debug_tail;
196        map->l_next = 0;
197    } else {
198        _r_debug.r_map = map;
199        map->l_prev = 0;
200        map->l_next = 0;
201    }
202    r_debug_tail = map;
203}
204
205static void remove_soinfo_from_debug_map(soinfo* info) {
206    link_map_t* map = &(info->link_map);
207
208    if (r_debug_tail == map) {
209        r_debug_tail = map->l_prev;
210    }
211
212    if (map->l_prev) {
213        map->l_prev->l_next = map->l_next;
214    }
215    if (map->l_next) {
216        map->l_next->l_prev = map->l_prev;
217    }
218}
219
220static void notify_gdb_of_load(soinfo* info) {
221    if (info->flags & FLAG_EXE) {
222        // GDB already knows about the main executable
223        return;
224    }
225
226    ScopedPthreadMutexLocker locker(&gDebugMutex);
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}
236
237static void notify_gdb_of_unload(soinfo* info) {
238    if (info->flags & FLAG_EXE) {
239        // GDB already knows about the main executable
240        return;
241    }
242
243    ScopedPthreadMutexLocker locker(&gDebugMutex);
244
245    _r_debug.r_state = RT_DELETE;
246    rtld_db_dlactivity();
247
248    remove_soinfo_from_debug_map(info);
249
250    _r_debug.r_state = RT_CONSISTENT;
251    rtld_db_dlactivity();
252}
253
254void notify_gdb_of_libraries() {
255    _r_debug.r_state = RT_ADD;
256    rtld_db_dlactivity();
257    _r_debug.r_state = RT_CONSISTENT;
258    rtld_db_dlactivity();
259}
260
261static bool ensure_free_list_non_empty() {
262  if (gSoInfoFreeList != NULL) {
263    return true;
264  }
265
266  // Allocate a new pool.
267  soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
268                                                              PROT_READ|PROT_WRITE,
269                                                              MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
270  if (pool == MAP_FAILED) {
271    return false;
272  }
273
274  // Add the pool to our list of pools.
275  pool->next = gSoInfoPools;
276  gSoInfoPools = pool;
277
278  // Chain the entries in the new pool onto the free list.
279  gSoInfoFreeList = &pool->info[0];
280  soinfo* next = NULL;
281  for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
282    pool->info[i].next = next;
283    next = &pool->info[i];
284  }
285
286  return true;
287}
288
289static void set_soinfo_pool_protection(int protection) {
290  for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
291    if (mprotect(p, sizeof(*p), protection) == -1) {
292      abort(); // Can't happen.
293    }
294  }
295}
296
297static soinfo* soinfo_alloc(const char* name) {
298  if (strlen(name) >= SOINFO_NAME_LEN) {
299    DL_ERR("library name \"%s\" too long", name);
300    return NULL;
301  }
302
303  if (!ensure_free_list_non_empty()) {
304    DL_ERR("out of memory when loading \"%s\"", name);
305    return NULL;
306  }
307
308  // Take the head element off the free list.
309  soinfo* si = gSoInfoFreeList;
310  gSoInfoFreeList = gSoInfoFreeList->next;
311
312  // Initialize the new element.
313  memset(si, 0, sizeof(soinfo));
314  strlcpy(si->name, name, sizeof(si->name));
315  sonext->next = si;
316  sonext = si;
317
318  TRACE("name %s: allocated soinfo @ %p", name, si);
319  return si;
320}
321
322static void soinfo_free(soinfo* si)
323{
324    if (si == NULL) {
325        return;
326    }
327
328    soinfo *prev = NULL, *trav;
329
330    TRACE("name %s: freeing soinfo @ %p", si->name, si);
331
332    for (trav = solist; trav != NULL; trav = trav->next) {
333        if (trav == si)
334            break;
335        prev = trav;
336    }
337    if (trav == NULL) {
338        /* si was not in solist */
339        DL_ERR("name \"%s\" is not in solist!", si->name);
340        return;
341    }
342
343    /* prev will never be NULL, because the first entry in solist is
344       always the static libdl_info.
345    */
346    prev->next = si->next;
347    if (si == sonext) {
348        sonext = prev;
349    }
350    si->next = gSoInfoFreeList;
351    gSoInfoFreeList = si;
352}
353
354
355static void parse_path(const char* path, const char* delimiters,
356                       const char** array, char* buf, size_t buf_size, size_t max_count) {
357  if (path == NULL) {
358    return;
359  }
360
361  size_t len = strlcpy(buf, path, buf_size);
362
363  size_t i = 0;
364  char* buf_p = buf;
365  while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
366    if (*array[i] != '\0') {
367      ++i;
368    }
369  }
370
371  // Forget the last path if we had to truncate; this occurs if the 2nd to
372  // last char isn't '\0' (i.e. wasn't originally a delimiter).
373  if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
374    array[i - 1] = NULL;
375  } else {
376    array[i] = NULL;
377  }
378}
379
380static void parse_LD_LIBRARY_PATH(const char* path) {
381  parse_path(path, ":", gLdPaths,
382             gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
383}
384
385static void parse_LD_PRELOAD(const char* path) {
386  // We have historically supported ':' as well as ' ' in LD_PRELOAD.
387  parse_path(path, " :", gLdPreloadNames,
388             gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
389}
390
391#ifdef ANDROID_ARM_LINKER
392
393/* For a given PC, find the .so that it belongs to.
394 * Returns the base address of the .ARM.exidx section
395 * for that .so, and the number of 8-byte entries
396 * in that section (via *pcount).
397 *
398 * Intended to be called by libc's __gnu_Unwind_Find_exidx().
399 *
400 * This function is exposed via dlfcn.cpp and libdl.so.
401 */
402_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
403{
404    soinfo *si;
405    unsigned addr = (unsigned)pc;
406
407    for (si = solist; si != 0; si = si->next){
408        if ((addr >= si->base) && (addr < (si->base + si->size))) {
409            *pcount = si->ARM_exidx_count;
410            return (_Unwind_Ptr)si->ARM_exidx;
411        }
412    }
413   *pcount = 0;
414    return NULL;
415}
416
417#endif
418
419/* Here, we only have to provide a callback to iterate across all the
420 * loaded libraries. gcc_eh does the rest. */
421int
422dl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
423                void *data)
424{
425    int rv = 0;
426    for (soinfo* si = solist; si != NULL; si = si->next) {
427        dl_phdr_info dl_info;
428        dl_info.dlpi_addr = si->link_map.l_addr;
429        dl_info.dlpi_name = si->link_map.l_name;
430        dl_info.dlpi_phdr = si->phdr;
431        dl_info.dlpi_phnum = si->phnum;
432        rv = cb(&dl_info, sizeof(dl_phdr_info), data);
433        if (rv != 0) {
434            break;
435        }
436    }
437    return rv;
438}
439
440static Elf32_Sym* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
441    Elf32_Sym* symtab = si->symtab;
442    const char* strtab = si->strtab;
443
444    TRACE_TYPE(LOOKUP, "SEARCH %s in %s@0x%08x %08x %d",
445               name, si->name, si->base, hash, hash % si->nbucket);
446
447    for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
448        Elf32_Sym* s = symtab + n;
449        if (strcmp(strtab + s->st_name, name)) continue;
450
451            /* only concern ourselves with global and weak symbol definitions */
452        switch(ELF32_ST_BIND(s->st_info)){
453        case STB_GLOBAL:
454        case STB_WEAK:
455            if (s->st_shndx == SHN_UNDEF) {
456                continue;
457            }
458
459            TRACE_TYPE(LOOKUP, "FOUND %s in %s (%08x) %d",
460                       name, si->name, s->st_value, s->st_size);
461            return s;
462        }
463    }
464
465    return NULL;
466}
467
468static unsigned elfhash(const char* _name) {
469    const unsigned char* name = (const unsigned char*) _name;
470    unsigned h = 0, g;
471
472    while(*name) {
473        h = (h << 4) + *name++;
474        g = h & 0xf0000000;
475        h ^= g;
476        h ^= g >> 24;
477    }
478    return h;
479}
480
481static Elf32_Sym* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
482    unsigned elf_hash = elfhash(name);
483    Elf32_Sym* s = NULL;
484
485    if (si != NULL && somain != NULL) {
486
487        /*
488         * Local scope is executable scope. Just start looking into it right away
489         * for the shortcut.
490         */
491
492        if (si == somain) {
493            s = soinfo_elf_lookup(si, elf_hash, name);
494            if (s != NULL) {
495                *lsi = si;
496                goto done;
497            }
498        } else {
499            /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
500
501            /*
502             * If this object was built with symbolic relocations disabled, the
503             * first place to look to resolve external references is the main
504             * executable.
505             */
506
507            if (!si->has_DT_SYMBOLIC) {
508                DEBUG("%s: looking up %s in executable %s",
509                      si->name, name, somain->name);
510                s = soinfo_elf_lookup(somain, elf_hash, name);
511                if (s != NULL) {
512                    *lsi = somain;
513                    goto done;
514                }
515            }
516
517            /* Look for symbols in the local scope (the object who is
518             * searching). This happens with C++ templates on i386 for some
519             * reason.
520             *
521             * Notes on weak symbols:
522             * The ELF specs are ambiguous about treatment of weak definitions in
523             * dynamic linking.  Some systems return the first definition found
524             * and some the first non-weak definition.   This is system dependent.
525             * Here we return the first definition found for simplicity.  */
526
527            s = soinfo_elf_lookup(si, elf_hash, name);
528            if (s != NULL) {
529                *lsi = si;
530                goto done;
531            }
532
533            /*
534             * If this object was built with -Bsymbolic and symbol is not found
535             * in the local scope, try to find the symbol in the main executable.
536             */
537
538            if (si->has_DT_SYMBOLIC) {
539                DEBUG("%s: looking up %s in executable %s after local scope",
540                      si->name, name, somain->name);
541                s = soinfo_elf_lookup(somain, elf_hash, name);
542                if (s != NULL) {
543                    *lsi = somain;
544                    goto done;
545                }
546            }
547        }
548    }
549
550    /* Next, look for it in the preloads list */
551    for (int i = 0; gLdPreloads[i] != NULL; i++) {
552        s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name);
553        if (s != NULL) {
554            *lsi = gLdPreloads[i];
555            goto done;
556        }
557    }
558
559    for (int i = 0; needed[i] != NULL; i++) {
560        DEBUG("%s: looking up %s in %s",
561              si->name, name, needed[i]->name);
562        s = soinfo_elf_lookup(needed[i], elf_hash, name);
563        if (s != NULL) {
564            *lsi = needed[i];
565            goto done;
566        }
567    }
568
569done:
570    if (s != NULL) {
571        TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = 0x%08x, "
572                   "found in %s, base = 0x%08x, load bias = 0x%08x",
573                   si->name, name, s->st_value,
574                   (*lsi)->name, (*lsi)->base, (*lsi)->load_bias);
575        return s;
576    }
577
578    return NULL;
579}
580
581/* This is used by dlsym(3).  It performs symbol lookup only within the
582   specified soinfo object and not in any of its dependencies.
583
584   TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
585   that it should do a breadth first search through the dependency
586   tree. This agrees with the ELF spec (aka System V Application
587   Binary Interface) where in Chapter 5 it discuss resolving "Shared
588   Object Dependencies" in breadth first search order.
589 */
590Elf32_Sym* dlsym_handle_lookup(soinfo* si, const char* name)
591{
592    return soinfo_elf_lookup(si, elfhash(name), name);
593}
594
595/* This is used by dlsym(3) to performs a global symbol lookup. If the
596   start value is null (for RTLD_DEFAULT), the search starts at the
597   beginning of the global solist. Otherwise the search starts at the
598   specified soinfo (for RTLD_NEXT).
599 */
600Elf32_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
601  unsigned elf_hash = elfhash(name);
602
603  if (start == NULL) {
604    start = solist;
605  }
606
607  Elf32_Sym* s = NULL;
608  for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
609    s = soinfo_elf_lookup(si, elf_hash, name);
610    if (s != NULL) {
611      *found = si;
612      break;
613    }
614  }
615
616  if (s != NULL) {
617    TRACE_TYPE(LOOKUP, "%s s->st_value = 0x%08x, found->base = 0x%08x",
618               name, s->st_value, (*found)->base);
619  }
620
621  return s;
622}
623
624soinfo* find_containing_library(const void* p) {
625  Elf32_Addr address = reinterpret_cast<Elf32_Addr>(p);
626  for (soinfo* si = solist; si != NULL; si = si->next) {
627    if (address >= si->base && address - si->base < si->size) {
628      return si;
629    }
630  }
631  return NULL;
632}
633
634Elf32_Sym* dladdr_find_symbol(soinfo* si, const void* addr) {
635  Elf32_Addr soaddr = reinterpret_cast<Elf32_Addr>(addr) - si->base;
636
637  // Search the library's symbol table for any defined symbol which
638  // contains this address.
639  for (size_t i = 0; i < si->nchain; ++i) {
640    Elf32_Sym* sym = &si->symtab[i];
641    if (sym->st_shndx != SHN_UNDEF &&
642        soaddr >= sym->st_value &&
643        soaddr < sym->st_value + sym->st_size) {
644      return sym;
645    }
646  }
647
648  return NULL;
649}
650
651#if 0
652static void dump(soinfo* si)
653{
654    Elf32_Sym* s = si->symtab;
655    for (unsigned n = 0; n < si->nchain; n++) {
656        TRACE("%04d> %08x: %02x %04x %08x %08x %s", n, s,
657               s->st_info, s->st_shndx, s->st_value, s->st_size,
658               si->strtab + s->st_name);
659        s++;
660    }
661}
662#endif
663
664static int open_library_on_path(const char* name, const char* const paths[]) {
665  char buf[512];
666  for (size_t i = 0; paths[i] != NULL; ++i) {
667    int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
668    if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
669      PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
670      continue;
671    }
672    int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
673    if (fd != -1) {
674      return fd;
675    }
676  }
677  return -1;
678}
679
680static int open_library(const char* name) {
681  TRACE("[ opening %s ]", name);
682
683  // If the name contains a slash, we should attempt to open it directly and not search the paths.
684  if (strchr(name, '/') != NULL) {
685    int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
686    if (fd != -1) {
687      return fd;
688    }
689    // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
690  }
691
692  // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
693  int fd = open_library_on_path(name, gLdPaths);
694  if (fd == -1) {
695    fd = open_library_on_path(name, gSoPaths);
696  }
697  return fd;
698}
699
700static soinfo* load_library(const char* name) {
701    // Open the file.
702    int fd = open_library(name);
703    if (fd == -1) {
704        DL_ERR("library \"%s\" not found", name);
705        return NULL;
706    }
707
708    // Read the ELF header and load the segments.
709    ElfReader elf_reader(name, fd);
710    if (!elf_reader.Load()) {
711        return NULL;
712    }
713
714    const char* bname = strrchr(name, '/');
715    soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
716    if (si == NULL) {
717        return NULL;
718    }
719    si->base = elf_reader.load_start();
720    si->size = elf_reader.load_size();
721    si->load_bias = elf_reader.load_bias();
722    si->flags = 0;
723    si->entry = 0;
724    si->dynamic = NULL;
725    si->phnum = elf_reader.phdr_count();
726    si->phdr = elf_reader.loaded_phdr();
727    return si;
728}
729
730static soinfo *find_loaded_library(const char *name)
731{
732    soinfo *si;
733    const char *bname;
734
735    // TODO: don't use basename only for determining libraries
736    // http://code.google.com/p/android/issues/detail?id=6670
737
738    bname = strrchr(name, '/');
739    bname = bname ? bname + 1 : name;
740
741    for (si = solist; si != NULL; si = si->next) {
742        if (!strcmp(bname, si->name)) {
743            return si;
744        }
745    }
746    return NULL;
747}
748
749static soinfo* find_library_internal(const char* name) {
750  if (name == NULL) {
751    return somain;
752  }
753
754  soinfo* si = find_loaded_library(name);
755  if (si != NULL) {
756    if (si->flags & FLAG_LINKED) {
757      return si;
758    }
759    DL_ERR("OOPS: recursive link to \"%s\"", si->name);
760    return NULL;
761  }
762
763  TRACE("[ '%s' has not been loaded yet.  Locating...]", name);
764  si = load_library(name);
765  if (si == NULL) {
766    return NULL;
767  }
768
769  // At this point we know that whatever is loaded @ base is a valid ELF
770  // shared library whose segments are properly mapped in.
771  TRACE("[ init_library base=0x%08x sz=0x%08x name='%s' ]",
772        si->base, si->size, si->name);
773
774  if (!soinfo_link_image(si)) {
775    munmap(reinterpret_cast<void*>(si->base), si->size);
776    soinfo_free(si);
777    return NULL;
778  }
779
780  return si;
781}
782
783static soinfo* find_library(const char* name) {
784  soinfo* si = find_library_internal(name);
785  if (si != NULL) {
786    si->ref_count++;
787  }
788  return si;
789}
790
791static int soinfo_unload(soinfo* si) {
792  if (si->ref_count == 1) {
793    TRACE("unloading '%s'", si->name);
794    si->CallDestructors();
795
796    for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
797      if (d->d_tag == DT_NEEDED) {
798        const char* library_name = si->strtab + d->d_un.d_val;
799        TRACE("%s needs to unload %s", si->name, library_name);
800        soinfo_unload(find_loaded_library(library_name));
801      }
802    }
803
804    munmap(reinterpret_cast<void*>(si->base), si->size);
805    notify_gdb_of_unload(si);
806    soinfo_free(si);
807    si->ref_count = 0;
808  } else {
809    si->ref_count--;
810    TRACE("not unloading '%s', decrementing ref_count to %d", si->name, si->ref_count);
811  }
812  return 0;
813}
814
815void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
816  if (!get_AT_SECURE()) {
817    parse_LD_LIBRARY_PATH(ld_library_path);
818  }
819}
820
821soinfo* do_dlopen(const char* name, int flags) {
822  if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
823    DL_ERR("invalid flags to dlopen: %x", flags);
824    return NULL;
825  }
826  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
827  soinfo* si = find_library(name);
828  if (si != NULL) {
829    si->CallConstructors();
830  }
831  set_soinfo_pool_protection(PROT_READ);
832  return si;
833}
834
835int do_dlclose(soinfo* si) {
836  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
837  int result = soinfo_unload(si);
838  set_soinfo_pool_protection(PROT_READ);
839  return result;
840}
841
842/* TODO: don't use unsigned for addrs below. It works, but is not
843 * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
844 * long.
845 */
846static int soinfo_relocate(soinfo* si, Elf32_Rel* rel, unsigned count,
847                           soinfo* needed[])
848{
849    Elf32_Sym* symtab = si->symtab;
850    const char* strtab = si->strtab;
851    Elf32_Sym* s;
852    Elf32_Rel* start = rel;
853    soinfo* lsi;
854
855    for (size_t idx = 0; idx < count; ++idx, ++rel) {
856        unsigned type = ELF32_R_TYPE(rel->r_info);
857        unsigned sym = ELF32_R_SYM(rel->r_info);
858        Elf32_Addr reloc = static_cast<Elf32_Addr>(rel->r_offset + si->load_bias);
859        Elf32_Addr sym_addr = 0;
860        char* sym_name = NULL;
861
862        DEBUG("Processing '%s' relocation at index %d", si->name, idx);
863        if (type == 0) { // R_*_NONE
864            continue;
865        }
866        if (sym != 0) {
867            sym_name = (char *)(strtab + symtab[sym].st_name);
868            s = soinfo_do_lookup(si, sym_name, &lsi, needed);
869            if (s == NULL) {
870                /* We only allow an undefined symbol if this is a weak
871                   reference..   */
872                s = &symtab[sym];
873                if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
874                    DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
875                    return -1;
876                }
877
878                /* IHI0044C AAELF 4.5.1.1:
879
880                   Libraries are not searched to resolve weak references.
881                   It is not an error for a weak reference to remain
882                   unsatisfied.
883
884                   During linking, the value of an undefined weak reference is:
885                   - Zero if the relocation type is absolute
886                   - The address of the place if the relocation is pc-relative
887                   - The address of nominal base address if the relocation
888                     type is base-relative.
889                  */
890
891                switch (type) {
892#if defined(ANDROID_ARM_LINKER)
893                case R_ARM_JUMP_SLOT:
894                case R_ARM_GLOB_DAT:
895                case R_ARM_ABS32:
896                case R_ARM_RELATIVE:    /* Don't care. */
897#elif defined(ANDROID_X86_LINKER)
898                case R_386_JMP_SLOT:
899                case R_386_GLOB_DAT:
900                case R_386_32:
901                case R_386_RELATIVE:    /* Dont' care. */
902#endif /* ANDROID_*_LINKER */
903                    /* sym_addr was initialized to be zero above or relocation
904                       code below does not care about value of sym_addr.
905                       No need to do anything.  */
906                    break;
907
908#if defined(ANDROID_X86_LINKER)
909                case R_386_PC32:
910                    sym_addr = reloc;
911                    break;
912#endif /* ANDROID_X86_LINKER */
913
914#if defined(ANDROID_ARM_LINKER)
915                case R_ARM_COPY:
916                    /* Fall through.  Can't really copy if weak symbol is
917                       not found in run-time.  */
918#endif /* ANDROID_ARM_LINKER */
919                default:
920                    DL_ERR("unknown weak reloc type %d @ %p (%d)",
921                                 type, rel, (int) (rel - start));
922                    return -1;
923                }
924            } else {
925                /* We got a definition.  */
926#if 0
927                if ((base == 0) && (si->base != 0)) {
928                        /* linking from libraries to main image is bad */
929                    DL_ERR("cannot locate \"%s\"...",
930                           strtab + symtab[sym].st_name);
931                    return -1;
932                }
933#endif
934                sym_addr = static_cast<Elf32_Addr>(s->st_value + lsi->load_bias);
935            }
936            count_relocation(kRelocSymbol);
937        } else {
938            s = NULL;
939        }
940
941/* TODO: This is ugly. Split up the relocations by arch into
942 * different files.
943 */
944        switch(type){
945#if defined(ANDROID_ARM_LINKER)
946        case R_ARM_JUMP_SLOT:
947            count_relocation(kRelocAbsolute);
948            MARK(rel->r_offset);
949            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
950            *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
951            break;
952        case R_ARM_GLOB_DAT:
953            count_relocation(kRelocAbsolute);
954            MARK(rel->r_offset);
955            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
956            *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
957            break;
958        case R_ARM_ABS32:
959            count_relocation(kRelocAbsolute);
960            MARK(rel->r_offset);
961            TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
962            *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr;
963            break;
964        case R_ARM_REL32:
965            count_relocation(kRelocRelative);
966            MARK(rel->r_offset);
967            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
968                       reloc, sym_addr, rel->r_offset, sym_name);
969            *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr - rel->r_offset;
970            break;
971#elif defined(ANDROID_X86_LINKER)
972        case R_386_JMP_SLOT:
973            count_relocation(kRelocAbsolute);
974            MARK(rel->r_offset);
975            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
976            *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
977            break;
978        case R_386_GLOB_DAT:
979            count_relocation(kRelocAbsolute);
980            MARK(rel->r_offset);
981            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
982            *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
983            break;
984#elif defined(ANDROID_MIPS_LINKER)
985    case R_MIPS_REL32:
986            count_relocation(kRelocAbsolute);
987            MARK(rel->r_offset);
988            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s",
989                       reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
990            if (s) {
991                *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr;
992            } else {
993                *reinterpret_cast<Elf32_Addr*>(reloc) += si->base;
994            }
995            break;
996#endif /* ANDROID_*_LINKER */
997
998#if defined(ANDROID_ARM_LINKER)
999        case R_ARM_RELATIVE:
1000#elif defined(ANDROID_X86_LINKER)
1001        case R_386_RELATIVE:
1002#endif /* ANDROID_*_LINKER */
1003            count_relocation(kRelocRelative);
1004            MARK(rel->r_offset);
1005            if (sym) {
1006                DL_ERR("odd RELATIVE form...");
1007                return -1;
1008            }
1009            TRACE_TYPE(RELO, "RELO RELATIVE %08x <- +%08x", reloc, si->base);
1010            *reinterpret_cast<Elf32_Addr*>(reloc) += si->base;
1011            break;
1012
1013#if defined(ANDROID_X86_LINKER)
1014        case R_386_32:
1015            count_relocation(kRelocRelative);
1016            MARK(rel->r_offset);
1017
1018            TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1019            *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr;
1020            break;
1021
1022        case R_386_PC32:
1023            count_relocation(kRelocRelative);
1024            MARK(rel->r_offset);
1025            TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
1026                       reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1027            *reinterpret_cast<Elf32_Addr*>(reloc) += (sym_addr - reloc);
1028            break;
1029#endif /* ANDROID_X86_LINKER */
1030
1031#ifdef ANDROID_ARM_LINKER
1032        case R_ARM_COPY:
1033            if ((si->flags & FLAG_EXE) == 0) {
1034                /*
1035                 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1036                 *
1037                 * Section 4.7.1.10 "Dynamic relocations"
1038                 * R_ARM_COPY may only appear in executable objects where e_type is
1039                 * set to ET_EXEC.
1040                 *
1041                 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1042                 * We should explicitly disallow ET_DYN executables from having
1043                 * R_ARM_COPY relocations.
1044                 */
1045                DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1046                return -1;
1047            }
1048            count_relocation(kRelocCopy);
1049            MARK(rel->r_offset);
1050            TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s", reloc, s->st_size, sym_addr, sym_name);
1051            if (reloc == sym_addr) {
1052                Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
1053
1054                if (src == NULL) {
1055                    DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
1056                    return -1;
1057                }
1058                if (lsi->has_DT_SYMBOLIC) {
1059                    DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
1060                           "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1061                    return -1;
1062                }
1063                if (s->st_size < src->st_size) {
1064                    DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
1065                           si->name, s->st_size, src->st_size);
1066                    return -1;
1067                }
1068                memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
1069            } else {
1070                DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
1071                return -1;
1072            }
1073            break;
1074#endif /* ANDROID_ARM_LINKER */
1075
1076        default:
1077            DL_ERR("unknown reloc type %d @ %p (%d)",
1078                   type, rel, (int) (rel - start));
1079            return -1;
1080        }
1081    }
1082    return 0;
1083}
1084
1085#ifdef ANDROID_MIPS_LINKER
1086static bool mips_relocate_got(soinfo* si, soinfo* needed[]) {
1087    unsigned* got = si->plt_got;
1088    if (got == NULL) {
1089        return true;
1090    }
1091    unsigned local_gotno = si->mips_local_gotno;
1092    unsigned gotsym = si->mips_gotsym;
1093    unsigned symtabno = si->mips_symtabno;
1094    Elf32_Sym* symtab = si->symtab;
1095
1096    /*
1097     * got[0] is address of lazy resolver function
1098     * got[1] may be used for a GNU extension
1099     * set it to a recognizable address in case someone calls it
1100     * (should be _rtld_bind_start)
1101     * FIXME: maybe this should be in a separate routine
1102     */
1103
1104    if ((si->flags & FLAG_LINKER) == 0) {
1105        size_t g = 0;
1106        got[g++] = 0xdeadbeef;
1107        if (got[g] & 0x80000000) {
1108            got[g++] = 0xdeadfeed;
1109        }
1110        /*
1111         * Relocate the local GOT entries need to be relocated
1112         */
1113        for (; g < local_gotno; g++) {
1114            got[g] += si->load_bias;
1115        }
1116    }
1117
1118    /* Now for the global GOT entries */
1119    Elf32_Sym* sym = symtab + gotsym;
1120    got = si->plt_got + local_gotno;
1121    for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
1122        const char* sym_name;
1123        Elf32_Sym* s;
1124        soinfo* lsi;
1125
1126        /* This is an undefined reference... try to locate it */
1127        sym_name = si->strtab + sym->st_name;
1128        s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1129        if (s == NULL) {
1130            /* We only allow an undefined symbol if this is a weak
1131               reference..   */
1132            s = &symtab[g];
1133            if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
1134                DL_ERR("cannot locate \"%s\"...", sym_name);
1135                return false;
1136            }
1137            *got = 0;
1138        }
1139        else {
1140            /* FIXME: is this sufficient?
1141             * For reference see NetBSD link loader
1142             * http://cvsweb.netbsd.org/bsdweb.cgi/src/libexec/ld.elf_so/arch/mips/mips_reloc.c?rev=1.53&content-type=text/x-cvsweb-markup
1143             */
1144             *got = lsi->load_bias + s->st_value;
1145        }
1146    }
1147    return true;
1148}
1149#endif
1150
1151void soinfo::CallArray(const char* array_name UNUSED, linker_function_t* functions, size_t count, bool reverse) {
1152  if (functions == NULL) {
1153    return;
1154  }
1155
1156  TRACE("[ Calling %s (size %d) @ %p for '%s' ]", array_name, count, functions, name);
1157
1158  int begin = reverse ? (count - 1) : 0;
1159  int end = reverse ? -1 : count;
1160  int step = reverse ? -1 : 1;
1161
1162  for (int i = begin; i != end; i += step) {
1163    TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1164    CallFunction("function", functions[i]);
1165  }
1166
1167  TRACE("[ Done calling %s for '%s' ]", array_name, name);
1168}
1169
1170void soinfo::CallFunction(const char* function_name UNUSED, linker_function_t function) {
1171  if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
1172    return;
1173  }
1174
1175  TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
1176  function();
1177  TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
1178
1179  // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1180  // are still writable. This happens with our debug malloc (see http://b/7941716).
1181  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
1182}
1183
1184void soinfo::CallPreInitConstructors() {
1185  // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
1186  // but ignored in a shared library.
1187  CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1188}
1189
1190void soinfo::CallConstructors() {
1191  if (constructors_called) {
1192    return;
1193  }
1194
1195  // We set constructors_called before actually calling the constructors, otherwise it doesn't
1196  // protect against recursive constructor calls. One simple example of constructor recursion
1197  // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1198  // 1. The program depends on libc, so libc's constructor is called here.
1199  // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1200  // 3. dlopen() calls the constructors on the newly created
1201  //    soinfo for libc_malloc_debug_leak.so.
1202  // 4. The debug .so depends on libc, so CallConstructors is
1203  //    called again with the libc soinfo. If it doesn't trigger the early-
1204  //    out above, the libc constructor will be called again (recursively!).
1205  constructors_called = true;
1206
1207  if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) {
1208    // The GNU dynamic linker silently ignores these, but we warn the developer.
1209    PRINT("\"%s\": ignoring %d-entry DT_PREINIT_ARRAY in shared library!",
1210          name, preinit_array_count);
1211  }
1212
1213  if (dynamic != NULL) {
1214    for (Elf32_Dyn* d = dynamic; d->d_tag != DT_NULL; ++d) {
1215      if (d->d_tag == DT_NEEDED) {
1216        const char* library_name = strtab + d->d_un.d_val;
1217        TRACE("\"%s\": calling constructors in DT_NEEDED \"%s\"", name, library_name);
1218        find_loaded_library(library_name)->CallConstructors();
1219      }
1220    }
1221  }
1222
1223  TRACE("\"%s\": calling constructors", name);
1224
1225  // DT_INIT should be called before DT_INIT_ARRAY if both are present.
1226  CallFunction("DT_INIT", init_func);
1227  CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
1228}
1229
1230void soinfo::CallDestructors() {
1231  TRACE("\"%s\": calling destructors", name);
1232
1233  // DT_FINI_ARRAY must be parsed in reverse order.
1234  CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
1235
1236  // DT_FINI should be called after DT_FINI_ARRAY if both are present.
1237  CallFunction("DT_FINI", fini_func);
1238}
1239
1240/* Force any of the closed stdin, stdout and stderr to be associated with
1241   /dev/null. */
1242static int nullify_closed_stdio() {
1243    int dev_null, i, status;
1244    int return_value = 0;
1245
1246    dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
1247    if (dev_null < 0) {
1248        DL_ERR("cannot open /dev/null: %s", strerror(errno));
1249        return -1;
1250    }
1251    TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
1252
1253    /* If any of the stdio file descriptors is valid and not associated
1254       with /dev/null, dup /dev/null to it.  */
1255    for (i = 0; i < 3; i++) {
1256        /* If it is /dev/null already, we are done. */
1257        if (i == dev_null) {
1258            continue;
1259        }
1260
1261        TRACE("[ Nullifying stdio file descriptor %d]", i);
1262        status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
1263
1264        /* If file is opened, we are good. */
1265        if (status != -1) {
1266            continue;
1267        }
1268
1269        /* The only error we allow is that the file descriptor does not
1270           exist, in which case we dup /dev/null to it. */
1271        if (errno != EBADF) {
1272            DL_ERR("fcntl failed: %s", strerror(errno));
1273            return_value = -1;
1274            continue;
1275        }
1276
1277        /* Try dupping /dev/null to this stdio file descriptor and
1278           repeat if there is a signal.  Note that any errors in closing
1279           the stdio descriptor are lost.  */
1280        status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
1281        if (status < 0) {
1282            DL_ERR("dup2 failed: %s", strerror(errno));
1283            return_value = -1;
1284            continue;
1285        }
1286    }
1287
1288    /* If /dev/null is not one of the stdio file descriptors, close it. */
1289    if (dev_null > 2) {
1290        TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
1291        status = TEMP_FAILURE_RETRY(close(dev_null));
1292        if (status == -1) {
1293            DL_ERR("close failed: %s", strerror(errno));
1294            return_value = -1;
1295        }
1296    }
1297
1298    return return_value;
1299}
1300
1301static bool soinfo_link_image(soinfo* si) {
1302    /* "base" might wrap around UINT32_MAX. */
1303    Elf32_Addr base = si->load_bias;
1304    const Elf32_Phdr *phdr = si->phdr;
1305    int phnum = si->phnum;
1306    bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
1307
1308    /* We can't debug anything until the linker is relocated */
1309    if (!relocating_linker) {
1310        INFO("[ linking %s ]", si->name);
1311        DEBUG("si->base = 0x%08x si->flags = 0x%08x", si->base, si->flags);
1312    }
1313
1314    /* Extract dynamic section */
1315    size_t dynamic_count;
1316    Elf32_Word dynamic_flags;
1317    phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
1318                                   &dynamic_count, &dynamic_flags);
1319    if (si->dynamic == NULL) {
1320        if (!relocating_linker) {
1321            DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
1322        }
1323        return false;
1324    } else {
1325        if (!relocating_linker) {
1326            DEBUG("dynamic = %p", si->dynamic);
1327        }
1328    }
1329
1330#ifdef ANDROID_ARM_LINKER
1331    (void) phdr_table_get_arm_exidx(phdr, phnum, base,
1332                                    &si->ARM_exidx, &si->ARM_exidx_count);
1333#endif
1334
1335    // Extract useful information from dynamic section.
1336    uint32_t needed_count = 0;
1337    for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1338        DEBUG("d = %p, d[0](tag) = 0x%08x d[1](val) = 0x%08x", d, d->d_tag, d->d_un.d_val);
1339        switch(d->d_tag){
1340        case DT_HASH:
1341            si->nbucket = ((unsigned *) (base + d->d_un.d_ptr))[0];
1342            si->nchain = ((unsigned *) (base + d->d_un.d_ptr))[1];
1343            si->bucket = (unsigned *) (base + d->d_un.d_ptr + 8);
1344            si->chain = (unsigned *) (base + d->d_un.d_ptr + 8 + si->nbucket * 4);
1345            break;
1346        case DT_STRTAB:
1347            si->strtab = (const char *) (base + d->d_un.d_ptr);
1348            break;
1349        case DT_SYMTAB:
1350            si->symtab = (Elf32_Sym *) (base + d->d_un.d_ptr);
1351            break;
1352        case DT_PLTREL:
1353            if (d->d_un.d_val != DT_REL) {
1354                DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1355                return false;
1356            }
1357            break;
1358        case DT_JMPREL:
1359            si->plt_rel = (Elf32_Rel*) (base + d->d_un.d_ptr);
1360            break;
1361        case DT_PLTRELSZ:
1362            si->plt_rel_count = d->d_un.d_val / sizeof(Elf32_Rel);
1363            break;
1364        case DT_REL:
1365            si->rel = (Elf32_Rel*) (base + d->d_un.d_ptr);
1366            break;
1367        case DT_RELSZ:
1368            si->rel_count = d->d_un.d_val / sizeof(Elf32_Rel);
1369            break;
1370        case DT_PLTGOT:
1371            /* Save this in case we decide to do lazy binding. We don't yet. */
1372            si->plt_got = (unsigned *)(base + d->d_un.d_ptr);
1373            break;
1374        case DT_DEBUG:
1375            // Set the DT_DEBUG entry to the address of _r_debug for GDB
1376            // if the dynamic table is writable
1377            if ((dynamic_flags & PF_W) != 0) {
1378                d->d_un.d_val = (int) &_r_debug;
1379            }
1380            break;
1381         case DT_RELA:
1382            DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1383            return false;
1384        case DT_INIT:
1385            si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
1386            DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func);
1387            break;
1388        case DT_FINI:
1389            si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
1390            DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func);
1391            break;
1392        case DT_INIT_ARRAY:
1393            si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
1394            DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array);
1395            break;
1396        case DT_INIT_ARRAYSZ:
1397            si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
1398            break;
1399        case DT_FINI_ARRAY:
1400            si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
1401            DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array);
1402            break;
1403        case DT_FINI_ARRAYSZ:
1404            si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
1405            break;
1406        case DT_PREINIT_ARRAY:
1407            si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
1408            DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array);
1409            break;
1410        case DT_PREINIT_ARRAYSZ:
1411            si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
1412            break;
1413        case DT_TEXTREL:
1414            si->has_text_relocations = true;
1415            break;
1416        case DT_SYMBOLIC:
1417            si->has_DT_SYMBOLIC = true;
1418            break;
1419        case DT_NEEDED:
1420            ++needed_count;
1421            break;
1422#if defined DT_FLAGS
1423        // TODO: why is DT_FLAGS not defined?
1424        case DT_FLAGS:
1425            if (d->d_un.d_val & DF_TEXTREL) {
1426                si->has_text_relocations = true;
1427            }
1428            if (d->d_un.d_val & DF_SYMBOLIC) {
1429                si->has_DT_SYMBOLIC = true;
1430            }
1431            break;
1432#endif
1433#if defined(ANDROID_MIPS_LINKER)
1434        case DT_STRSZ:
1435        case DT_SYMENT:
1436        case DT_RELENT:
1437             break;
1438        case DT_MIPS_RLD_MAP:
1439            // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
1440            {
1441              r_debug** dp = (r_debug**) d->d_un.d_ptr;
1442              *dp = &_r_debug;
1443            }
1444            break;
1445        case DT_MIPS_RLD_VERSION:
1446        case DT_MIPS_FLAGS:
1447        case DT_MIPS_BASE_ADDRESS:
1448        case DT_MIPS_UNREFEXTNO:
1449            break;
1450
1451        case DT_MIPS_SYMTABNO:
1452            si->mips_symtabno = d->d_un.d_val;
1453            break;
1454
1455        case DT_MIPS_LOCAL_GOTNO:
1456            si->mips_local_gotno = d->d_un.d_val;
1457            break;
1458
1459        case DT_MIPS_GOTSYM:
1460            si->mips_gotsym = d->d_un.d_val;
1461            break;
1462
1463        default:
1464            DEBUG("Unused DT entry: type 0x%08x arg 0x%08x", d->d_tag, d->d_un.d_val);
1465            break;
1466#endif
1467        }
1468    }
1469
1470    DEBUG("si->base = 0x%08x, si->strtab = %p, si->symtab = %p",
1471          si->base, si->strtab, si->symtab);
1472
1473    // Sanity checks.
1474    if (relocating_linker && needed_count != 0) {
1475        DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1476        return false;
1477    }
1478    if (si->nbucket == 0) {
1479        DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1480        return false;
1481    }
1482    if (si->strtab == 0) {
1483        DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1484        return false;
1485    }
1486    if (si->symtab == 0) {
1487        DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1488        return false;
1489    }
1490
1491    // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
1492    if (si->flags & FLAG_EXE) {
1493        memset(gLdPreloads, 0, sizeof(gLdPreloads));
1494        size_t preload_count = 0;
1495        for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1496            soinfo* lsi = find_library(gLdPreloadNames[i]);
1497            if (lsi != NULL) {
1498                gLdPreloads[preload_count++] = lsi;
1499            } else {
1500                // As with glibc, failure to load an LD_PRELOAD library is just a warning.
1501                DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
1502                        gLdPreloadNames[i], si->name, linker_get_error_buffer());
1503            }
1504        }
1505    }
1506
1507    soinfo** needed = (soinfo**) alloca((1 + needed_count) * sizeof(soinfo*));
1508    soinfo** pneeded = needed;
1509
1510    for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1511        if (d->d_tag == DT_NEEDED) {
1512            const char* library_name = si->strtab + d->d_un.d_val;
1513            DEBUG("%s needs %s", si->name, library_name);
1514            soinfo* lsi = find_library(library_name);
1515            if (lsi == NULL) {
1516                strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
1517                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1518                       library_name, si->name, tmp_err_buf);
1519                return false;
1520            }
1521            *pneeded++ = lsi;
1522        }
1523    }
1524    *pneeded = NULL;
1525
1526    if (si->has_text_relocations) {
1527        /* Unprotect the segments, i.e. make them writable, to allow
1528         * text relocations to work properly. We will later call
1529         * phdr_table_protect_segments() after all of them are applied
1530         * and all constructors are run.
1531         */
1532        DL_WARN("%s has text relocations. This is wasting memory and is "
1533                "a security risk. Please fix.", si->name);
1534        if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1535            DL_ERR("can't unprotect loadable segments for \"%s\": %s",
1536                   si->name, strerror(errno));
1537            return false;
1538        }
1539    }
1540
1541    if (si->plt_rel != NULL) {
1542        DEBUG("[ relocating %s plt ]", si->name );
1543        if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1544            return false;
1545        }
1546    }
1547    if (si->rel != NULL) {
1548        DEBUG("[ relocating %s ]", si->name );
1549        if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1550            return false;
1551        }
1552    }
1553
1554#ifdef ANDROID_MIPS_LINKER
1555    if (!mips_relocate_got(si, needed)) {
1556        return false;
1557    }
1558#endif
1559
1560    si->flags |= FLAG_LINKED;
1561    DEBUG("[ finished linking %s ]", si->name);
1562
1563    if (si->has_text_relocations) {
1564        /* All relocations are done, we can protect our segments back to
1565         * read-only. */
1566        if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
1567            DL_ERR("can't protect segments for \"%s\": %s",
1568                   si->name, strerror(errno));
1569            return false;
1570        }
1571    }
1572
1573    /* We can also turn on GNU RELRO protection */
1574    if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
1575        DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
1576               si->name, strerror(errno));
1577        return false;
1578    }
1579
1580    notify_gdb_of_load(si);
1581    return true;
1582}
1583
1584/*
1585 * This function add vdso to internal dso list.
1586 * It helps to stack unwinding through signal handlers.
1587 * Also, it makes bionic more like glibc.
1588 */
1589static void add_vdso(KernelArgumentBlock& args UNUSED) {
1590#ifdef AT_SYSINFO_EHDR
1591    Elf32_Ehdr* ehdr_vdso = reinterpret_cast<Elf32_Ehdr*>(args.getauxval(AT_SYSINFO_EHDR));
1592
1593    soinfo* si = soinfo_alloc("[vdso]");
1594    si->phdr = reinterpret_cast<Elf32_Phdr*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
1595    si->phnum = ehdr_vdso->e_phnum;
1596    si->link_map.l_name = si->name;
1597    for (size_t i = 0; i < si->phnum; ++i) {
1598        if (si->phdr[i].p_type == PT_LOAD) {
1599            si->link_map.l_addr = reinterpret_cast<Elf32_Addr>(ehdr_vdso) - si->phdr[i].p_vaddr;
1600            break;
1601        }
1602    }
1603#endif
1604}
1605
1606/*
1607 * This code is called after the linker has linked itself and
1608 * fixed it's own GOT. It is safe to make references to externs
1609 * and other non-local data at this point.
1610 */
1611static Elf32_Addr __linker_init_post_relocation(KernelArgumentBlock& args, Elf32_Addr linker_base) {
1612    /* NOTE: we store the args pointer on a special location
1613     *       of the temporary TLS area in order to pass it to
1614     *       the C Library's runtime initializer.
1615     *
1616     *       The initializer must clear the slot and reset the TLS
1617     *       to point to a different location to ensure that no other
1618     *       shared library constructor can access it.
1619     */
1620  __libc_init_tls(args);
1621
1622#if TIMING
1623    struct timeval t0, t1;
1624    gettimeofday(&t0, 0);
1625#endif
1626
1627    // Initialize environment functions, and get to the ELF aux vectors table.
1628    linker_env_init(args);
1629
1630    // If this is a setuid/setgid program, close the security hole described in
1631    // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
1632    if (get_AT_SECURE()) {
1633        nullify_closed_stdio();
1634    }
1635
1636    debuggerd_init();
1637
1638    // Get a few environment variables.
1639    const char* LD_DEBUG = linker_env_get("LD_DEBUG");
1640    if (LD_DEBUG != NULL) {
1641      gLdDebugVerbosity = atoi(LD_DEBUG);
1642    }
1643
1644    // Normally, these are cleaned by linker_env_init, but the test
1645    // doesn't cost us anything.
1646    const char* ldpath_env = NULL;
1647    const char* ldpreload_env = NULL;
1648    if (!get_AT_SECURE()) {
1649      ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1650      ldpreload_env = linker_env_get("LD_PRELOAD");
1651    }
1652
1653    INFO("[ android linker & debugger ]");
1654
1655    soinfo* si = soinfo_alloc(args.argv[0]);
1656    if (si == NULL) {
1657        exit(EXIT_FAILURE);
1658    }
1659
1660    /* bootstrap the link map, the main exe always needs to be first */
1661    si->flags |= FLAG_EXE;
1662    link_map_t* map = &(si->link_map);
1663
1664    map->l_addr = 0;
1665    map->l_name = args.argv[0];
1666    map->l_prev = NULL;
1667    map->l_next = NULL;
1668
1669    _r_debug.r_map = map;
1670    r_debug_tail = map;
1671
1672    /* gdb expects the linker to be in the debug shared object list.
1673     * Without this, gdb has trouble locating the linker's ".text"
1674     * and ".plt" sections. Gdb could also potentially use this to
1675     * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1676     * Don't use soinfo_alloc(), because the linker shouldn't
1677     * be on the soinfo list.
1678     */
1679    {
1680        static soinfo linker_soinfo;
1681        strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
1682        linker_soinfo.flags = 0;
1683        linker_soinfo.base = linker_base;
1684
1685        /*
1686         * Set the dynamic field in the link map otherwise gdb will complain with
1687         * the following:
1688         *   warning: .dynamic section for "/system/bin/linker" is not at the
1689         *   expected address (wrong library or version mismatch?)
1690         */
1691        Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
1692        Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff);
1693        phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1694                                       &linker_soinfo.dynamic, NULL, NULL);
1695        insert_soinfo_into_debug_map(&linker_soinfo);
1696    }
1697
1698    // Extract information passed from the kernel.
1699    si->phdr = reinterpret_cast<Elf32_Phdr*>(args.getauxval(AT_PHDR));
1700    si->phnum = args.getauxval(AT_PHNUM);
1701    si->entry = args.getauxval(AT_ENTRY);
1702
1703    /* Compute the value of si->base. We can't rely on the fact that
1704     * the first entry is the PHDR because this will not be true
1705     * for certain executables (e.g. some in the NDK unit test suite)
1706     */
1707    si->base = 0;
1708    si->size = phdr_table_get_load_size(si->phdr, si->phnum);
1709    si->load_bias = 0;
1710    for (size_t i = 0; i < si->phnum; ++i) {
1711      if (si->phdr[i].p_type == PT_PHDR) {
1712        si->load_bias = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_vaddr;
1713        si->base = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_offset;
1714        break;
1715      }
1716    }
1717    si->dynamic = NULL;
1718    si->ref_count = 1;
1719
1720    // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
1721    parse_LD_LIBRARY_PATH(ldpath_env);
1722    parse_LD_PRELOAD(ldpreload_env);
1723
1724    somain = si;
1725
1726    if (!soinfo_link_image(si)) {
1727        __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
1728        exit(EXIT_FAILURE);
1729    }
1730
1731    add_vdso(args);
1732
1733    si->CallPreInitConstructors();
1734
1735    for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
1736        gLdPreloads[i]->CallConstructors();
1737    }
1738
1739    /* After the link_image, the si->load_bias is initialized.
1740     * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1741     * We need to update this value for so exe here. So Unwind_Backtrace
1742     * for some arch like x86 could work correctly within so exe.
1743     */
1744    map->l_addr = si->load_bias;
1745    si->CallConstructors();
1746
1747#if TIMING
1748    gettimeofday(&t1,NULL);
1749    PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
1750               (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
1751               (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
1752               ));
1753#endif
1754#if STATS
1755    PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
1756           linker_stats.count[kRelocAbsolute],
1757           linker_stats.count[kRelocRelative],
1758           linker_stats.count[kRelocCopy],
1759           linker_stats.count[kRelocSymbol]);
1760#endif
1761#if COUNT_PAGES
1762    {
1763        unsigned n;
1764        unsigned i;
1765        unsigned count = 0;
1766        for (n = 0; n < 4096; n++) {
1767            if (bitmask[n]) {
1768                unsigned x = bitmask[n];
1769                for (i = 0; i < 8; i++) {
1770                    if (x & 1) {
1771                        count++;
1772                    }
1773                    x >>= 1;
1774                }
1775            }
1776        }
1777        PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
1778    }
1779#endif
1780
1781#if TIMING || STATS || COUNT_PAGES
1782    fflush(stdout);
1783#endif
1784
1785    TRACE("[ Ready to execute '%s' @ 0x%08x ]", si->name, si->entry);
1786    return si->entry;
1787}
1788
1789/* Compute the load-bias of an existing executable. This shall only
1790 * be used to compute the load bias of an executable or shared library
1791 * that was loaded by the kernel itself.
1792 *
1793 * Input:
1794 *    elf    -> address of ELF header, assumed to be at the start of the file.
1795 * Return:
1796 *    load bias, i.e. add the value of any p_vaddr in the file to get
1797 *    the corresponding address in memory.
1798 */
1799static Elf32_Addr get_elf_exec_load_bias(const Elf32_Ehdr* elf) {
1800  Elf32_Addr        offset     = elf->e_phoff;
1801  const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
1802  const Elf32_Phdr* phdr_end   = phdr_table + elf->e_phnum;
1803
1804  for (const Elf32_Phdr* phdr = phdr_table; phdr < phdr_end; phdr++) {
1805    if (phdr->p_type == PT_LOAD) {
1806      return reinterpret_cast<Elf32_Addr>(elf) + phdr->p_offset - phdr->p_vaddr;
1807    }
1808  }
1809  return 0;
1810}
1811
1812/*
1813 * This is the entry point for the linker, called from begin.S. This
1814 * method is responsible for fixing the linker's own relocations, and
1815 * then calling __linker_init_post_relocation().
1816 *
1817 * Because this method is called before the linker has fixed it's own
1818 * relocations, any attempt to reference an extern variable, extern
1819 * function, or other GOT reference will generate a segfault.
1820 */
1821extern "C" Elf32_Addr __linker_init(void* raw_args) {
1822  KernelArgumentBlock args(raw_args);
1823
1824  Elf32_Addr linker_addr = args.getauxval(AT_BASE);
1825
1826  Elf32_Ehdr* elf_hdr = (Elf32_Ehdr*) linker_addr;
1827  Elf32_Phdr* phdr = (Elf32_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff);
1828
1829  soinfo linker_so;
1830  memset(&linker_so, 0, sizeof(soinfo));
1831
1832  linker_so.base = linker_addr;
1833  linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
1834  linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
1835  linker_so.dynamic = NULL;
1836  linker_so.phdr = phdr;
1837  linker_so.phnum = elf_hdr->e_phnum;
1838  linker_so.flags |= FLAG_LINKER;
1839
1840  if (!soinfo_link_image(&linker_so)) {
1841    // It would be nice to print an error message, but if the linker
1842    // can't link itself, there's no guarantee that we'll be able to
1843    // call write() (because it involves a GOT reference).
1844    //
1845    // This situation should never occur unless the linker itself
1846    // is corrupt.
1847    exit(EXIT_FAILURE);
1848  }
1849
1850  // We have successfully fixed our own relocations. It's safe to run
1851  // the main part of the linker now.
1852  args.abort_message_ptr = &gAbortMessage;
1853  Elf32_Addr start_address = __linker_init_post_relocation(args, linker_addr);
1854
1855  set_soinfo_pool_protection(PROT_READ);
1856
1857  // Return the address that the calling assembly stub should jump to.
1858  return start_address;
1859}
1860