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