linker.cpp revision 5ae44f302b7d1d19f25c4c6f125e32dc369961d9
11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
2943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan * Copyright (C) 2008, 2009 The Android Open Source Project
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved.
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  * Redistributions of source code must retain the above copyright
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  * Redistributions in binary form must reproduce the above copyright
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    the documentation and/or other materials provided with the
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    distribution.
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE.
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
294688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <dlfcn.h>
304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <errno.h>
314688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <fcntl.h>
321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <linux/auxvec.h>
334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <pthread.h>
344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <stdbool.h>
351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdio.h>
361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdlib.h>
371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <string.h>
381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <sys/atomics.h>
394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <sys/mman.h>
404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <sys/stat.h>
414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <unistd.h>
421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Private C library headers.
444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <private/bionic_tls.h>
454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <private/logd.h>
463b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes#include <private/ScopedPthreadMutexLocker.h>
471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker.h"
491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker_debug.h"
50be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner#include "linker_environ.h"
515c734644eebf8d01be1e86cbe20a111a5c5a2738David 'Digit' Turner#include "linker_format.h"
5223363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner#include "linker_phdr.h"
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5472f9a5c374bf559e9c69a62c1d95304f913ef6b2Kenny Root#define SO_MAX 128
551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
56bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley/* Assume average path length of 64 and max 8 paths */
57bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley#define LDPATH_BUFSIZE 512
58bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley#define LDPATH_MAX 8
59bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
604fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer#define LDPRELOAD_BUFSIZE 512
614fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer#define LDPRELOAD_MAX 8
624fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Do NOT use malloc() and friends or pthread_*() code here.
661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Don't use printf() either; it's caused mysterious memory
671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * corruption in the past.
681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The linker runs before we bring up libc and it's easiest
691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to make sure it does not depend on any complex libc features
701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * open issues / todo:
721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - are we doing everything we should for ARM_COPY relocations?
741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - cleaner error reporting
751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - after linking, set as much stuff as possible to READONLY
761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   and NOEXEC
771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - linker hardcodes PAGE_SIZE and PAGE_MASK because the kernel
781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   headers provide versions that are negative...
791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - allocate space for soinfo structs dynamically instead of
804688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes *   having a hard limit (SO_MAX)
814688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes */
821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
845135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevichstatic int soinfo_link_image(soinfo *si);
851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int socount = 0;
871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic soinfo sopool[SO_MAX];
881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic soinfo *freelist = NULL;
891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic soinfo *solist = &libdl_info;
901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic soinfo *sonext = &libdl_info;
914a9afcb10151b083cd2d75253385615f459172edIliyan Malchevstatic soinfo *somain; /* main process, always the one after libdl_info */
921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
93bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartleystatic char ldpaths_buf[LDPATH_BUFSIZE];
94bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartleystatic const char *ldpaths[LDPATH_MAX + 1];
95bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
964fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischerstatic char ldpreloads_buf[LDPRELOAD_BUFSIZE];
974fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischerstatic const char *ldpreload_names[LDPRELOAD_MAX + 1];
984fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
994fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischerstatic soinfo *preloads[LDPRELOAD_MAX + 1];
1004fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1018c4f3ce8d084e3949d77ad8743593b5c83c3d41cNick Kralevich#if LINKER_DEBUG
1021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectint debug_verbosity;
1038c4f3ce8d084e3949d77ad8743593b5c83c3d41cNick Kralevich#endif
1048c4f3ce8d084e3949d77ad8743593b5c83c3d41cNick Kralevich
1051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int pid;
1061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
107bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesenum RelocationKind {
108bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocAbsolute = 0,
109bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocRelative,
110bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocCopy,
111bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocSymbol,
112bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocMax
113bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
114be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
1151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
116bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstruct linker_stats_t {
117bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    int count[kRelocMax];
118bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
119bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
120bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic linker_stats_t linker_stats;
121bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
122bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind kind) {
123bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    ++linker_stats.count[kind];
124bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
125bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
126bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind) {
127bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
1281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
131bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic unsigned bitmask[4096];
132bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(offset) \
133bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    do { \
134bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
135bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    } while(0)
136bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
137bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(x) do {} while (0)
1381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// You shouldn't try to call memory-allocating functions in the dynamic linker.
1414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Guard against the most obvious ones.
1424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#define DISALLOW_ALLOCATION(return_type, name, ...)                             \
1434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    return_type name __VA_ARGS__                                                \
1444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    {                                                                           \
1454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
1464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes         __libc_android_log_write(ANDROID_LOG_FATAL, "linker", msg);            \
1474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        write(2, msg, sizeof(msg));                                             \
1484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        abort();                                                                \
1494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
1504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#define UNUSED __attribute__((unused))
1514688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
1524688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void, free, (void* u UNUSED));
1534688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
1544688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
1552e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1560353195f344666256dba474a15c9ba22cf0cccc9Dima Zavinstatic char tmp_err_buf[768];
1572e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavinstatic char __linker_dl_err_buf[768];
158e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes#define DL_ERR(fmt, x...) \
159e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes    do { \
1603b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes        format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), fmt, ##x); \
161e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes        ERROR(fmt "\n", ##x); \
1622e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    } while(0)
1632e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1645419b9474753d25dff947c7740532f86d130c0beElliott Hughesconst char* linker_get_error() {
1655419b9474753d25dff947c7740532f86d130c0beElliott Hughes  return &__linker_dl_err_buf[0];
1662e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin}
1672e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
1691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This function is an empty stub where GDB locates a breakpoint to get notified
1701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about linker activity.
1711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1725419b9474753d25dff947c7740532f86d130c0beElliott Hughesextern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
1731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
174bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
1751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                                  RT_CONSISTENT, 0};
176bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic link_map* r_debug_tail = 0;
1771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1783b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughesstatic pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
1791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
180bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void insert_soinfo_into_debug_map(soinfo * info) {
181bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    // Copy the necessary fields into the debug structure.
182bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    link_map* map = &(info->linkmap);
1831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = info->base;
1841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_name = (char*) info->name;
1855cf640c926e7d7b79934c97226433a3dc448e1ebThinker K.F Li    map->l_ld = (uintptr_t)info->dynamic;
1861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Stick the new library at the end of the list.
1881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * gdb tends to care more about libc than it does
1891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * about leaf libraries, and ordering it this way
1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * reduces the back-and-forth over the wire.
1911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (r_debug_tail) {
1931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        r_debug_tail->l_next = map;
1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = r_debug_tail;
1951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
1961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    } else {
1971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        _r_debug.r_map = map;
1981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = 0;
1991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
204bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void remove_soinfo_from_debug_map(soinfo* info) {
205bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    link_map* map = &(info->linkmap);
2065e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
207bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (r_debug_tail == map) {
2085e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        r_debug_tail = map->l_prev;
209bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2105e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
211bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_prev) {
212bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_prev->l_next = map->l_next;
213bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
214bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_next) {
215bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_next->l_prev = map->l_prev;
216bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2175e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2185e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
219bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_load(soinfo* info) {
2201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (info->flags & FLAG_EXE) {
2211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        // GDB already knows about the main executable
2221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
2231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2253b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes    ScopedPthreadMutexLocker locker(&gDebugMutex);
2261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_ADD;
2281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(info);
2311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_CONSISTENT;
2331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2345e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2355e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
236bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_unload(soinfo* info) {
2375e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    if (info->flags & FLAG_EXE) {
2385e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        // GDB already knows about the main executable
2395e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        return;
2405e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    }
2415e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2423b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes    ScopedPthreadMutexLocker locker(&gDebugMutex);
2435e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2445e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    _r_debug.r_state = RT_DELETE;
2455e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2465e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2475e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    remove_soinfo_from_debug_map(info);
2485e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2495e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    _r_debug.r_state = RT_CONSISTENT;
2505e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
25318a206c81d9743481e364384affd43306911283dElliott Hughesvoid notify_gdb_of_libraries() {
2541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_ADD;
2551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_CONSISTENT;
2571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
26016084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerstatic soinfo *soinfo_alloc(const char *name)
2611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
2624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (strlen(name) >= SOINFO_NAME_LEN) {
2634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("library name \"%s\" too long", name);
264943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan        return NULL;
2651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
26716084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    /* The freelist is populated when we call soinfo_free(), which in turn is
2681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       done only by dlclose(), which is not likely to be used.
2691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    */
2701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (!freelist) {
2714688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (socount == SO_MAX) {
2724688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("too many libraries when loading \"%s\"", name);
2731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return NULL;
2741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
2751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        freelist = sopool + socount++;
2761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        freelist->next = NULL;
2771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo* si = freelist;
2801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    freelist = freelist->next;
2811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Make sure we get a clean block of soinfo */
2831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    memset(si, 0, sizeof(soinfo));
284be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    strlcpy((char*) si->name, name, sizeof(si->name));
2851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    sonext->next = si;
2861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->next = NULL;
2871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->refcount = 0;
2881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    sonext = si;
2891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
2911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si;
2921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void soinfo_free(soinfo* si)
2951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
2964688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si == NULL) {
2974688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return;
2984688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
2994688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *prev = NULL, *trav;
3011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);
3031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(trav = solist; trav != NULL; trav = trav->next){
3051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (trav == si)
3061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
3071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        prev = trav;
3081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (trav == NULL) {
3101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* si was not ni solist */
3114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("name \"%s\" is not in solist!", si->name);
3121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
3131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
315be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    /* prev will never be NULL, because the first entry in solist is
3161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       always the static libdl_info.
3171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    */
3181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    prev->next = si->next;
3191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si == sonext) sonext = prev;
3201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->next = freelist;
3211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    freelist = si;
3221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#ifdef ANDROID_ARM_LINKER
3254688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* For a given PC, find the .so that it belongs to.
3271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the base address of the .ARM.exidx section
3281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for that .so, and the number of 8-byte entries
3291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in that section (via *pcount).
3301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Intended to be called by libc's __gnu_Unwind_Find_exidx().
3321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3333b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes * This function is exposed via dlfcn.cpp and libdl.so.
3341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
3351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
3361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
3381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned addr = (unsigned)pc;
3391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
340468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    for (si = solist; si != 0; si = si->next){
341468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if ((addr >= si->base) && (addr < (si->base + si->size))) {
342468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            *pcount = si->ARM_exidx_count;
343f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            return (_Unwind_Ptr)si->ARM_exidx;
3441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
3451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   *pcount = 0;
3471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return NULL;
3481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
350d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
3514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Here, we only have to provide a callback to iterate across all the
3531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * loaded libraries. gcc_eh does the rest. */
3541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectint
355bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesdl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
3561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                void *data)
3571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int rv = 0;
359bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    for (soinfo* si = solist; si != NULL; si = si->next) {
360bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        dl_phdr_info dl_info;
3611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_addr = si->linkmap.l_addr;
3621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_name = si->linkmap.l_name;
3631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phdr = si->phdr;
3641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phnum = si->phnum;
365bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        rv = cb(&dl_info, sizeof(dl_phdr_info), data);
366bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        if (rv != 0) {
3671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
368bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        }
3691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return rv;
3711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3724688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
3741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
37516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerstatic Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name)
3761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s;
3781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *symtab = si->symtab;
3791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const char *strtab = si->strtab;
3801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned n;
3811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
3831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               name, si->name, si->base, hash, hash % si->nbucket);
3841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    n = hash % si->nbucket;
3851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
3871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        s = symtab + n;
3881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if(strcmp(strtab + s->st_name, name)) continue;
3891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
390e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            /* only concern ourselves with global and weak symbol definitions */
3911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(ELF32_ST_BIND(s->st_info)){
3921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case STB_GLOBAL:
393e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan        case STB_WEAK:
394439fa8ea8c33a7945258dfe84a71ab2665efaa9aRobin Burchell            if(s->st_shndx == SHN_UNDEF)
395439fa8ea8c33a7945258dfe84a71ab2665efaa9aRobin Burchell                continue;
3961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
3981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       name, si->name, s->st_value, s->st_size);
3991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return s;
4001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
4011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
403943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
4041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned elfhash(const char *_name)
4071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const unsigned char *name = (const unsigned char *) _name;
4091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned h = 0, g;
4101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    while(*name) {
4121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h = (h << 4) + *name++;
4131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        g = h & 0xf0000000;
4141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g;
4151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g >> 24;
4161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return h;
4181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic Elf32_Sym *
4215ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvelsoinfo_do_lookup(soinfo *si, const char *name, soinfo **lsi,
4225ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                 soinfo *needed[])
4236ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev{
424943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
425d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    Elf32_Sym *s = NULL;
4264fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    int i;
4276ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
4285ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    if (si != NULL) {
4295ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
4305ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        /*
4315ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel         * If this object was built with symbolic relocations disabled, the
4325ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel         * first place to look to resolve external references is the main
4335ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel         * executable.
4345ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel         */
4355ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
4365ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if (!si->has_DT_SYMBOLIC) {
4375ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            DEBUG("%5d %s: looking up %s in executable %s\n",
4385ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                  pid, si->name, name, somain->name);
4395ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            s = soinfo_elf_lookup(somain, elf_hash, name);
4405ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (s != NULL) {
4415ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                *lsi = somain;
4425ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                goto done;
4435ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
4445ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
4455ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
446d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich        /* Look for symbols in the local scope (the object who is
447d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich         * searching). This happens with C++ templates on i386 for some
448d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich         * reason.
449d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich         *
450d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich         * Notes on weak symbols:
451d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich         * The ELF specs are ambiguous about treatment of weak definitions in
452d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich         * dynamic linking.  Some systems return the first definition found
453d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich         * and some the first non-weak definition.   This is system dependent.
454d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich         * Here we return the first definition found for simplicity.  */
455468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
456d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich        s = soinfo_elf_lookup(si, elf_hash, name);
4575ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if (s != NULL) {
4585ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            *lsi = si;
459d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            goto done;
4605ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
461d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    }
4626ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
4634fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* Next, look for it in the preloads list */
4644fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    for(i = 0; preloads[i] != NULL; i++) {
4655ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_elf_lookup(preloads[i], elf_hash, name);
4665ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if(s != NULL) {
4675ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            *lsi = preloads[i];
4684fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            goto done;
4695ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
4704fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
4714fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
47212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    for(i = 0; needed[i] != NULL; i++) {
47312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        DEBUG("%5d %s: looking up %s in %s\n",
4745ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel              pid, si->name, name, needed[i]->name);
4755ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_elf_lookup(needed[i], elf_hash, name);
4765ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if (s != NULL) {
4775ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            *lsi = needed[i];
47812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            goto done;
4795ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
4806ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
4816ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
4826ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevdone:
4836ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    if(s != NULL) {
4846ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, "
485bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner                   "found in %s, base = 0x%08x, load bias = 0x%08x\n",
486f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee                   pid, si->name, name, s->st_value,
4875ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                   (*lsi)->name, (*lsi)->base, (*lsi)->load_bias);
4886ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        return s;
4896ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
4906ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
491943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
4926ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev}
4936ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
4946ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev/* This is used by dl_sym().  It performs symbol lookup only within the
4956ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev   specified soinfo object and not in any of its dependencies.
4966ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
49716084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' TurnerElf32_Sym *soinfo_lookup(soinfo *si, const char *name)
4981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
49916084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    return soinfo_elf_lookup(si, elfhash(name), name);
5001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5026ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev/* This is used by dl_sym().  It performs a global symbol lookup.
5036ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
5041698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt FischerElf32_Sym *lookup(const char *name, soinfo **found, soinfo *start)
5051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
506943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
5071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s = NULL;
5081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
5091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5101698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer    if(start == NULL) {
5111698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer        start = solist;
5121698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer    }
5131698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer
5141698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer    for(si = start; (s == NULL) && (si != NULL); si = si->next)
5151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
5166ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        if(si->flags & FLAG_ERROR)
5171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
51816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner        s = soinfo_elf_lookup(si, elf_hash, name);
5191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (s != NULL) {
5209ea64da6c511e8f9f4edae4c10c20879957631abIliyan Malchev            *found = si;
5211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
5221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
5231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
5241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5256ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    if(s != NULL) {
5261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
5271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                   "si->base = 0x%08x\n", pid, name, s->st_value, si->base);
5281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return s;
5291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
5301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
531943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
5321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
534bda5da074eab4bdf374e1f4a19d480c62c72f5ffMathias Agopiansoinfo *find_containing_library(const void *addr)
535e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer{
536e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    soinfo *si;
537e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
538e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    for(si = solist; si != NULL; si = si->next)
539e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    {
540e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
541e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer            return si;
542e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        }
543e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
544e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
545e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    return NULL;
546e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
547e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
54816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' TurnerElf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr)
549e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer{
550e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    unsigned int i;
551e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    unsigned soaddr = (unsigned)addr - si->base;
552e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
553e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    /* Search the library's symbol table for any defined symbol which
554e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer     * contains this address */
555e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    for(i=0; i<si->nchain; i++) {
556e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        Elf32_Sym *sym = &si->symtab[i];
557e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
558e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        if(sym->st_shndx != SHN_UNDEF &&
559e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer           soaddr >= sym->st_value &&
560e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer           soaddr < sym->st_value + sym->st_size) {
561e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer            return sym;
562e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        }
563e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
564e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
565e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    return NULL;
566e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
567e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
5681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0
5691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void dump(soinfo *si)
5701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
5711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s = si->symtab;
5721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned n;
5731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(n = 0; n < si->nchain; n++) {
5751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
5761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               s->st_info, s->st_shndx, s->st_value, s->st_size,
5771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               si->strtab + s->st_name);
5781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        s++;
5791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
5801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
5821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
58316084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerstatic const char * const sopaths[] = {
584fedbcde6ef552e84bf7ce7598bca7dddf1722d6aBrian Swetland    "/vendor/lib",
5851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    "/system/lib",
5861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    0
5871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project};
5881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
589bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic int _open_lib(const char* name) {
590bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    // TODO: why not just call open?
591bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    struct stat sb;
592bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (stat(name, &sb) == -1 || !S_ISREG(sb.st_mode)) {
593bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        return -1;
5941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
595bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    return TEMP_FAILURE_RETRY(open(name, O_RDONLY));
5961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int open_library(const char *name)
5991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
6001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int fd;
6011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    char buf[512];
60216084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    const char * const*path;
603bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    int n;
6041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("[ %5d opening %s ]\n", pid, name);
6061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if(name == 0) return -1;
6081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if(strlen(name) > 256) return -1;
6091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
6111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return fd;
6121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
613bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    for (path = ldpaths; *path; path++) {
6145c734644eebf8d01be1e86cbe20a111a5c5a2738David 'Digit' Turner        n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
615bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley        if (n < 0 || n >= (int)sizeof(buf)) {
616bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley            WARN("Ignoring very long library path: %s/%s\n", *path, name);
617bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley            continue;
618bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley        }
619bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley        if ((fd = _open_lib(buf)) >= 0)
620bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley            return fd;
621bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    }
6221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (path = sopaths; *path; path++) {
6235c734644eebf8d01be1e86cbe20a111a5c5a2738David 'Digit' Turner        n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
624bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley        if (n < 0 || n >= (int)sizeof(buf)) {
625bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley            WARN("Ignoring very long library path: %s/%s\n", *path, name);
626bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley            continue;
627bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley        }
6281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if ((fd = _open_lib(buf)) >= 0)
6291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return fd;
6301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return -1;
6331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Returns 'true' if the library is prelinked or on failure so we error out
6364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// either way. We no longer support prelinking.
6374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic bool is_prelinked(int fd, const char* name)
6381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
6394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    struct prelink_info_t {
6404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        long mmap_addr;
6414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        char tag[4]; // "PRE ".
6424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    };
6434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
644bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
6451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (sz < 0) {
6464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("lseek failed: %s", strerror(errno));
6474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return true;
6481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
650bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    prelink_info_t info;
6518dfc073b1487ab25ab483ab346cee1d9e584adb1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
6528dfc073b1487ab25ab483ab346cee1d9e584adb1Elliott Hughes    if (rc != sizeof(info)) {
6534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("could not read prelink_info_t structure for \"%s\":", name, strerror(errno));
6544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return true;
6551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (memcmp(info.tag, "PRE ", 4) == 0) {
6584688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("prelinked libraries no longer supported: %s", name);
6594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return true;
6601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    return false;
6621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
66416084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner/* verify_elf_header
66516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner *      Verifies the content of an ELF header.
6661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
6671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Args:
6681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
6691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns:
6701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *       0 on success
6711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *      -1 if no valid ELF object is found @ base.
6721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
6731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
67416084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerverify_elf_header(const Elf32_Ehdr* hdr)
6751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
6761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
6771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
6781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
6791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
680d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    if (hdr->e_type != ET_DYN) return -1;
6811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* TODO: Should we verify anything else in the header? */
683897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#ifdef ANDROID_ARM_LINKER
684897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang    if (hdr->e_machine != EM_ARM) return -1;
685897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#elif defined(ANDROID_X86_LINKER)
686897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang    if (hdr->e_machine != EM_386) return -1;
687d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_MIPS_LINKER)
688d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if (hdr->e_machine != EM_MIPS) return -1;
689897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#endif
6901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
6911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6934688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct scoped_fd {
6944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ~scoped_fd() {
6954688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (fd != -1) {
6964688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            close(fd);
6974688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
6984688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
6994688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    int fd;
7004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes};
7011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7024688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct soinfo_ptr {
7034688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo_ptr(const char* name) {
7044688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        const char* bname = strrchr(name, '/');
7054688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        ptr = soinfo_alloc(bname ? bname + 1 : name);
7064688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7074688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ~soinfo_ptr() {
7084688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        soinfo_free(ptr);
7094688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7104688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo* release() {
7114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        soinfo* result = ptr;
7124688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        ptr = NULL;
7134688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return result;
7144688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7154688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo* ptr;
7164688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes};
7171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7184688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// TODO: rewrite linker_phdr.h to use a class, then lose this.
7194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct phdr_ptr {
7204688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    phdr_ptr() : phdr_mmap(NULL) {}
7214688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ~phdr_ptr() {
7224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (phdr_mmap != NULL) {
7234688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            phdr_table_unload(phdr_mmap, phdr_size);
7244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
7254688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    void* phdr_mmap;
7274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr phdr_size;
7284688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes};
729b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner
7304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic soinfo* load_library(const char* name)
7314688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes{
7324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Open the file.
7334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    scoped_fd fd;
7344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    fd.fd = open_library(name);
7354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (fd.fd == -1) {
7364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("library \"%s\" not found", name);
7371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
7382e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    }
7391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Read the ELF header.
7414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Ehdr header[1];
7424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header)));
74323363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ret < 0) {
7444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't read file \"%s\": %s", name, strerror(errno));
7454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
74623363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
74723363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ret != (int)sizeof(header)) {
7484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("too small to be an ELF executable: %s", name);
7494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
75023363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
75123363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (verify_elf_header(header) < 0) {
7524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("not a valid ELF executable: %s", name);
7534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
7541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Read the program header table.
7574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    const Elf32_Phdr* phdr_table;
7584688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    phdr_ptr phdr_holder;
7594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ret = phdr_table_load(fd.fd, header->e_phoff, header->e_phnum,
7604688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                          &phdr_holder.phdr_mmap, &phdr_holder.phdr_size, &phdr_table);
76123363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ret < 0) {
7624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't load program header table: %s: %s", name, strerror(errno));
7634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
7641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7654688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    size_t phdr_count = header->e_phnum;
7661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Get the load extents.
7684688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
7694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    TRACE("[ %5d - '%s' wants sz=0x%08x ]\n", pid, name, ext_sz);
77023363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ext_sz == 0) {
7714688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("no loadable segments in file: %s", name);
7724688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
77323363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
77423363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner
7754688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // We no longer support pre-linked libraries.
7764688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (is_prelinked(fd.fd, name)) {
7774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
77823363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
77916084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner
7804688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Reserve address space for all loadable segments.
7814688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    void* load_start = NULL;
7824688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr load_size = 0;
7834688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr load_bias = 0;
784b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    ret = phdr_table_reserve_memory(phdr_table,
785b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    phdr_count,
786b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    &load_start,
787b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    &load_size,
788b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    &load_bias);
789b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (ret < 0) {
7904688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't reserve %d bytes in address space for \"%s\": %s",
7914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               ext_sz, name, strerror(errno));
7924688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
793b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
7941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
796b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner          pid, name, load_start, load_size);
797b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner
798b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* Map all the segments in our address space with default protections */
799b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    ret = phdr_table_load_segments(phdr_table,
800b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                   phdr_count,
801b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                   load_bias,
8024688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                                   fd.fd);
803b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (ret < 0) {
8044688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't map loadable segments for \"%s\": %s",
8054688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               name, strerror(errno));
8064688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
807b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
8081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8094688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo_ptr si(name);
8104688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si.ptr == NULL) {
8114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
8121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8144688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->base = (Elf32_Addr) load_start;
8154688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->size = load_size;
8164688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->load_bias = load_bias;
8174688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->flags = 0;
8184688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->entry = 0;
8194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->dynamic = (unsigned *)-1;
8204688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->phnum = phdr_count;
8214688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
8224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si.ptr->phdr == NULL) {
8234688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't find loaded PHDR for \"%s\"", name);
8244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
82523363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
8264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
8274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    return si.release();
8281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic soinfo *
8311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectinit_library(soinfo *si)
8321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
8331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* At this point we know that whatever is loaded @ base is a valid ELF
8341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * shared library whose segments are properly mapped in. */
8351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
8361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project          pid, si->base, si->size, si->name);
8371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8385135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if(soinfo_link_image(si)) {
8391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        munmap((void *)si->base, si->size);
8401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
8411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si;
8441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
84612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvelstatic soinfo *find_loaded_library(const char *name)
8471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
8481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
8496774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    const char *bname;
8506774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
85112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // TODO: don't use basename only for determining libraries
85212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // http://code.google.com/p/android/issues/detail?id=6670
85312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
85412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    bname = strrchr(name, '/');
85512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    bname = bname ? bname + 1 : name;
85612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
85712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    for(si = solist; si != NULL; si = si->next){
85812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        if(!strcmp(bname, si->name)) {
85912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            return si;
86012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        }
86112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    }
86212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    return NULL;
86312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel}
86412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
86512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvelsoinfo *find_library(const char *name)
86612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel{
86712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    soinfo *si;
86812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
8696774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    if (name == NULL)
8706774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner        return somain;
8716774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
87212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    si = find_loaded_library(name);
87312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    if (si != NULL) {
87412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        if(si->flags & FLAG_ERROR) {
87512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            DL_ERR("\"%s\" failed to load previously", name);
8762e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin            return NULL;
8771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
87812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        if(si->flags & FLAG_LINKED) return si;
87912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        DL_ERR("OOPS: recursive link to \"%s\"", si->name);
88012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        return NULL;
8811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("[ %5d '%s' has not been loaded yet.  Locating...]\n", pid, name);
8841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si = load_library(name);
8851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if(si == NULL)
8861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
8871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return init_library(si);
8881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void call_destructors(soinfo *si);
891bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
892bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesint soinfo_unload(soinfo* si) {
8931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si->refcount == 1) {
8941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("%5d unloading '%s'\n", pid, si->name);
8951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        call_destructors(si);
8961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
897bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        for (unsigned* d = si->dynamic; *d; d += 2) {
8981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if(d[0] == DT_NEEDED){
89912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                soinfo *lsi = find_loaded_library(si->strtab + d[1]);
90012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                if (lsi) {
9016ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev                    TRACE("%5d %s needs to unload %s\n", pid,
9026ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev                          si->name, lsi->name);
90316084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner                    soinfo_unload(lsi);
904bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes                } else {
905bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes                    // TODO: should we return -1 in this case?
9064688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("\"%s\": could not unload dependent library",
9074688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                           si->name);
908bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes                }
9091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
9101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
9111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        munmap((char *)si->base, si->size);
9135e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        notify_gdb_of_unload(si);
91416084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner        soinfo_free(si);
9151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        si->refcount = 0;
916bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    } else {
9171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        si->refcount--;
9181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
9191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              pid, si->name, si->refcount);
9201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
921bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    return 0;
9221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
9231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* TODO: don't use unsigned for addrs below. It works, but is not
9251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
9261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * long.
9271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
92812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvelstatic int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count,
92912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                           soinfo *needed[])
9301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
9311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *symtab = si->symtab;
9321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const char *strtab = si->strtab;
9331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s;
9341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Rel *start = rel;
9355ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    soinfo *lsi;
9361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    for (size_t idx = 0; idx < count; ++idx, ++rel) {
9381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned type = ELF32_R_TYPE(rel->r_info);
9391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned sym = ELF32_R_SYM(rel->r_info);
940bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner        unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
9411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned sym_addr = 0;
9421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        char *sym_name = NULL;
9431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
9451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              si->name, idx);
946d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (type == 0) { // R_*_NONE
947d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            continue;
948d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
9491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if(sym != 0) {
950d1b40d8c69dfca94185ea9231f6cb16f70c02e39Dima Zavin            sym_name = (char *)(strtab + symtab[sym].st_name);
9515ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            s = soinfo_do_lookup(si, sym_name, &lsi, needed);
952e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            if(s == NULL) {
953e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* We only allow an undefined symbol if this is a weak
954e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   reference..   */
955e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                s = &symtab[sym];
956e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
957e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes                    DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
958e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
959e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
960e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
961e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* IHI0044C AAELF 4.5.1.1:
962e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
963e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   Libraries are not searched to resolve weak references.
964e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   It is not an error for a weak reference to remain
965e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   unsatisfied.
966e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
967e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   During linking, the value of an undefined weak reference is:
968e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - Zero if the relocation type is absolute
969e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - The address of the place if the relocation is pc-relative
970bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes                   - The address of nominal base address if the relocation
971e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                     type is base-relative.
972e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                  */
973e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
974e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                switch (type) {
975e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_ARM_LINKER)
976e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_JUMP_SLOT:
977e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_GLOB_DAT:
978e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_ABS32:
979e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_RELATIVE:    /* Don't care. */
980e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#elif defined(ANDROID_X86_LINKER)
981d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                case R_386_JMP_SLOT:
982e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_GLOB_DAT:
983e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_32:
984e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_RELATIVE:    /* Dont' care. */
985e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_*_LINKER */
986e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    /* sym_addr was initialized to be zero above or relocation
987e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       code below does not care about value of sym_addr.
988e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       No need to do anything.  */
989e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
990e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
991e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_X86_LINKER)
992e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_PC32:
993e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    sym_addr = reloc;
994e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
995e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_X86_LINKER */
996e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
997e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_ARM_LINKER)
998e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_COPY:
999e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    /* Fall through.  Can't really copy if weak symbol is
1000e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       not found in run-time.  */
1001e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_ARM_LINKER */
1002e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                default:
10034688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("unknown weak reloc type %d @ %p (%d)",
10044688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                                 type, rel, (int) (rel - start));
1005e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1006e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1007e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            } else {
1008e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* We got a definition.  */
10091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0
1010d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                if((base == 0) && (si->base != 0)){
1011d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                        /* linking from libraries to main image is bad */
10124688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("cannot locate \"%s\"...",
10134688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                           strtab + symtab[sym].st_name);
1014d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                    return -1;
1015d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                }
10161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
10175ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                sym_addr = (unsigned)(s->st_value + lsi->load_bias);
1018d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1019bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocSymbol);
10201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        } else {
1021e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            s = NULL;
10221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
10231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
10241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* TODO: This is ugly. Split up the relocations by arch into
10251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * different files.
10261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
10271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(type){
10281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_ARM_LINKER)
10291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_JUMP_SLOT:
1030bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
10311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
10321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
10331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, sym_addr, sym_name);
10341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
10351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
10361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_GLOB_DAT:
1037bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
10381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
10391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
10401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, sym_addr, sym_name);
10411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
10421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
10431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_ABS32:
1044bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
10451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
10461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
10471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, sym_addr, sym_name);
10481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) += sym_addr;
10491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
105034ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner        case R_ARM_REL32:
1051bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
105234ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            MARK(rel->r_offset);
105334ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
105434ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner                       reloc, sym_addr, rel->r_offset, sym_name);
105534ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            *((unsigned*)reloc) += sym_addr - rel->r_offset;
105634ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            break;
10571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#elif defined(ANDROID_X86_LINKER)
1058d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case R_386_JMP_SLOT:
1059bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
10601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
10611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
10621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, sym_addr, sym_name);
10631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
10641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
10651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_GLOB_DAT:
1066bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
10671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
10681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
10691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, sym_addr, sym_name);
10701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
10711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1072d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_MIPS_LINKER)
1073d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    case R_MIPS_JUMP_SLOT:
1074bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
1075d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            MARK(rel->r_offset);
1076d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1077d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                       reloc, sym_addr, sym_name);
1078d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *((unsigned*)reloc) = sym_addr;
1079d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1080d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    case R_MIPS_REL32:
1081bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
1082d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            MARK(rel->r_offset);
1083d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x %s\n", pid,
1084d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                       reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1085d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            if (s) {
1086d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                *((unsigned*)reloc) += sym_addr;
1087d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            } else {
1088d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                *((unsigned*)reloc) += si->base;
1089d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1090d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
10911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_*_LINKER */
10921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
10931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_ARM_LINKER)
10941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_RELATIVE:
10951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#elif defined(ANDROID_X86_LINKER)
10961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_RELATIVE:
10971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_*_LINKER */
1098bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
10991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
11004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            if (sym) {
11014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("odd RELATIVE form...", pid);
11021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                return -1;
11031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
11041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
11051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, si->base);
11061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) += si->base;
11071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_X86_LINKER)
11101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_32:
1111bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
11121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
11131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
11151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, sym_addr, sym_name);
11161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned *)reloc) += (unsigned)sym_addr;
11171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_PC32:
1120bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
11211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
11221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
11231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       "+%08x (%08x - %08x) %s\n", pid, reloc,
11241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       (sym_addr - reloc), sym_addr, reloc, sym_name);
11251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
11261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_X86_LINKER */
11281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef ANDROID_ARM_LINKER
11301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_COPY:
1131d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            if ((si->flags & FLAG_EXE) == 0) {
1132d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                /*
1133d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1134d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 *
1135d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * Section 4.7.1.10 "Dynamic relocations"
1136d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * R_ARM_COPY may only appear in executable objects where e_type is
1137d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * set to ET_EXEC.
1138d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 *
1139d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1140d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * We should explicitly disallow ET_DYN executables from having
1141d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * R_ARM_COPY relocations.
1142d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 */
1143d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1144d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                return -1;
1145d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            }
1146bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocCopy);
11471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
11481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
11491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, s->st_size, sym_addr, sym_name);
1150d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            if (reloc == sym_addr) {
11515ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
11525ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
11535ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (src == NULL) {
11545ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
11555ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
11565ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
11575ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (lsi->has_DT_SYMBOLIC) {
11585ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
11595ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                           "library %s (built with -Bsymbolic?)", si->name, lsi->name);
11605ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
11615ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
11625ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (s->st_size < src->st_size) {
11635ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
11645ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                           si->name, s->st_size, src->st_size);
11655ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
11665ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
11675ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
11685ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            } else {
11695ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
1170d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                return -1;
1171d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            }
11721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_ARM_LINKER */
11741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        default:
11764688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("unknown reloc type %d @ %p (%d)",
11774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                   type, rel, (int) (rel - start));
11781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return -1;
11791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
11801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
11811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
11821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
11831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1184d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#ifdef ANDROID_MIPS_LINKER
1185bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic int mips_relocate_got(soinfo* si, soinfo* needed[]) {
1186d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned *got;
1187d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned local_gotno, gotsym, symtabno;
1188d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    Elf32_Sym *symtab, *sym;
1189d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned g;
1190d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1191d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got;
1192d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    local_gotno = si->mips_local_gotno;
1193d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    gotsym = si->mips_gotsym;
1194d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    symtabno = si->mips_symtabno;
1195d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    symtab = si->symtab;
1196d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1197d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    /*
1198d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * got[0] is address of lazy resolver function
1199d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * got[1] may be used for a GNU extension
1200bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes     * set it to a recognizable address in case someone calls it
1201d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * (should be _rtld_bind_start)
1202d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * FIXME: maybe this should be in a separate routine
1203d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     */
1204d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1205d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if ((si->flags & FLAG_LINKER) == 0) {
1206d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        g = 0;
1207d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        got[g++] = 0xdeadbeef;
1208d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (got[g] & 0x80000000) {
1209d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            got[g++] = 0xdeadfeed;
1210d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1211d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        /*
1212d7daacb46372132ae3f0121647074936c304b572Raghu Gandham         * Relocate the local GOT entries need to be relocated
1213d7daacb46372132ae3f0121647074936c304b572Raghu Gandham         */
1214d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        for (; g < local_gotno; g++) {
1215d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            got[g] += si->load_bias;
1216d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1217d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1218d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1219d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    /* Now for the global GOT entries */
1220d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    sym = symtab + gotsym;
1221d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got + local_gotno;
1222d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    for (g = gotsym; g < symtabno; g++, sym++, got++) {
1223d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        const char *sym_name;
1224d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        Elf32_Sym *s;
12255ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        soinfo *lsi;
1226d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1227d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        /* This is an undefined reference... try to locate it */
1228d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        sym_name = si->strtab + sym->st_name;
12295ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1230d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (s == NULL) {
1231d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* We only allow an undefined symbol if this is a weak
1232d7daacb46372132ae3f0121647074936c304b572Raghu Gandham               reference..   */
1233d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            s = &symtab[g];
1234d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
12354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("cannot locate \"%s\"...", sym_name);
1236d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                return -1;
1237d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1238d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *got = 0;
1239d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1240d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        else {
1241d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* FIXME: is this sufficient?
1242d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             * For reference see NetBSD link loader
1243d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             * 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
1244d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             */
12455ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel             *got = lsi->load_bias + s->st_value;
1246d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1247d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1248d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    return 0;
1249d7daacb46372132ae3f0121647074936c304b572Raghu Gandham}
1250d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1251d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
12528215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner/* Please read the "Initialization and Termination functions" functions.
12538215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * of the linker design note in bionic/linker/README.TXT to understand
12548215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * what the following code is doing.
12558215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
12568215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * The important things to remember are:
12578215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
12588215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_PREINIT_ARRAY must be called first for executables, and should
12598215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   not appear in shared libraries.
12608215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
12618215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_INIT should be called before DT_INIT_ARRAY if both are present
12628215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
12638215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_FINI should be called after DT_FINI_ARRAY if both are present
12648215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
12658215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_FINI_ARRAY must be parsed in reverse order.
12668215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner */
12678215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
12688215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turnerstatic void call_array(unsigned *ctor, int count, int reverse)
12691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
12708215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner    int n, inc = 1;
12718215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
12728215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner    if (reverse) {
12738215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        ctor += (count-1);
12748215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        inc   = -1;
12758215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner    }
12768215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
12778215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner    for(n = count; n > 0; n--) {
12788215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
12798215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner              reverse ? "dtor" : "ctor",
12801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              (unsigned)ctor, (unsigned)*ctor);
12818215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        void (*func)() = (void (*)()) *ctor;
12828215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        ctor += inc;
12831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if(((int) func == 0) || ((int) func == -1)) continue;
12841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
12851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        func();
12861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
12871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
12881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12899181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanovstatic void soinfo_call_preinit_constructors(soinfo *si)
12909181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov{
12919181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov  TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
12929181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov      pid, (unsigned)si->preinit_array, si->preinit_array_count,
12939181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov      si->name);
12949181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov  call_array(si->preinit_array, si->preinit_array_count, 0);
12959181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov  TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
12969181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov}
12979181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
129816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnervoid soinfo_call_constructors(soinfo *si)
12991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
1300e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov    if (si->constructors_called)
1301e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov        return;
1302e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1303f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    // Set this before actually calling the constructors, otherwise it doesn't
1304f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    // protect against recursive constructor calls. One simple example of
1305f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    // constructor recursion is the libc debug malloc, which is implemented in
1306f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    // libc_malloc_debug_leak.so:
1307f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    // 1. The program depends on libc, so libc's constructor is called here.
1308f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
130916084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    // 3. dlopen() calls soinfo_call_constructors() with the newly created
1310f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    //    soinfo for libc_malloc_debug_leak.so.
131116084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    // 4. The debug so depends on libc, so soinfo_call_constructors() is
1312f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    //    called again with the libc soinfo. If it doesn't trigger the early-
1313f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    //    out above, the libc constructor will be called again (recursively!).
1314f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    si->constructors_called = 1;
1315f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall
13169181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov    if (!(si->flags & FLAG_EXE) && si->preinit_array) {
13179181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov      DL_ERR("shared library \"%s\" has a preinit_array table @ 0x%08x. "
13189181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov          "This is INVALID.", si->name, (unsigned) si->preinit_array);
13191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1321e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov    if (si->dynamic) {
1322e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov        unsigned *d;
1323e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov        for(d = si->dynamic; *d; d += 2) {
1324e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov            if(d[0] == DT_NEEDED){
132512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                soinfo* lsi = find_loaded_library(si->strtab + d[1]);
132612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                if (!lsi) {
132712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                    DL_ERR("\"%s\": could not initialize dependent library",
132812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                           si->name);
1329e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov                } else {
133016084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner                    soinfo_call_constructors(lsi);
1331e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov                }
1332e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov            }
1333e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov        }
1334e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov    }
1335e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
13361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si->init_func) {
13371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
13381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              (unsigned)si->init_func, si->name);
13391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        si->init_func();
13401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
13411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si->init_array) {
13441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
13451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              (unsigned)si->init_array, si->init_array_count, si->name);
13468215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        call_array(si->init_array, si->init_array_count, 0);
13471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
13481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1350e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov}
13518215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
13521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void call_destructors(soinfo *si)
13531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
13541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si->fini_array) {
13551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
13561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              (unsigned)si->fini_array, si->fini_array_count, si->name);
13578215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        call_array(si->fini_array, si->fini_array_count, 1);
13581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
13591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si->fini_func) {
13621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
13631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              (unsigned)si->fini_func, si->name);
13641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        si->fini_func();
13651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
13661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
13681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with
13701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   /dev/null. */
13715419b9474753d25dff947c7740532f86d130c0beElliott Hughesstatic int nullify_closed_stdio() {
13721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int dev_null, i, status;
13731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int return_value = 0;
13741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
137516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
13761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null < 0) {
13774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("cannot open /dev/null: %s", strerror(errno));
13781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return -1;
13791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
13811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If any of the stdio file descriptors is valid and not associated
13831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       with /dev/null, dup /dev/null to it.  */
13841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (i = 0; i < 3; i++) {
13851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* If it is /dev/null already, we are done. */
13864688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (i == dev_null) {
13871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
13884688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
13891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
13914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
13921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13934688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        /* If file is opened, we are good. */
13944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status != -1) {
13954688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            continue;
13964688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
13971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* The only error we allow is that the file descriptor does not
13991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           exist, in which case we dup /dev/null to it. */
14001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (errno != EBADF) {
14014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("fcntl failed: %s", strerror(errno));
14021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
14041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* Try dupping /dev/null to this stdio file descriptor and
14071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           repeat if there is a signal.  Note that any errors in closing
14081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           the stdio descriptor are lost.  */
14094688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
14101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (status < 0) {
14114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("dup2 failed: %s", strerror(errno));
14121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
14141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If /dev/null is not one of the stdio file descriptors, close it. */
14181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null > 2) {
14191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
14204688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(close(dev_null));
14214688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status == -1) {
14224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("close failed: %s", strerror(errno));
14231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return return_value;
14281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14305135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevichstatic int soinfo_link_image(soinfo *si)
14311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
14321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned *d;
1433f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee    /* "base" might wrap around UINT32_MAX. */
1434bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    Elf32_Addr base = si->load_bias;
1435b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    const Elf32_Phdr *phdr = si->phdr;
14361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int phnum = si->phnum;
1437b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    int relocating_linker = (si->flags & FLAG_LINKER) != 0;
143812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    soinfo **needed, **pneeded;
143912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    size_t dynamic_count;
14401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1441b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can't debug anything until the linker is relocated */
1442b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (!relocating_linker) {
1443b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        INFO("[ %5d linking %s ]\n", pid, si->name);
1444b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1445b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner            si->base, si->flags);
1446b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
14471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
144863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    /* Extract dynamic section */
144912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
145012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                                   &dynamic_count);
145163f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    if (si->dynamic == NULL) {
1452b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
14534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("missing PT_DYNAMIC?!");
1454b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
145563f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner        goto fail;
145663f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    } else {
1457b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1458b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner            DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1459b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
146063f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    }
146163f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
146263f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#ifdef ANDROID_ARM_LINKER
146363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    (void) phdr_table_get_arm_exidx(phdr, phnum, base,
146463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner                                    &si->ARM_exidx, &si->ARM_exidx_count);
146563f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif
146663f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
14671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* extract useful information from dynamic section */
14681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(d = si->dynamic; *d; d++){
14691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
14701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(*d++){
14711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_HASH:
1472f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->nbucket = ((unsigned *) (base + *d))[0];
1473f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->nchain = ((unsigned *) (base + *d))[1];
1474f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->bucket = (unsigned *) (base + *d + 8);
1475f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
14761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_STRTAB:
1478f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->strtab = (const char *) (base + *d);
14791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_SYMTAB:
1481f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->symtab = (Elf32_Sym *) (base + *d);
14821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTREL:
14841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if(*d != DT_REL) {
1485d00d23a9d4598108d7f498291b35c7730e48cf5cErik Gilling                DL_ERR("DT_RELA not supported");
14861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                goto fail;
14871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
14881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_JMPREL:
1490f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->plt_rel = (Elf32_Rel*) (base + *d);
14911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTRELSZ:
14931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->plt_rel_count = *d / 8;
14941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_REL:
1496f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->rel = (Elf32_Rel*) (base + *d);
14971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_RELSZ:
14991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->rel_count = *d / 8;
15001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTGOT:
15021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            /* Save this in case we decide to do lazy binding. We don't yet. */
1503f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->plt_got = (unsigned *)(base + *d);
15041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_DEBUG:
1506d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if !defined(ANDROID_MIPS_LINKER)
1507bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_DEBUG entry to the address of _r_debug for GDB
15081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *d = (int) &_r_debug;
1509d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
15101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1511ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI         case DT_RELA:
15124688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("DT_RELA not supported");
15131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            goto fail;
15141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT:
1515f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->init_func = (void (*)(void))(base + *d);
15161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            DEBUG("%5d %s constructors (init func) found at %p\n",
15171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                  pid, si->name, si->init_func);
15181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI:
1520f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->fini_func = (void (*)(void))(base + *d);
15211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            DEBUG("%5d %s destructors (fini func) found at %p\n",
15221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                  pid, si->name, si->fini_func);
15231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAY:
1525f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->init_array = (unsigned *)(base + *d);
15261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            DEBUG("%5d %s constructors (init_array) found at %p\n",
15271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                  pid, si->name, si->init_array);
15281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAYSZ:
15301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
15311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAY:
1533f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->fini_array = (unsigned *)(base + *d);
15341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            DEBUG("%5d %s destructors (fini_array) found at %p\n",
15351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                  pid, si->name, si->fini_array);
15361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAYSZ:
15381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
15391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAY:
1541f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->preinit_array = (unsigned *)(base + *d);
15421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            DEBUG("%5d %s constructors (preinit_array) found at %p\n",
15431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                  pid, si->name, si->preinit_array);
15441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAYSZ:
15461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
15471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_TEXTREL:
15495135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            si->has_text_relocations = true;
15501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15515ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_SYMBOLIC:
15525ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            si->has_DT_SYMBOLIC = true;
15535ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
15545ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel#if defined(DT_FLAGS)
15555ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_FLAGS:
15565ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (*d & DF_TEXTREL) {
15575ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_text_relocations = true;
15585ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
15595ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (*d & DF_SYMBOLIC) {
15605ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_DT_SYMBOLIC = true;
15615ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
15625ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
15635ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel#endif
1564d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if defined(ANDROID_MIPS_LINKER)
1565d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_NEEDED:
1566d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_STRSZ:
1567d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_SYMENT:
1568d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_RELENT:
1569d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             break;
1570d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_MAP:
1571bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
1572d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            {
1573bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes              r_debug** dp = (r_debug**) *d;
1574d7daacb46372132ae3f0121647074936c304b572Raghu Gandham              *dp = &_r_debug;
1575d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1576d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1577d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_VERSION:
1578d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_FLAGS:
1579d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_BASE_ADDRESS:
1580d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_UNREFEXTNO:
1581d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RWPLT:
1582d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1583d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1584d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_PLTGOT:
1585d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if 0
1586d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* not yet... */
1587d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_pltgot = (unsigned *)(si->base + *d);
1588d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1589d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1590d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1591d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_SYMTABNO:
1592d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_symtabno = *d;
1593d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1594d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1595d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_LOCAL_GOTNO:
1596d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_local_gotno = *d;
1597d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1598d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1599d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_GOTSYM:
1600d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_gotsym = *d;
1601d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1602d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1603d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        default:
1604d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            DEBUG("%5d Unused DT entry: type 0x%08x arg 0x%08x\n",
1605d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                  pid, d[-1], d[0]);
1606d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1607d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
16081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
16091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1611be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
16121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           pid, si->base, si->strtab, si->symtab);
16131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if((si->strtab == 0) || (si->symtab == 0)) {
16154688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("missing essential tables");
16161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        goto fail;
16171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16194fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* if this is the main executable, then load all of the preloads now */
16204fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    if(si->flags & FLAG_EXE) {
16214fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        int i;
16224fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        memset(preloads, 0, sizeof(preloads));
16234fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        for(i = 0; ldpreload_names[i] != NULL; i++) {
16244fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            soinfo *lsi = find_library(ldpreload_names[i]);
16254fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            if(lsi == 0) {
16264fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer                strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
16274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
16284688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                       ldpreload_names[i], si->name, tmp_err_buf);
16294fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer                goto fail;
16304fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            }
16314fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            lsi->refcount++;
16324fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            preloads[i] = lsi;
16334fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        }
16344fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
16354fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
163612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    /* dynamic_count is an upper bound for the number of needed libs */
163712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    pneeded = needed = (soinfo**) alloca((1 + dynamic_count) * sizeof(soinfo*));
163812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
16391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(d = si->dynamic; *d; d += 2) {
16401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if(d[0] == DT_NEEDED){
16411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
16422e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin            soinfo *lsi = find_library(si->strtab + d[1]);
16431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if(lsi == 0) {
16440353195f344666256dba474a15c9ba22cf0cccc9Dima Zavin                strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
16454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
16464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                       si->strtab + d[1], si->name, tmp_err_buf);
16471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                goto fail;
16481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
164912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            *pneeded++ = lsi;
16501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            lsi->refcount++;
16511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
16521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
165312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    *pneeded = NULL;
16541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16555135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
16565135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        /* Unprotect the segments, i.e. make them writable, to allow
16575135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * text relocations to work properly. We will later call
16585135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * phdr_table_protect_segments() after all of them are applied
16595135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * and all constructors are run.
16605135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         */
16615135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
16625135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't unprotect loadable segments for \"%s\": %s",
16635135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
16645135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            goto fail;
16655135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
16665135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    }
16675135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich
16681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if(si->plt_rel) {
16691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
167012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed))
16711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            goto fail;
16721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if(si->rel) {
16741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        DEBUG("[ %5d relocating %s ]\n", pid, si->name );
167512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        if(soinfo_relocate(si, si->rel, si->rel_count, needed))
16761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            goto fail;
16771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1679d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#ifdef ANDROID_MIPS_LINKER
168012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    if(mips_relocate_got(si, needed)) {
1681d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        goto fail;
1682d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1683d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1684d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
16851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_LINKED;
16861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
16871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16885135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
16895135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        /* All relocations are done, we can protect our segments back to
16905135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * read-only. */
16915135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
16925135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't protect segments for \"%s\": %s",
16935135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
16945135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            goto fail;
16955135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
16961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1698b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can also turn on GNU RELRO protection */
1699b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
17004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
17014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               si->name, strerror(errno));
1702b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        goto fail;
17039ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich    }
17049ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
17051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If this is a SET?ID program, dup /dev/null to opened stdin,
17061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       stdout and stderr to close a security hole described in:
17071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
17091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
171118a206c81d9743481e364384affd43306911283dElliott Hughes    if (get_AT_SECURE()) {
17124688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        nullify_closed_stdio();
17134688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
17141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    notify_gdb_of_load(si);
17151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
17161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectfail:
1718a716190241da07f42cf874b04bf044261f36381cDima Zavin    ERROR("failed to link %s\n", si->name);
17191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_ERROR;
17201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return -1;
17211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
17221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17234688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void parse_path(const char* path, const char* delimiters,
17244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                       const char** array, char* buf, size_t buf_size, size_t max_count)
1725bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley{
17264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (path == NULL) {
17274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return;
17284688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
1729bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
17304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    size_t len = strlcpy(buf, path, buf_size);
1731bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
17324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    size_t i = 0;
17334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    char* buf_p = buf;
17344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
17354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (*array[i] != '\0') {
1736bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley            ++i;
17374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
1738bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    }
1739bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
17404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Forget the last path if we had to truncate; this occurs if the 2nd to
17414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // last char isn't '\0' (i.e. wasn't originally a delimiter).
17424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
17434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        array[i - 1] = NULL;
1744bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    } else {
17454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        array[i] = NULL;
1746bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    }
1747bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley}
1748bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
17494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void parse_LD_LIBRARY_PATH(const char* path) {
17504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_path(path, ":", ldpaths,
17514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               ldpaths_buf, sizeof(ldpaths_buf), LDPATH_MAX);
17524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes}
17534fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
17544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void parse_LD_PRELOAD(const char* path) {
17554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // We have historically supported ':' as well as ' ' in LD_PRELOAD.
17564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_path(path, " :", ldpreload_names,
17574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               ldpreloads_buf, sizeof(ldpreloads_buf), LDPRELOAD_MAX);
17584fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer}
17594fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1760468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1761468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and
1762468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs
1763468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point.
1764468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
1765bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissellstatic unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base)
17661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
17671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    static soinfo linker_soinfo;
17681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int argc = (int) *elfdata;
17701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    char **argv = (char**) (elfdata + 1);
1771bb44055d0a3515410f45417d5b0e047cc60f0dc6Stephen Smalley    unsigned *vecs = (unsigned*) (argv + argc + 1);
17721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1773ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner    /* NOTE: we store the elfdata pointer on a special location
1774ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       of the temporary TLS area in order to pass it to
1775ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       the C Library's runtime initializer.
1776ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *
1777ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       The initializer must clear the slot and reset the TLS
1778ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       to point to a different location to ensure that no other
1779ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       shared library constructor can access it.
1780ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     */
17811a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    __libc_init_tls(elfdata);
17821a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov
17831a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    pid = getpid();
17841a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov
17851a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING
17861a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    struct timeval t0, t1;
17871a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    gettimeofday(&t0, 0);
17881a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif
17891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
179018a206c81d9743481e364384affd43306911283dElliott Hughes    // Initialize environment functions, and get to the ELF aux vectors table.
1791be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    vecs = linker_env_init(vecs);
1792be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
17931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    debugger_init();
17941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
179518a206c81d9743481e364384affd43306911283dElliott Hughes    // Get a few environment variables.
17968c4f3ce8d084e3949d77ad8743593b5c83c3d41cNick Kralevich#if LINKER_DEBUG
179718a206c81d9743481e364384affd43306911283dElliott Hughes    {
179818a206c81d9743481e364384affd43306911283dElliott Hughes        const char* env = linker_env_get("LD_DEBUG");
179918a206c81d9743481e364384affd43306911283dElliott Hughes        if (env != NULL) {
1800be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner            debug_verbosity = atoi(env);
180118a206c81d9743481e364384affd43306911283dElliott Hughes        }
180218a206c81d9743481e364384affd43306911283dElliott Hughes    }
18038c4f3ce8d084e3949d77ad8743593b5c83c3d41cNick Kralevich#endif
1804be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
180518a206c81d9743481e364384affd43306911283dElliott Hughes    // Normally, these are cleaned by linker_env_init, but the test
180618a206c81d9743481e364384affd43306911283dElliott Hughes    // doesn't cost us anything.
180718a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpath_env = NULL;
180818a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpreload_env = NULL;
180918a206c81d9743481e364384affd43306911283dElliott Hughes    if (!get_AT_SECURE()) {
181018a206c81d9743481e364384affd43306911283dElliott Hughes      ldpath_env = linker_env_get("LD_LIBRARY_PATH");
181118a206c81d9743481e364384affd43306911283dElliott Hughes      ldpreload_env = linker_env_get("LD_PRELOAD");
18121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    INFO("[ android linker & debugger ]\n");
18151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
18161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
181718a206c81d9743481e364384affd43306911283dElliott Hughes    soinfo* si = soinfo_alloc(argv[0]);
181818a206c81d9743481e364384affd43306911283dElliott Hughes    if (si == NULL) {
181918a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
18201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1822d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    /* bootstrap the link map, the main exe always needs to be first */
18231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_EXE;
1824bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    link_map* map = &(si->linkmap);
18251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = 0;
18271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_name = argv[0];
18281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_prev = NULL;
18291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_next = NULL;
18301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_map = map;
18321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
18331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1834bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell        /* gdb expects the linker to be in the debug shared object list.
1835bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * Without this, gdb has trouble locating the linker's ".text"
1836bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * and ".plt" sections. Gdb could also potentially use this to
1837bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1838bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * Don't use soinfo_alloc(), because the linker shouldn't
18391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         * be on the soinfo list.
18401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         */
1841be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
18421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    linker_soinfo.flags = 0;
1843bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell    linker_soinfo.base = linker_base;
184406f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng    /*
184506f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     * Set the dynamic field in the link map otherwise gdb will complain with
184606f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     * the following:
184706f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     *   warning: .dynamic section for "/system/bin/linker" is not at the
184806f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     *   expected address (wrong library or version mismatch?)
184906f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     */
185006f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng    Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
185106f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng    Elf32_Phdr *phdr =
185206f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng        (Elf32_Phdr *)((unsigned char *) linker_base + elf_hdr->e_phoff);
185312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
185412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                                   &linker_soinfo.dynamic, NULL);
18551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(&linker_soinfo);
18561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1857d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    /* extract information passed from the kernel */
185818a206c81d9743481e364384affd43306911283dElliott Hughes    while (vecs[0] != 0){
18591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(vecs[0]){
18601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case AT_PHDR:
18611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->phdr = (Elf32_Phdr*) vecs[1];
18621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case AT_PHNUM:
18641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->phnum = (int) vecs[1];
18651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case AT_ENTRY:
18671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->entry = vecs[1];
18681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
18701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        vecs += 2;
18711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18738180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    /* Compute the value of si->base. We can't rely on the fact that
18748180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * the first entry is the PHDR because this will not be true
18758180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * for certain executables (e.g. some in the NDK unit test suite)
18768180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     */
18778180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    int nn;
18788180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    si->base = 0;
1879b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    si->size = phdr_table_get_load_size(si->phdr, si->phnum);
1880bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    si->load_bias = 0;
18818180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    for ( nn = 0; nn < si->phnum; nn++ ) {
18828180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner        if (si->phdr[nn].p_type == PT_PHDR) {
1883bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner            si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr;
1884bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner            si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset;
18858180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner            break;
18868180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner        }
18878180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    }
18881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->dynamic = (unsigned *)-1;
18896774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    si->refcount = 1;
18901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
18924688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_LIBRARY_PATH(ldpath_env);
18934688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_PRELOAD(ldpreload_env);
18944fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
18955ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    somain = si;
18965ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
18975135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if(soinfo_link_image(si)) {
18982e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin        char errmsg[] = "CANNOT LINK EXECUTABLE\n";
18992e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin        write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
19002e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin        write(2, errmsg, sizeof(errmsg));
190118a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
19021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
19031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19049181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov    soinfo_call_preinit_constructors(si);
19059181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
190618a206c81d9743481e364384affd43306911283dElliott Hughes    for (size_t i = 0; preloads[i] != NULL; ++i) {
1907326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng        soinfo_call_constructors(preloads[i]);
1908326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng    }
1909326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng
19109c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin    /*After the link_image, the si->base is initialized.
19119c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     *For so lib, the map->l_addr will be updated in notify_gdb_of_load.
19129c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     *We need to update this value for so exe here. So Unwind_Backtrace
19139c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     *for some arch like x86 could work correctly within so exe.
19149c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     */
19159c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin    map->l_addr = si->base;
191616084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    soinfo_call_constructors(si);
1917e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
19181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING
19191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    gettimeofday(&t1,NULL);
19201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
19211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
19221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
19231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               ));
19241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
19261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
1927bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocAbsolute],
1928bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocRelative],
1929bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocCopy],
1930bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocSymbol]);
19311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
19331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
19341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned n;
19351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned i;
19361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned count = 0;
19371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        for(n = 0; n < 4096; n++){
19381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if(bitmask[n]){
19391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                unsigned x = bitmask[n];
19401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                for(i = 0; i < 8; i++){
19411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    if(x & 1) count++;
19421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    x >>= 1;
19431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                }
19441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
19451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
19461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
19471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
19481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES
19511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    fflush(stdout);
19521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
19551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project          si->entry);
19561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si->entry;
19571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
1958468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1959468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1960468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Find the value of AT_BASE passed to us by the kernel. This is the load
1961468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * location of the linker.
1962468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
1963468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevichstatic unsigned find_linker_base(unsigned **elfdata) {
1964468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    int argc = (int) *elfdata;
1965468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    char **argv = (char**) (elfdata + 1);
1966468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    unsigned *vecs = (unsigned*) (argv + argc + 1);
1967468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    while (vecs[0] != 0) {
1968468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        vecs++;
1969468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    }
1970468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1971468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    /* The end of the environment block is marked by two NULL pointers */
1972468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    vecs++;
1973468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1974468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    while(vecs[0]) {
1975468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if (vecs[0] == AT_BASE) {
1976468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            return vecs[1];
1977468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        }
1978468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        vecs += 2;
1979468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    }
1980468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1981468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    return 0; // should never happen
1982468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
1983468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1984bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only
1985bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library
1986bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself.
1987bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *
1988bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input:
1989bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    elf    -> address of ELF header, assumed to be at the start of the file.
1990bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return:
1991bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    load bias, i.e. add the value of any p_vaddr in the file to get
1992bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    the corresponding address in memory.
1993bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */
1994bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turnerstatic Elf32_Addr
1995bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turnerget_elf_exec_load_bias(const Elf32_Ehdr* elf)
1996bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner{
1997bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    Elf32_Addr        offset     = elf->e_phoff;
1998bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
1999bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    const Elf32_Phdr* phdr_end   = phdr_table + elf->e_phnum;
2000bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    const Elf32_Phdr* phdr;
2001bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
2002bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    for (phdr = phdr_table; phdr < phdr_end; phdr++) {
2003bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner        if (phdr->p_type == PT_LOAD) {
2004bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner            return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
2005bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner        }
2006bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    }
2007bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    return 0;
2008bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner}
2009bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
2010468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
2011468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This
2012468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and
2013468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation().
2014468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich *
2015468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own
2016468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern
2017468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault.
2018468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
20194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesextern "C" unsigned __linker_init(unsigned **elfdata) {
2020468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    unsigned linker_addr = find_linker_base(elfdata);
2021468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
2022468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    Elf32_Phdr *phdr =
2023468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
2024468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2025468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    soinfo linker_so;
2026468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    memset(&linker_so, 0, sizeof(soinfo));
2027468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2028468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.base = linker_addr;
2029b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2030bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
2031468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.dynamic = (unsigned *) -1;
2032468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.phdr = phdr;
2033468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.phnum = elf_hdr->e_phnum;
2034468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.flags |= FLAG_LINKER;
2035468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
20365135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (soinfo_link_image(&linker_so)) {
2037468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // It would be nice to print an error message, but if the linker
2038468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // can't link itself, there's no guarantee that we'll be able to
2039468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // call write() (because it involves a GOT reference).
2040468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        //
2041468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // This situation should never occur unless the linker itself
2042468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // is corrupt.
204318a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
2044468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    }
2045468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2046468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    // We have successfully fixed our own relocations. It's safe to run
2047468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    // the main part of the linker now.
20485419b9474753d25dff947c7740532f86d130c0beElliott Hughes    unsigned start_address = __linker_init_post_relocation(elfdata, linker_addr);
20495419b9474753d25dff947c7740532f86d130c0beElliott Hughes
20505419b9474753d25dff947c7740532f86d130c0beElliott Hughes    // Return the address that the calling assembly stub should jump to.
20515419b9474753d25dff947c7740532f86d130c0beElliott Hughes    return start_address;
2052468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
2053