linker.cpp revision 06f0e74a2b957db93fed3fdd49fe939ab2fe1595
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>
461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker.h"
481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker_debug.h"
49be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner#include "linker_environ.h"
505c734644eebf8d01be1e86cbe20a111a5c5a2738David 'Digit' Turner#include "linker_format.h"
5123363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner#include "linker_phdr.h"
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
534a9afcb10151b083cd2d75253385615f459172edIliyan Malchev#define ALLOW_SYMBOLS_FROM_MAIN 1
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
8416084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerstatic int soinfo_link_image(soinfo *si, unsigned wr_offset);
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 Malchev#if ALLOW_SYMBOLS_FROM_MAIN
924a9afcb10151b083cd2d75253385615f459172edIliyan Malchevstatic soinfo *somain; /* main process, always the one after libdl_info */
934a9afcb10151b083cd2d75253385615f459172edIliyan Malchev#endif
941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
95af7315acf6a3a5ac329b04cb543b5d8a95dc26f1Iliyan Malchev
966ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevstatic inline int validate_soinfo(soinfo *si)
976ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev{
986ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    return (si >= sopool && si < sopool + SO_MAX) ||
996ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        si == &libdl_info;
1006ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev}
1016ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
102bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartleystatic char ldpaths_buf[LDPATH_BUFSIZE];
103bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartleystatic const char *ldpaths[LDPATH_MAX + 1];
104bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
1054fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischerstatic char ldpreloads_buf[LDPRELOAD_BUFSIZE];
1064fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischerstatic const char *ldpreload_names[LDPRELOAD_MAX + 1];
1074fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1084fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischerstatic soinfo *preloads[LDPRELOAD_MAX + 1];
1094fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1108c4f3ce8d084e3949d77ad8743593b5c83c3d41cNick Kralevich#if LINKER_DEBUG
1111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectint debug_verbosity;
1128c4f3ce8d084e3949d77ad8743593b5c83c3d41cNick Kralevich#endif
1138c4f3ce8d084e3949d77ad8743593b5c83c3d41cNick Kralevich
1141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int pid;
1151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
116be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner/* This boolean is set if the program being loaded is setuid */
117be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turnerstatic int program_is_setuid;
118be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
1191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
1201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstruct _link_stats linker_stats;
1211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
1241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectunsigned bitmask[4096];
1251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// You shouldn't try to call memory-allocating functions in the dynamic linker.
1284688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Guard against the most obvious ones.
1294688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#define DISALLOW_ALLOCATION(return_type, name, ...)                             \
1304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    return_type name __VA_ARGS__                                                \
1314688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    {                                                                           \
1324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
1334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes         __libc_android_log_write(ANDROID_LOG_FATAL, "linker", msg);            \
1344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        write(2, msg, sizeof(msg));                                             \
1354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        abort();                                                                \
1364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
1374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#define UNUSED __attribute__((unused))
1384688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
1394688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void, free, (void* u UNUSED));
1404688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
1414688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
1422e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1430353195f344666256dba474a15c9ba22cf0cccc9Dima Zavinstatic char tmp_err_buf[768];
1442e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavinstatic char __linker_dl_err_buf[768];
1454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#define BASENAME(s) (strrchr(s, '/') != NULL ? strrchr(s, '/') + 1 : s)
1462e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin#define DL_ERR(fmt, x...)                                                     \
1472e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    do {                                                                      \
148d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf),       \
1494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            "(%s:%d, pid %d) %s: " fmt,                                       \
1504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            BASENAME(__FILE__), __LINE__, pid, __func__, ##x);                \
151d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        ERROR(fmt "\n", ##x);                                                 \
1522e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    } while(0)
1532e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1542e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavinconst char *linker_get_error(void)
1552e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin{
1562e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    return (const char *)&__linker_dl_err_buf[0];
1572e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin}
1582e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
1601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This function is an empty stub where GDB locates a breakpoint to get notified
1611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about linker activity.
1621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesextern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity(void);
1641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic struct r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
1661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                                  RT_CONSISTENT, 0};
1671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic struct link_map *r_debug_tail = 0;
1681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1695e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchevstatic pthread_mutex_t _r_debug_lock = PTHREAD_MUTEX_INITIALIZER;
1701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void insert_soinfo_into_debug_map(soinfo * info)
1721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
1731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    struct link_map * map;
1741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Copy the necessary fields into the debug structure.
1761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
1771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map = &(info->linkmap);
1781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = info->base;
1791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_name = (char*) info->name;
1805cf640c926e7d7b79934c97226433a3dc448e1ebThinker K.F Li    map->l_ld = (uintptr_t)info->dynamic;
1811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Stick the new library at the end of the list.
1831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * gdb tends to care more about libc than it does
1841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * about leaf libraries, and ordering it this way
1851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * reduces the back-and-forth over the wire.
1861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
1871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (r_debug_tail) {
1881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        r_debug_tail->l_next = map;
1891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = r_debug_tail;
1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
1911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    } else {
1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        _r_debug.r_map = map;
1931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = 0;
1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
1951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
1971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
1981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1995e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchevstatic void remove_soinfo_from_debug_map(soinfo * info)
2005e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev{
2015e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    struct link_map * map = &(info->linkmap);
2025e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2035e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    if (r_debug_tail == map)
2045e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        r_debug_tail = map->l_prev;
2055e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2065e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    if (map->l_prev) map->l_prev->l_next = map->l_next;
2075e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    if (map->l_next) map->l_next->l_prev = map->l_prev;
2085e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2095e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectvoid notify_gdb_of_load(soinfo * info)
2111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
2121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (info->flags & FLAG_EXE) {
2131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        // GDB already knows about the main executable
2141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
2151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2175e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    pthread_mutex_lock(&_r_debug_lock);
2181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_ADD;
2201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(info);
2231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_CONSISTENT;
2251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2275e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    pthread_mutex_unlock(&_r_debug_lock);
2285e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2295e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2305e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchevvoid notify_gdb_of_unload(soinfo * info)
2315e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev{
2325e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    if (info->flags & FLAG_EXE) {
2335e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        // GDB already knows about the main executable
2345e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        return;
2355e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    }
2365e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2375e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    pthread_mutex_lock(&_r_debug_lock);
2385e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2395e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    _r_debug.r_state = RT_DELETE;
2405e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2415e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2425e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    remove_soinfo_from_debug_map(info);
2435e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2445e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    _r_debug.r_state = RT_CONSISTENT;
2455e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2465e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2475e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    pthread_mutex_unlock(&_r_debug_lock);
2481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesextern "C" void notify_gdb_of_libraries()
2511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
2521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_ADD;
2531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_CONSISTENT;
2551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
25816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerstatic soinfo *soinfo_alloc(const char *name)
2591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
2604688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (strlen(name) >= SOINFO_NAME_LEN) {
2614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("library name \"%s\" too long", name);
262943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan        return NULL;
2631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
26516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    /* The freelist is populated when we call soinfo_free(), which in turn is
2661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       done only by dlclose(), which is not likely to be used.
2671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    */
2681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (!freelist) {
2694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (socount == SO_MAX) {
2704688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("too many libraries when loading \"%s\"", name);
2711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return NULL;
2721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
2731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        freelist = sopool + socount++;
2741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        freelist->next = NULL;
2751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo* si = freelist;
2781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    freelist = freelist->next;
2791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Make sure we get a clean block of soinfo */
2811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    memset(si, 0, sizeof(soinfo));
282be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    strlcpy((char*) si->name, name, sizeof(si->name));
2831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    sonext->next = si;
2841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->next = NULL;
2851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->refcount = 0;
2861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    sonext = si;
2871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("%5d name %s: allocated soinfo @ %p\n", pid, name, si);
2891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si;
2901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2924688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void soinfo_free(soinfo* si)
2931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
2944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si == NULL) {
2954688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return;
2964688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
2974688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
2981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *prev = NULL, *trav;
2991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("%5d name %s: freeing soinfo @ %p\n", pid, si->name, si);
3011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(trav = solist; trav != NULL; trav = trav->next){
3031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (trav == si)
3041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
3051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        prev = trav;
3061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (trav == NULL) {
3081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* si was not ni solist */
3094688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("name \"%s\" is not in solist!", si->name);
3101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
3111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
313be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    /* prev will never be NULL, because the first entry in solist is
3141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       always the static libdl_info.
3151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    */
3161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    prev->next = si->next;
3171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si == sonext) sonext = prev;
3181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->next = freelist;
3191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    freelist = si;
3201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectconst char *addr_to_name(unsigned addr)
3231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    for (soinfo* si = solist; si != 0; si = si->next) {
3254688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if ((addr >= si->base) && (addr < (si->base + si->size))) {
3261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return si->name;
3271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
3281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return "";
3301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#ifdef ANDROID_ARM_LINKER
3334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* For a given PC, find the .so that it belongs to.
3351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the base address of the .ARM.exidx section
3361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for that .so, and the number of 8-byte entries
3371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in that section (via *pcount).
3381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Intended to be called by libc's __gnu_Unwind_Find_exidx().
3401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This function is exposed via dlfcn.c and libdl.so.
3421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
3431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
3441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
3461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned addr = (unsigned)pc;
3471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
348468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    for (si = solist; si != 0; si = si->next){
349468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if ((addr >= si->base) && (addr < (si->base + si->size))) {
350468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            *pcount = si->ARM_exidx_count;
351f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            return (_Unwind_Ptr)si->ARM_exidx;
3521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
3531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   *pcount = 0;
3551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return NULL;
3561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
358d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
3594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Here, we only have to provide a callback to iterate across all the
3611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * loaded libraries. gcc_eh does the rest. */
3621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectint
3631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectdl_iterate_phdr(int (*cb)(struct dl_phdr_info *info, size_t size, void *data),
3641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                void *data)
3651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
3671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    struct dl_phdr_info dl_info;
3681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int rv = 0;
3691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (si = solist; si != NULL; si = si->next) {
3711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_addr = si->linkmap.l_addr;
3721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_name = si->linkmap.l_name;
3731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phdr = si->phdr;
3741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phnum = si->phnum;
3751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        rv = cb(&dl_info, sizeof (struct dl_phdr_info), data);
3761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (rv != 0)
3771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
3781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return rv;
3801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3814688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
3831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
38416084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerstatic Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name)
3851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s;
3871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *symtab = si->symtab;
3881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const char *strtab = si->strtab;
3891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned n;
3901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE_TYPE(LOOKUP, "%5d SEARCH %s in %s@0x%08x %08x %d\n", pid,
3921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               name, si->name, si->base, hash, hash % si->nbucket);
3931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    n = hash % si->nbucket;
3941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
3961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        s = symtab + n;
3971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if(strcmp(strtab + s->st_name, name)) continue;
3981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
399e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            /* only concern ourselves with global and weak symbol definitions */
4001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(ELF32_ST_BIND(s->st_info)){
4011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case STB_GLOBAL:
402e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan        case STB_WEAK:
403439fa8ea8c33a7945258dfe84a71ab2665efaa9aRobin Burchell            if(s->st_shndx == SHN_UNDEF)
404439fa8ea8c33a7945258dfe84a71ab2665efaa9aRobin Burchell                continue;
4051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(LOOKUP, "%5d FOUND %s in %s (%08x) %d\n", pid,
4071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       name, si->name, s->st_value, s->st_size);
4081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return s;
4091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
4101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
412943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
4131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned elfhash(const char *_name)
4161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const unsigned char *name = (const unsigned char *) _name;
4181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned h = 0, g;
4191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    while(*name) {
4211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h = (h << 4) + *name++;
4221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        g = h & 0xf0000000;
4231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g;
4241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g >> 24;
4251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return h;
4271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic Elf32_Sym *
430bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turnersoinfo_do_lookup(soinfo *si, const char *name, Elf32_Addr *offset)
4316ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev{
432943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
4336ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    Elf32_Sym *s;
4346ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    unsigned *d;
4356ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    soinfo *lsi = si;
4364fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    int i;
4376ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
438468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    /* Look for symbols in the local scope (the object who is
4396ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev     * searching). This happens with C++ templates on i386 for some
440e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan     * reason.
441e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan     *
442e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan     * Notes on weak symbols:
443e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan     * The ELF specs are ambigious about treatment of weak definitions in
444e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan     * dynamic linking.  Some systems return the first definition found
445e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan     * and some the first non-weak definition.   This is system dependent.
446e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan     * Here we return the first definition found for simplicity.  */
447468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
44816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    s = soinfo_elf_lookup(si, elf_hash, name);
4496ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    if(s != NULL)
4506ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        goto done;
4516ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
4524fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* Next, look for it in the preloads list */
4534fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    for(i = 0; preloads[i] != NULL; i++) {
4544fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        lsi = preloads[i];
45516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner        s = soinfo_elf_lookup(lsi, elf_hash, name);
4564fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        if(s != NULL)
4574fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            goto done;
4584fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
4594fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
4606ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    for(d = si->dynamic; *d; d += 2) {
4616ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        if(d[0] == DT_NEEDED){
4626ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev            lsi = (soinfo *)d[1];
4636ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev            if (!validate_soinfo(lsi)) {
4644688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("bad DT_NEEDED pointer in \"%s\"", lsi->name);
465943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan                return NULL;
4666ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev            }
4676ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
4686ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev            DEBUG("%5d %s: looking up %s in %s\n",
4696ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev                  pid, si->name, name, lsi->name);
47016084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner            s = soinfo_elf_lookup(lsi, elf_hash, name);
4718211bc6325ef61f2fe2021fac9e0f4219abaccf2Robin Burchell            if (s != NULL)
4726ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev                goto done;
4736ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        }
4746ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
4756ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
4764a9afcb10151b083cd2d75253385615f459172edIliyan Malchev#if ALLOW_SYMBOLS_FROM_MAIN
4774a9afcb10151b083cd2d75253385615f459172edIliyan Malchev    /* If we are resolving relocations while dlopen()ing a library, it's OK for
4784a9afcb10151b083cd2d75253385615f459172edIliyan Malchev     * the library to resolve a symbol that's defined in the executable itself,
4794a9afcb10151b083cd2d75253385615f459172edIliyan Malchev     * although this is rare and is generally a bad idea.
4804a9afcb10151b083cd2d75253385615f459172edIliyan Malchev     */
4814a9afcb10151b083cd2d75253385615f459172edIliyan Malchev    if (somain) {
4824a9afcb10151b083cd2d75253385615f459172edIliyan Malchev        lsi = somain;
4834a9afcb10151b083cd2d75253385615f459172edIliyan Malchev        DEBUG("%5d %s: looking up %s in executable %s\n",
4844a9afcb10151b083cd2d75253385615f459172edIliyan Malchev              pid, si->name, name, lsi->name);
48516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner        s = soinfo_elf_lookup(lsi, elf_hash, name);
4864a9afcb10151b083cd2d75253385615f459172edIliyan Malchev    }
4874a9afcb10151b083cd2d75253385615f459172edIliyan Malchev#endif
4884a9afcb10151b083cd2d75253385615f459172edIliyan Malchev
4896ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevdone:
4906ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    if(s != NULL) {
4916ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        TRACE_TYPE(LOOKUP, "%5d si %s sym %s s->st_value = 0x%08x, "
492bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner                   "found in %s, base = 0x%08x, load bias = 0x%08x\n",
493f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee                   pid, si->name, name, s->st_value,
494bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner                   lsi->name, lsi->base, lsi->load_bias);
495bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner        *offset = lsi->load_bias;
4966ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        return s;
4976ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
4986ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
499943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
5006ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev}
5016ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5026ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev/* This is used by dl_sym().  It performs symbol lookup only within the
5036ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev   specified soinfo object and not in any of its dependencies.
5046ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
50516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' TurnerElf32_Sym *soinfo_lookup(soinfo *si, const char *name)
5061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
50716084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    return soinfo_elf_lookup(si, elfhash(name), name);
5081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5106ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev/* This is used by dl_sym().  It performs a global symbol lookup.
5116ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
5121698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt FischerElf32_Sym *lookup(const char *name, soinfo **found, soinfo *start)
5131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
514943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
5151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s = NULL;
5161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
5171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5181698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer    if(start == NULL) {
5191698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer        start = solist;
5201698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer    }
5211698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer
5221698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer    for(si = start; (s == NULL) && (si != NULL); si = si->next)
5231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
5246ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        if(si->flags & FLAG_ERROR)
5251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
52616084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner        s = soinfo_elf_lookup(si, elf_hash, name);
5271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (s != NULL) {
5289ea64da6c511e8f9f4edae4c10c20879957631abIliyan Malchev            *found = si;
5291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
5301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
5311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
5321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5336ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    if(s != NULL) {
5341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE_TYPE(LOOKUP, "%5d %s s->st_value = 0x%08x, "
5351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                   "si->base = 0x%08x\n", pid, name, s->st_value, si->base);
5361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return s;
5371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
5381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
539943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
5401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
542bda5da074eab4bdf374e1f4a19d480c62c72f5ffMathias Agopiansoinfo *find_containing_library(const void *addr)
543e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer{
544e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    soinfo *si;
545e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
546e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    for(si = solist; si != NULL; si = si->next)
547e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    {
548e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
549e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer            return si;
550e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        }
551e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
552e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
553e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    return NULL;
554e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
555e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
55616084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' TurnerElf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr)
557e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer{
558e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    unsigned int i;
559e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    unsigned soaddr = (unsigned)addr - si->base;
560e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
561e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    /* Search the library's symbol table for any defined symbol which
562e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer     * contains this address */
563e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    for(i=0; i<si->nchain; i++) {
564e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        Elf32_Sym *sym = &si->symtab[i];
565e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
566e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        if(sym->st_shndx != SHN_UNDEF &&
567e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer           soaddr >= sym->st_value &&
568e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer           soaddr < sym->st_value + sym->st_size) {
569e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer            return sym;
570e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        }
571e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
572e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
573e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    return NULL;
574e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
575e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
5761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0
5771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void dump(soinfo *si)
5781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
5791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s = si->symtab;
5801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned n;
5811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(n = 0; n < si->nchain; n++) {
5831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("%5d %04d> %08x: %02x %04x %08x %08x %s\n", pid, n, s,
5841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               s->st_info, s->st_shndx, s->st_value, s->st_size,
5851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               si->strtab + s->st_name);
5861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        s++;
5871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
5881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
5901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
59116084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerstatic const char * const sopaths[] = {
592fedbcde6ef552e84bf7ce7598bca7dddf1722d6aBrian Swetland    "/vendor/lib",
5931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    "/system/lib",
5941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    0
5951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project};
5961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int _open_lib(const char *name)
5981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
5991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int fd;
6001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    struct stat filestat;
6011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if ((stat(name, &filestat) >= 0) && S_ISREG(filestat.st_mode)) {
60316084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner        if ((fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY))) >= 0)
6041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return fd;
6051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return -1;
6081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int open_library(const char *name)
6111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
6121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int fd;
6131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    char buf[512];
61416084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    const char * const*path;
615bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    int n;
6161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("[ %5d opening %s ]\n", pid, name);
6181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if(name == 0) return -1;
6201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if(strlen(name) > 256) return -1;
6211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if ((name[0] == '/') && ((fd = _open_lib(name)) >= 0))
6231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return fd;
6241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
625bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    for (path = ldpaths; *path; path++) {
6265c734644eebf8d01be1e86cbe20a111a5c5a2738David 'Digit' Turner        n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
627bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley        if (n < 0 || n >= (int)sizeof(buf)) {
628bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley            WARN("Ignoring very long library path: %s/%s\n", *path, name);
629bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley            continue;
630bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley        }
631bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley        if ((fd = _open_lib(buf)) >= 0)
632bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley            return fd;
633bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    }
6341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (path = sopaths; *path; path++) {
6355c734644eebf8d01be1e86cbe20a111a5c5a2738David 'Digit' Turner        n = format_buffer(buf, sizeof(buf), "%s/%s", *path, name);
636bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley        if (n < 0 || n >= (int)sizeof(buf)) {
637bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley            WARN("Ignoring very long library path: %s/%s\n", *path, name);
638bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley            continue;
639bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley        }
6401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if ((fd = _open_lib(buf)) >= 0)
6411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return fd;
6421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return -1;
6451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Returns 'true' if the library is prelinked or on failure so we error out
6484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// either way. We no longer support prelinking.
6494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic bool is_prelinked(int fd, const char* name)
6501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
6514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    struct prelink_info_t {
6524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        long mmap_addr;
6534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        char tag[4]; // "PRE ".
6544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    };
6554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
6564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    off_t sz = lseek(fd, -sizeof(struct prelink_info_t), SEEK_END);
6571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (sz < 0) {
6584688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("lseek failed: %s", strerror(errno));
6594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return true;
6601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    struct prelink_info_t info;
6638dfc073b1487ab25ab483ab346cee1d9e584adb1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
6648dfc073b1487ab25ab483ab346cee1d9e584adb1Elliott Hughes    if (rc != sizeof(info)) {
6654688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("could not read prelink_info_t structure for \"%s\":", name, strerror(errno));
6664688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return true;
6671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (memcmp(info.tag, "PRE ", 4) == 0) {
6704688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("prelinked libraries no longer supported: %s", name);
6714688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return true;
6721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6734688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    return false;
6741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
67616084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner/* verify_elf_header
67716084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner *      Verifies the content of an ELF header.
6781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
6791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Args:
6801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
6811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns:
6821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *       0 on success
6831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *      -1 if no valid ELF object is found @ base.
6841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
6851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
68616084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerverify_elf_header(const Elf32_Ehdr* hdr)
6871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
6881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
6891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
6901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
6911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
6921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* TODO: Should we verify anything else in the header? */
694897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#ifdef ANDROID_ARM_LINKER
695897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang    if (hdr->e_machine != EM_ARM) return -1;
696897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#elif defined(ANDROID_X86_LINKER)
697897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang    if (hdr->e_machine != EM_386) return -1;
698d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_MIPS_LINKER)
699d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if (hdr->e_machine != EM_MIPS) return -1;
700897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#endif
7011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
7021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7044688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct scoped_fd {
7054688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ~scoped_fd() {
7064688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (fd != -1) {
7074688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            close(fd);
7084688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
7094688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7104688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    int fd;
7114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes};
7121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7134688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct soinfo_ptr {
7144688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo_ptr(const char* name) {
7154688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        const char* bname = strrchr(name, '/');
7164688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        ptr = soinfo_alloc(bname ? bname + 1 : name);
7174688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7184688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ~soinfo_ptr() {
7194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        soinfo_free(ptr);
7204688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7214688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo* release() {
7224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        soinfo* result = ptr;
7234688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        ptr = NULL;
7244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return result;
7254688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo* ptr;
7274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes};
7281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7294688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// TODO: rewrite linker_phdr.h to use a class, then lose this.
7304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct phdr_ptr {
7314688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    phdr_ptr() : phdr_mmap(NULL) {}
7324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ~phdr_ptr() {
7334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (phdr_mmap != NULL) {
7344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            phdr_table_unload(phdr_mmap, phdr_size);
7354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
7364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    void* phdr_mmap;
7384688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr phdr_size;
7394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes};
740b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner
7414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic soinfo* load_library(const char* name)
7424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes{
7434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Open the file.
7444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    scoped_fd fd;
7454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    fd.fd = open_library(name);
7464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (fd.fd == -1) {
7474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("library \"%s\" not found", name);
7481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
7492e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    }
7501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Read the ELF header.
7524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Ehdr header[1];
7534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header)));
75423363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ret < 0) {
7554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't read file \"%s\": %s", name, strerror(errno));
7564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
75723363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
75823363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ret != (int)sizeof(header)) {
7594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("too small to be an ELF executable: %s", name);
7604688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
76123363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
76223363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (verify_elf_header(header) < 0) {
7634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("not a valid ELF executable: %s", name);
7644688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
7651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Read the program header table.
7684688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    const Elf32_Phdr* phdr_table;
7694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    phdr_ptr phdr_holder;
7704688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ret = phdr_table_load(fd.fd, header->e_phoff, header->e_phnum,
7714688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                          &phdr_holder.phdr_mmap, &phdr_holder.phdr_size, &phdr_table);
77223363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ret < 0) {
7734688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't load program header table: %s: %s", name, strerror(errno));
7744688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
7751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7764688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    size_t phdr_count = header->e_phnum;
7771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7784688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Get the load extents.
7794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
7804688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    TRACE("[ %5d - '%s' wants sz=0x%08x ]\n", pid, name, ext_sz);
78123363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ext_sz == 0) {
7824688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("no loadable segments in file: %s", name);
7834688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
78423363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
78523363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner
7864688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // We no longer support pre-linked libraries.
7874688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (is_prelinked(fd.fd, name)) {
7884688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
78923363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
79016084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner
7914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Reserve address space for all loadable segments.
7924688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    void* load_start = NULL;
7934688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr load_size = 0;
7944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr load_bias = 0;
795b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    ret = phdr_table_reserve_memory(phdr_table,
796b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    phdr_count,
797b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    &load_start,
798b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    &load_size,
799b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    &load_bias);
800b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (ret < 0) {
8014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't reserve %d bytes in address space for \"%s\": %s",
8024688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               ext_sz, name, strerror(errno));
8034688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
804b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
8051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("[ %5d allocated memory for %s @ %p (0x%08x) ]\n",
807b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner          pid, name, load_start, load_size);
808b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner
809b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* Map all the segments in our address space with default protections */
810b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    ret = phdr_table_load_segments(phdr_table,
811b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                   phdr_count,
812b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                   load_bias,
8134688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                                   fd.fd);
814b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (ret < 0) {
8154688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't map loadable segments for \"%s\": %s",
8164688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               name, strerror(errno));
8174688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
818b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
8191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
820b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* Unprotect the segments, i.e. make them writable, to allow
821b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner     * relocations to work properly. We will later call
822b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner     * phdr_table_protect_segments() after all of them are applied
823b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner     * and all constructors are run.
824b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner     */
825b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    ret = phdr_table_unprotect_segments(phdr_table,
826b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                        phdr_count,
827b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                        load_bias);
828b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (ret < 0) {
8294688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't unprotect loadable segments for \"%s\": %s",
8304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               name, strerror(errno));
8314688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
832b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
833b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner
8344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo_ptr si(name);
8354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si.ptr == NULL) {
8364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
8371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->base = (Elf32_Addr) load_start;
8404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->size = load_size;
8414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->load_bias = load_bias;
8424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->flags = 0;
8434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->entry = 0;
8444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->dynamic = (unsigned *)-1;
8454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->phnum = phdr_count;
8464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
8474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si.ptr->phdr == NULL) {
8484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't find loaded PHDR for \"%s\"", name);
8494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
85023363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
8514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
8524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    return si.release();
8531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic soinfo *
8561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectinit_library(soinfo *si)
8571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
8581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned wr_offset = 0xffffffff;
8591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* At this point we know that whatever is loaded @ base is a valid ELF
8611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * shared library whose segments are properly mapped in. */
8621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("[ %5d init_library base=0x%08x sz=0x%08x name='%s') ]\n",
8631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project          pid, si->base, si->size, si->name);
8641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
86516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    if(soinfo_link_image(si, wr_offset)) {
8661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            /* We failed to link.  However, we can only restore libbase
8671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            ** if no additional libraries have moved it since we updated it.
8681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            */
8691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        munmap((void *)si->base, si->size);
8701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
8711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si;
8741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectsoinfo *find_library(const char *name)
8771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
8781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
8796774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    const char *bname;
8806774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
8816774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner#if ALLOW_SYMBOLS_FROM_MAIN
8826774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    if (name == NULL)
8836774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner        return somain;
8846774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner#else
8856774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    if (name == NULL)
8866774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner        return NULL;
8876774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner#endif
8886774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
8896774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    bname = strrchr(name, '/');
890fde8642fc43bdd224e43e5ee9583a49a758fb03cErik Gilling    bname = bname ? bname + 1 : name;
8911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(si = solist; si != 0; si = si->next){
893fde8642fc43bdd224e43e5ee9583a49a758fb03cErik Gilling        if(!strcmp(bname, si->name)) {
89430eb40241c612a7ddbd21c4daa4e7bbfd9d5e89fErik Gilling            if(si->flags & FLAG_ERROR) {
8954688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("\"%s\" failed to load previously", bname);
89630eb40241c612a7ddbd21c4daa4e7bbfd9d5e89fErik Gilling                return NULL;
89730eb40241c612a7ddbd21c4daa4e7bbfd9d5e89fErik Gilling            }
8981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if(si->flags & FLAG_LINKED) return si;
8994688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("OOPS: recursive link to \"%s\"", si->name);
9002e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin            return NULL;
9011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
9021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
9031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("[ %5d '%s' has not been loaded yet.  Locating...]\n", pid, name);
9051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si = load_library(name);
9061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if(si == NULL)
9071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
9081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return init_library(si);
9091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
9101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
911be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner/* TODO:
9124688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes *   find a way to decrement libbase
9131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
9141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void call_destructors(soinfo *si);
91516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerunsigned soinfo_unload(soinfo *si)
9161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
9171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned *d;
9181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si->refcount == 1) {
9191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("%5d unloading '%s'\n", pid, si->name);
9201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        call_destructors(si);
9211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9229ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich        /*
9239ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich         * Make sure that we undo the PT_GNU_RELRO protections we added
92416084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner         * in soinfo_link_image. This is needed to undo the DT_NEEDED hack below.
9259ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich         */
926b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (phdr_table_unprotect_gnu_relro(si->phdr, si->phnum,
927b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                           si->load_bias) < 0) {
9284688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("%s: could not undo GNU_RELRO protections. "
929b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                    "Expect a crash soon. errno=%d (%s)",
9304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    si->name, errno, strerror(errno));
9319ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich        }
9329ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
9331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        for(d = si->dynamic; *d; d += 2) {
9341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if(d[0] == DT_NEEDED){
9356ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev                soinfo *lsi = (soinfo *)d[1];
9369ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
9379ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich                // The next line will segfault if the we don't undo the
9389ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich                // PT_GNU_RELRO protections (see comments above and in
93916084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner                // soinfo_link_image().
9406ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev                d[1] = 0;
9419ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
9426ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev                if (validate_soinfo(lsi)) {
9436ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev                    TRACE("%5d %s needs to unload %s\n", pid,
9446ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev                          si->name, lsi->name);
94516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner                    soinfo_unload(lsi);
9466ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev                }
9471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                else
9484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("\"%s\": could not unload dependent library",
9494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                           si->name);
9501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
9511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
9521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        munmap((char *)si->base, si->size);
9545e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        notify_gdb_of_unload(si);
95516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner        soinfo_free(si);
9561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        si->refcount = 0;
9571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
9581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    else {
9591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        si->refcount--;
9601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        PRINT("%5d not unloading '%s', decrementing refcount to %d\n",
9611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              pid, si->name, si->refcount);
9621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
9631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si->refcount;
9641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
9651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* TODO: don't use unsigned for addrs below. It works, but is not
9671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
9681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * long.
9691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
97016084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerstatic int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count)
9711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
9721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *symtab = si->symtab;
9731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const char *strtab = si->strtab;
9741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s;
975f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee    Elf32_Addr offset;
9761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Rel *start = rel;
9771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9784688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    for (size_t idx = 0; idx < count; ++idx, ++rel) {
9791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned type = ELF32_R_TYPE(rel->r_info);
9801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned sym = ELF32_R_SYM(rel->r_info);
981bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner        unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
9821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned sym_addr = 0;
9831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        char *sym_name = NULL;
9841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        DEBUG("%5d Processing '%s' relocation at index %d\n", pid,
9861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              si->name, idx);
987d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (type == 0) { // R_*_NONE
988d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            continue;
989d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
9901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if(sym != 0) {
991d1b40d8c69dfca94185ea9231f6cb16f70c02e39Dima Zavin            sym_name = (char *)(strtab + symtab[sym].st_name);
992bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner            s = soinfo_do_lookup(si, sym_name, &offset);
993e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            if(s == NULL) {
994e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* We only allow an undefined symbol if this is a weak
995e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   reference..   */
996e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                s = &symtab[sym];
997e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
9984688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("cannot locate \"%s\"...", sym_name);
999e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1000e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1001e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1002e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* IHI0044C AAELF 4.5.1.1:
1003e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1004e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   Libraries are not searched to resolve weak references.
1005e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   It is not an error for a weak reference to remain
1006e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   unsatisfied.
1007e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1008e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   During linking, the value of an undefined weak reference is:
1009e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - Zero if the relocation type is absolute
1010e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - The address of the place if the relocation is pc-relative
1011e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - The address of nominial base address if the relocation
1012e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                     type is base-relative.
1013e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                  */
1014e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1015e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                switch (type) {
1016e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_ARM_LINKER)
1017e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_JUMP_SLOT:
1018e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_GLOB_DAT:
1019e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_ABS32:
1020e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_RELATIVE:    /* Don't care. */
1021e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#elif defined(ANDROID_X86_LINKER)
1022d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                case R_386_JMP_SLOT:
1023e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_GLOB_DAT:
1024e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_32:
1025e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_RELATIVE:    /* Dont' care. */
1026e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_*_LINKER */
1027e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    /* sym_addr was initialized to be zero above or relocation
1028e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       code below does not care about value of sym_addr.
1029e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       No need to do anything.  */
1030e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
1031e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1032e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_X86_LINKER)
1033e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_PC32:
1034e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    sym_addr = reloc;
1035e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
1036e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_X86_LINKER */
1037e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1038e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_ARM_LINKER)
1039e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_COPY:
1040e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    /* Fall through.  Can't really copy if weak symbol is
1041e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       not found in run-time.  */
1042e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_ARM_LINKER */
1043e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                default:
10444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("unknown weak reloc type %d @ %p (%d)",
10454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                                 type, rel, (int) (rel - start));
1046e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1047e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1048e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            } else {
1049e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* We got a definition.  */
10501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0
1051d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                if((base == 0) && (si->base != 0)){
1052d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                        /* linking from libraries to main image is bad */
10534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("cannot locate \"%s\"...",
10544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                           strtab + symtab[sym].st_name);
1055d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                    return -1;
1056d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                }
10571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1058bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner                sym_addr = (unsigned)(s->st_value + offset);
1059d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
10601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            COUNT_RELOC(RELOC_SYMBOL);
10611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        } else {
1062e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            s = NULL;
10631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
10641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
10651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* TODO: This is ugly. Split up the relocations by arch into
10661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * different files.
10671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
10681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(type){
10691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_ARM_LINKER)
10701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_JUMP_SLOT:
10711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            COUNT_RELOC(RELOC_ABSOLUTE);
10721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
10731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
10741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, sym_addr, sym_name);
10751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
10761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
10771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_GLOB_DAT:
10781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            COUNT_RELOC(RELOC_ABSOLUTE);
10791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
10801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
10811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, sym_addr, sym_name);
10821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
10831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
10841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_ABS32:
10851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            COUNT_RELOC(RELOC_ABSOLUTE);
10861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
10871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO ABS %08x <- %08x %s\n", pid,
10881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, sym_addr, sym_name);
10891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) += sym_addr;
10901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
109134ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner        case R_ARM_REL32:
109234ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            COUNT_RELOC(RELOC_RELATIVE);
109334ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            MARK(rel->r_offset);
109434ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x - %08x %s\n", pid,
109534ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner                       reloc, sym_addr, rel->r_offset, sym_name);
109634ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            *((unsigned*)reloc) += sym_addr - rel->r_offset;
109734ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            break;
10981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#elif defined(ANDROID_X86_LINKER)
1099d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case R_386_JMP_SLOT:
11001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            COUNT_RELOC(RELOC_ABSOLUTE);
11011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
11021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
11031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, sym_addr, sym_name);
11041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
11051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_GLOB_DAT:
11071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            COUNT_RELOC(RELOC_ABSOLUTE);
11081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
11091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO GLOB_DAT %08x <- %08x %s\n", pid,
11101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, sym_addr, sym_name);
11111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
11121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1113d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_MIPS_LINKER)
1114d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    case R_MIPS_JUMP_SLOT:
1115d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            COUNT_RELOC(RELOC_ABSOLUTE);
1116d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            MARK(rel->r_offset);
1117d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            TRACE_TYPE(RELO, "%5d RELO JMP_SLOT %08x <- %08x %s\n", pid,
1118d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                       reloc, sym_addr, sym_name);
1119d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *((unsigned*)reloc) = sym_addr;
1120d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1121d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    case R_MIPS_REL32:
1122d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            COUNT_RELOC(RELOC_ABSOLUTE);
1123d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            MARK(rel->r_offset);
1124d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            TRACE_TYPE(RELO, "%5d RELO REL32 %08x <- %08x %s\n", pid,
1125d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                       reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1126d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            if (s) {
1127d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                *((unsigned*)reloc) += sym_addr;
1128d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            } else {
1129d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                *((unsigned*)reloc) += si->base;
1130d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1131d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
11321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_*_LINKER */
11331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_ARM_LINKER)
11351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_RELATIVE:
11361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#elif defined(ANDROID_X86_LINKER)
11371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_RELATIVE:
11381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_*_LINKER */
11391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            COUNT_RELOC(RELOC_RELATIVE);
11401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
11414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            if (sym) {
11424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("odd RELATIVE form...", pid);
11431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                return -1;
11441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
11451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO RELATIVE %08x <- +%08x\n", pid,
11461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, si->base);
11471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) += si->base;
11481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_X86_LINKER)
11511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_32:
11521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            COUNT_RELOC(RELOC_RELATIVE);
11531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
11541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO R_386_32 %08x <- +%08x %s\n", pid,
11561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, sym_addr, sym_name);
11571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned *)reloc) += (unsigned)sym_addr;
11581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_PC32:
11611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            COUNT_RELOC(RELOC_RELATIVE);
11621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
11631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO R_386_PC32 %08x <- "
11641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       "+%08x (%08x - %08x) %s\n", pid, reloc,
11651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       (sym_addr - reloc), sym_addr, reloc, sym_name);
11661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
11671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_X86_LINKER */
11691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef ANDROID_ARM_LINKER
11711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_COPY:
11721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            COUNT_RELOC(RELOC_COPY);
11731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
11741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            TRACE_TYPE(RELO, "%5d RELO %08x <- %d @ %08x %s\n", pid,
11751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       reloc, s->st_size, sym_addr, sym_name);
11761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            memcpy((void*)reloc, (void*)sym_addr, s->st_size);
11771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_ARM_LINKER */
11791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        default:
11814688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("unknown reloc type %d @ %p (%d)",
11824688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                   type, rel, (int) (rel - start));
11831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return -1;
11841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
11851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
11861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
11871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
11881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1189d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#ifdef ANDROID_MIPS_LINKER
1190d7daacb46372132ae3f0121647074936c304b572Raghu Gandhamint mips_relocate_got(struct soinfo *si)
1191d7daacb46372132ae3f0121647074936c304b572Raghu Gandham{
1192d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned *got;
1193d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned local_gotno, gotsym, symtabno;
1194d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    Elf32_Sym *symtab, *sym;
1195d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned g;
1196d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1197d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got;
1198d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    local_gotno = si->mips_local_gotno;
1199d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    gotsym = si->mips_gotsym;
1200d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    symtabno = si->mips_symtabno;
1201d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    symtab = si->symtab;
1202d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1203d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    /*
1204d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * got[0] is address of lazy resolver function
1205d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * got[1] may be used for a GNU extension
1206d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * set it to a recognisable address in case someone calls it
1207d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * (should be _rtld_bind_start)
1208d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * FIXME: maybe this should be in a separate routine
1209d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     */
1210d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1211d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if ((si->flags & FLAG_LINKER) == 0) {
1212d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        g = 0;
1213d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        got[g++] = 0xdeadbeef;
1214d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (got[g] & 0x80000000) {
1215d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            got[g++] = 0xdeadfeed;
1216d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1217d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        /*
1218d7daacb46372132ae3f0121647074936c304b572Raghu Gandham         * Relocate the local GOT entries need to be relocated
1219d7daacb46372132ae3f0121647074936c304b572Raghu Gandham         */
1220d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        for (; g < local_gotno; g++) {
1221d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            got[g] += si->load_bias;
1222d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1223d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1224d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1225d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    /* Now for the global GOT entries */
1226d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    sym = symtab + gotsym;
1227d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got + local_gotno;
1228d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    for (g = gotsym; g < symtabno; g++, sym++, got++) {
1229d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        const char *sym_name;
1230d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        unsigned base;
1231d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        Elf32_Sym *s;
1232d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1233d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        /* This is an undefined reference... try to locate it */
1234d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        sym_name = si->strtab + sym->st_name;
1235d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        s = soinfo_do_lookup(si, sym_name, &base);
1236d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (s == NULL) {
1237d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* We only allow an undefined symbol if this is a weak
1238d7daacb46372132ae3f0121647074936c304b572Raghu Gandham               reference..   */
1239d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            s = &symtab[g];
1240d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
12414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("cannot locate \"%s\"...", sym_name);
1242d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                return -1;
1243d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1244d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *got = 0;
1245d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1246d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        else {
1247d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* FIXME: is this sufficient?
1248d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             * For reference see NetBSD link loader
1249d7daacb46372132ae3f0121647074936c304b572Raghu 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
1250d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             */
1251d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             *got = base + s->st_value;
1252d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1253d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1254d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    return 0;
1255d7daacb46372132ae3f0121647074936c304b572Raghu Gandham}
1256d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1257d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
12588215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner/* Please read the "Initialization and Termination functions" functions.
12598215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * of the linker design note in bionic/linker/README.TXT to understand
12608215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * what the following code is doing.
12618215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
12628215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * The important things to remember are:
12638215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
12648215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_PREINIT_ARRAY must be called first for executables, and should
12658215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   not appear in shared libraries.
12668215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
12678215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_INIT should be called before DT_INIT_ARRAY if both are present
12688215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
12698215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_FINI should be called after DT_FINI_ARRAY if both are present
12708215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
12718215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_FINI_ARRAY must be parsed in reverse order.
12728215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner */
12738215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
12748215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turnerstatic void call_array(unsigned *ctor, int count, int reverse)
12751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
12768215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner    int n, inc = 1;
12778215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
12788215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner    if (reverse) {
12798215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        ctor += (count-1);
12808215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        inc   = -1;
12818215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner    }
12828215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
12838215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner    for(n = count; n > 0; n--) {
12848215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        TRACE("[ %5d Looking at %s *0x%08x == 0x%08x ]\n", pid,
12858215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner              reverse ? "dtor" : "ctor",
12861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              (unsigned)ctor, (unsigned)*ctor);
12878215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        void (*func)() = (void (*)()) *ctor;
12888215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        ctor += inc;
12891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if(((int) func == 0) || ((int) func == -1)) continue;
12901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Calling func @ 0x%08x ]\n", pid, (unsigned)func);
12911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        func();
12921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
12931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
12941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
129516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnervoid soinfo_call_constructors(soinfo *si)
12961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
1297e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov    if (si->constructors_called)
1298e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov        return;
1299e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1300f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    // Set this before actually calling the constructors, otherwise it doesn't
1301f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    // protect against recursive constructor calls. One simple example of
1302f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    // constructor recursion is the libc debug malloc, which is implemented in
1303f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    // libc_malloc_debug_leak.so:
1304f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    // 1. The program depends on libc, so libc's constructor is called here.
1305f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
130616084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    // 3. dlopen() calls soinfo_call_constructors() with the newly created
1307f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    //    soinfo for libc_malloc_debug_leak.so.
130816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    // 4. The debug so depends on libc, so soinfo_call_constructors() is
1309f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    //    called again with the libc soinfo. If it doesn't trigger the early-
1310f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    //    out above, the libc constructor will be called again (recursively!).
1311f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall    si->constructors_called = 1;
1312f5d1693e3ca8cfd089e6259f320044833eae2991Jesse Hall
13131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si->flags & FLAG_EXE) {
13141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Calling preinit_array @ 0x%08x [%d] for '%s' ]\n",
13151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              pid, (unsigned)si->preinit_array, si->preinit_array_count,
13161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              si->name);
13178215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        call_array(si->preinit_array, si->preinit_array_count, 0);
13181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Done calling preinit_array for '%s' ]\n", pid, si->name);
13191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    } else {
13201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (si->preinit_array) {
13214688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("shared library \"%s\" has a preinit_array table @ 0x%08x. "
13224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                   "This is INVALID.", si->name, (unsigned) si->preinit_array);
13231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
13241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1326e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov    if (si->dynamic) {
1327e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov        unsigned *d;
1328e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov        for(d = si->dynamic; *d; d += 2) {
1329e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov            if(d[0] == DT_NEEDED){
1330e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov                soinfo* lsi = (soinfo *)d[1];
1331e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov                if (!validate_soinfo(lsi)) {
13324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("bad DT_NEEDED pointer in \"%s\"", si->name);
1333e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov                } else {
133416084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner                    soinfo_call_constructors(lsi);
1335e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov                }
1336e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov            }
1337e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov        }
1338e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov    }
1339e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
13401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si->init_func) {
13411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Calling init_func @ 0x%08x for '%s' ]\n", pid,
13421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              (unsigned)si->init_func, si->name);
13431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        si->init_func();
13441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Done calling init_func for '%s' ]\n", pid, si->name);
13451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si->init_array) {
13481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Calling init_array @ 0x%08x [%d] for '%s' ]\n", pid,
13491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              (unsigned)si->init_array, si->init_array_count, si->name);
13508215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        call_array(si->init_array, si->init_array_count, 0);
13511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Done calling init_array for '%s' ]\n", pid, si->name);
13521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1354e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov}
13558215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
13561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void call_destructors(soinfo *si)
13571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
13581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si->fini_array) {
13591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Calling fini_array @ 0x%08x [%d] for '%s' ]\n", pid,
13601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              (unsigned)si->fini_array, si->fini_array_count, si->name);
13618215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner        call_array(si->fini_array, si->fini_array_count, 1);
13621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Done calling fini_array for '%s' ]\n", pid, si->name);
13631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si->fini_func) {
13661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Calling fini_func @ 0x%08x for '%s' ]\n", pid,
13671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project              (unsigned)si->fini_func, si->name);
13681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        si->fini_func();
13691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Done calling fini_func for '%s' ]\n", pid, si->name);
13701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
13721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with
13741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   /dev/null. */
13751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int nullify_closed_stdio (void)
13761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
13771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int dev_null, i, status;
13781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int return_value = 0;
13791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
138016084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
13811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null < 0) {
13824688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("cannot open /dev/null: %s", strerror(errno));
13831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return -1;
13841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("[ %5d Opened /dev/null file-descriptor=%d]\n", pid, dev_null);
13861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If any of the stdio file descriptors is valid and not associated
13881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       with /dev/null, dup /dev/null to it.  */
13891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (i = 0; i < 3; i++) {
13901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* If it is /dev/null already, we are done. */
13914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (i == dev_null) {
13921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
13934688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
13941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Nullifying stdio file descriptor %d]\n", pid, i);
13964688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
13971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13984688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        /* If file is opened, we are good. */
13994688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status != -1) {
14004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            continue;
14014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
14021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* The only error we allow is that the file descriptor does not
14041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           exist, in which case we dup /dev/null to it. */
14051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (errno != EBADF) {
14064688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("fcntl failed: %s", strerror(errno));
14071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
14091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* Try dupping /dev/null to this stdio file descriptor and
14121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           repeat if there is a signal.  Note that any errors in closing
14131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           the stdio descriptor are lost.  */
14144688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
14151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (status < 0) {
14164688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("dup2 failed: %s", strerror(errno));
14171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
14191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If /dev/null is not one of the stdio file descriptors, close it. */
14231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null > 2) {
14241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        TRACE("[ %5d Closing /dev/null file-descriptor=%d]\n", pid, dev_null);
14254688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(close(dev_null));
14264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status == -1) {
14274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("close failed: %s", strerror(errno));
14281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return return_value;
14331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
143516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerstatic int soinfo_link_image(soinfo *si, unsigned wr_offset)
14361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
14371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned *d;
1438f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee    /* "base" might wrap around UINT32_MAX. */
1439bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    Elf32_Addr base = si->load_bias;
1440b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    const Elf32_Phdr *phdr = si->phdr;
14411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int phnum = si->phnum;
1442b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    int relocating_linker = (si->flags & FLAG_LINKER) != 0;
14431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1444b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can't debug anything until the linker is relocated */
1445b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (!relocating_linker) {
1446b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        INFO("[ %5d linking %s ]\n", pid, si->name);
1447b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        DEBUG("%5d si->base = 0x%08x si->flags = 0x%08x\n", pid,
1448b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner            si->base, si->flags);
1449b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
14501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
145163f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    /* Extract dynamic section */
145263f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    si->dynamic = phdr_table_get_dynamic_section(phdr, phnum, base);
145363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    if (si->dynamic == NULL) {
1454b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
14554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("missing PT_DYNAMIC?!");
1456b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
145763f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner        goto fail;
145863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    } else {
1459b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1460b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner            DEBUG("%5d dynamic = %p\n", pid, si->dynamic);
1461b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
146263f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    }
146363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
146463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#ifdef ANDROID_ARM_LINKER
146563f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    (void) phdr_table_get_arm_exidx(phdr, phnum, base,
146663f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner                                    &si->ARM_exidx, &si->ARM_exidx_count);
146763f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif
146863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
1469468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    if (si->flags & (FLAG_EXE | FLAG_LINKER)) {
1470b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (phdr_table_unprotect_segments(si->phdr,
1471b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                          si->phnum,
1472b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                          si->load_bias) < 0) {
1473b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner            /* We can't call DL_ERR if the linker's relocations haven't
1474b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner             * been performed yet */
1475b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner            if (!relocating_linker) {
14764688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("can't unprotect segments for \"%s\": %s",
14774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                       si->name, strerror(errno));
14781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
1479b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner            goto fail;
14801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* extract useful information from dynamic section */
14841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(d = si->dynamic; *d; d++){
14851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        DEBUG("%5d d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", pid, d, d[0], d[1]);
14861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(*d++){
14871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_HASH:
1488f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->nbucket = ((unsigned *) (base + *d))[0];
1489f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->nchain = ((unsigned *) (base + *d))[1];
1490f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->bucket = (unsigned *) (base + *d + 8);
1491f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
14921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_STRTAB:
1494f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->strtab = (const char *) (base + *d);
14951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_SYMTAB:
1497f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->symtab = (Elf32_Sym *) (base + *d);
14981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTREL:
15001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if(*d != DT_REL) {
1501d00d23a9d4598108d7f498291b35c7730e48cf5cErik Gilling                DL_ERR("DT_RELA not supported");
15021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                goto fail;
15031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
15041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_JMPREL:
1506f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->plt_rel = (Elf32_Rel*) (base + *d);
15071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTRELSZ:
15091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->plt_rel_count = *d / 8;
15101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_REL:
1512f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->rel = (Elf32_Rel*) (base + *d);
15131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_RELSZ:
15151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->rel_count = *d / 8;
15161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTGOT:
15181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            /* Save this in case we decide to do lazy binding. We don't yet. */
1519f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->plt_got = (unsigned *)(base + *d);
15201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_DEBUG:
1522d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if !defined(ANDROID_MIPS_LINKER)
15231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            // Set the DT_DEBUG entry to the addres of _r_debug for GDB
15241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *d = (int) &_r_debug;
1525d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
15261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1527ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI         case DT_RELA:
15284688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("DT_RELA not supported");
15291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            goto fail;
15301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT:
1531f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->init_func = (void (*)(void))(base + *d);
15321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            DEBUG("%5d %s constructors (init func) found at %p\n",
15331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                  pid, si->name, si->init_func);
15341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI:
1536f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->fini_func = (void (*)(void))(base + *d);
15371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            DEBUG("%5d %s destructors (fini func) found at %p\n",
15381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                  pid, si->name, si->fini_func);
15391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAY:
1541f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->init_array = (unsigned *)(base + *d);
15421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            DEBUG("%5d %s constructors (init_array) found at %p\n",
15431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                  pid, si->name, si->init_array);
15441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAYSZ:
15461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
15471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAY:
1549f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->fini_array = (unsigned *)(base + *d);
15501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            DEBUG("%5d %s destructors (fini_array) found at %p\n",
15511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                  pid, si->name, si->fini_array);
15521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAYSZ:
15541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
15551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAY:
1557f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->preinit_array = (unsigned *)(base + *d);
15581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            DEBUG("%5d %s constructors (preinit_array) found at %p\n",
15591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                  pid, si->name, si->preinit_array);
15601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAYSZ:
15621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
15631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_TEXTREL:
15651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            /* TODO: make use of this. */
15661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            /* this means that we might have to write into where the text
15671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project             * segment was loaded during relocation... Do something with
15681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project             * it.
15691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project             */
15701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            DEBUG("%5d Text segment should be writable during relocation.\n",
15711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                  pid);
15721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1573d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if defined(ANDROID_MIPS_LINKER)
1574d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_NEEDED:
1575d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_STRSZ:
1576d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_SYMENT:
1577d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_RELENT:
1578d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             break;
1579d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_MAP:
1580d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* Set the DT_MIPS_RLD_MAP entry to the addres of _r_debug for GDB */
1581d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            {
1582d7daacb46372132ae3f0121647074936c304b572Raghu Gandham              struct r_debug **dp = (struct r_debug **)*d;
1583d7daacb46372132ae3f0121647074936c304b572Raghu Gandham              *dp = &_r_debug;
1584d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1585d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1586d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_VERSION:
1587d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_FLAGS:
1588d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_BASE_ADDRESS:
1589d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_UNREFEXTNO:
1590d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RWPLT:
1591d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1592d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1593d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_PLTGOT:
1594d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if 0
1595d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* not yet... */
1596d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_pltgot = (unsigned *)(si->base + *d);
1597d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1598d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1599d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1600d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_SYMTABNO:
1601d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_symtabno = *d;
1602d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1603d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1604d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_LOCAL_GOTNO:
1605d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_local_gotno = *d;
1606d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1607d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1608d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_GOTSYM:
1609d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_gotsym = *d;
1610d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1611d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1612d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        default:
1613d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            DEBUG("%5d Unused DT entry: type 0x%08x arg 0x%08x\n",
1614d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                  pid, d[-1], d[0]);
1615d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1616d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
16171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
16181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1620be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    DEBUG("%5d si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
16211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           pid, si->base, si->strtab, si->symtab);
16221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if((si->strtab == 0) || (si->symtab == 0)) {
16244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("missing essential tables");
16251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        goto fail;
16261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16284fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* if this is the main executable, then load all of the preloads now */
16294fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    if(si->flags & FLAG_EXE) {
16304fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        int i;
16314fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        memset(preloads, 0, sizeof(preloads));
16324fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        for(i = 0; ldpreload_names[i] != NULL; i++) {
16334fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            soinfo *lsi = find_library(ldpreload_names[i]);
16344fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            if(lsi == 0) {
16354fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer                strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
16364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
16374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                       ldpreload_names[i], si->name, tmp_err_buf);
16384fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer                goto fail;
16394fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            }
16404fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            lsi->refcount++;
16414fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            preloads[i] = lsi;
16424fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        }
16434fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
16444fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
16451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(d = si->dynamic; *d; d += 2) {
16461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if(d[0] == DT_NEEDED){
16471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            DEBUG("%5d %s needs %s\n", pid, si->name, si->strtab + d[1]);
16482e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin            soinfo *lsi = find_library(si->strtab + d[1]);
16491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if(lsi == 0) {
16500353195f344666256dba474a15c9ba22cf0cccc9Dima Zavin                strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
16514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
16524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                       si->strtab + d[1], si->name, tmp_err_buf);
16531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                goto fail;
16541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
16556ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev            /* Save the soinfo of the loaded DT_NEEDED library in the payload
16566ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev               of the DT_NEEDED entry itself, so that we can retrieve the
16576ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev               soinfo directly later from the dynamic segment.  This is a hack,
16586ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev               but it allows us to map from DT_NEEDED to soinfo efficiently
16599ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich               later on when we resolve relocations, trying to look up a symbol
16606ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev               with dlsym().
16616ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev            */
16626ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev            d[1] = (unsigned)lsi;
16631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            lsi->refcount++;
16641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
16651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if(si->plt_rel) {
16681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        DEBUG("[ %5d relocating %s plt ]\n", pid, si->name );
166916084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner        if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count))
16701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            goto fail;
16711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if(si->rel) {
16731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        DEBUG("[ %5d relocating %s ]\n", pid, si->name );
167416084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner        if(soinfo_relocate(si, si->rel, si->rel_count))
16751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            goto fail;
16761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1678d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#ifdef ANDROID_MIPS_LINKER
1679d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if(mips_relocate_got(si)) {
1680d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        goto fail;
1681d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1682d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1683d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
16841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_LINKED;
16851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    DEBUG("[ %5d finished linking %s ]\n", pid, si->name);
16861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1687b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* All relocations are done, we can protect our segments back to
1688b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner     * read-only. */
1689b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
16904688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't protect segments for \"%s\": %s",
16914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               si->name, strerror(errno));
1692b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        goto fail;
16931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1695b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can also turn on GNU RELRO protection */
1696b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
16974688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
16984688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               si->name, strerror(errno));
1699b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        goto fail;
17009ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich    }
17019ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
17021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If this is a SET?ID program, dup /dev/null to opened stdin,
17031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       stdout and stderr to close a security hole described in:
17041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
17061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
17084688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (program_is_setuid) {
17094688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        nullify_closed_stdio();
17104688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
17111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    notify_gdb_of_load(si);
17121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
17131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectfail:
1715a716190241da07f42cf874b04bf044261f36381cDima Zavin    ERROR("failed to link %s\n", si->name);
17161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_ERROR;
17171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return -1;
17181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
17191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17204688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void parse_path(const char* path, const char* delimiters,
17214688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                       const char** array, char* buf, size_t buf_size, size_t max_count)
1722bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley{
17234688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (path == NULL) {
17244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return;
17254688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
1726bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
17274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    size_t len = strlcpy(buf, path, buf_size);
1728bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
17294688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    size_t i = 0;
17304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    char* buf_p = buf;
17314688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
17324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (*array[i] != '\0') {
1733bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley            ++i;
17344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
1735bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    }
1736bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
17374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Forget the last path if we had to truncate; this occurs if the 2nd to
17384688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // last char isn't '\0' (i.e. wasn't originally a delimiter).
17394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
17404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        array[i - 1] = NULL;
1741bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    } else {
17424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        array[i] = NULL;
1743bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    }
1744bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley}
1745bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
17464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void parse_LD_LIBRARY_PATH(const char* path) {
17474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_path(path, ":", ldpaths,
17484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               ldpaths_buf, sizeof(ldpaths_buf), LDPATH_MAX);
17494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes}
17504fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
17514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void parse_LD_PRELOAD(const char* path) {
17524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // We have historically supported ':' as well as ' ' in LD_PRELOAD.
17534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_path(path, " :", ldpreload_names,
17544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               ldpreloads_buf, sizeof(ldpreloads_buf), LDPRELOAD_MAX);
17554fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer}
17564fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1757468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1758468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and
1759468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs
1760468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point.
1761468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
1762bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissellstatic unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base)
17631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
17641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    static soinfo linker_soinfo;
17651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int argc = (int) *elfdata;
17671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    char **argv = (char**) (elfdata + 1);
1768bb44055d0a3515410f45417d5b0e047cc60f0dc6Stephen Smalley    unsigned *vecs = (unsigned*) (argv + argc + 1);
1769bb44055d0a3515410f45417d5b0e047cc60f0dc6Stephen Smalley    unsigned *v;
17701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
1771326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng    int i;
17721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    struct link_map * map;
1773be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    const char *ldpath_env = NULL;
1774be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    const char *ldpreload_env = NULL;
17751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1776ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner    /* NOTE: we store the elfdata pointer on a special location
1777ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       of the temporary TLS area in order to pass it to
1778ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       the C Library's runtime initializer.
1779ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *
1780ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       The initializer must clear the slot and reset the TLS
1781ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       to point to a different location to ensure that no other
1782ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       shared library constructor can access it.
1783ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     */
17841a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    __libc_init_tls(elfdata);
17851a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov
17861a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    pid = getpid();
17871a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov
17881a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING
17891a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    struct timeval t0, t1;
17901a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    gettimeofday(&t0, 0);
17911a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif
17921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1793be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    /* Initialize environment functions, and get to the ELF aux vectors table */
1794be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    vecs = linker_env_init(vecs);
1795be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
1796861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalley    /* Check auxv for AT_SECURE first to see if program is setuid, setgid,
1797861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalley       has file caps, or caused a SELinux/AppArmor domain transition. */
1798861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalley    for (v = vecs; v[0]; v += 2) {
1799861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalley        if (v[0] == AT_SECURE) {
1800861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalley            /* kernel told us whether to enable secure mode */
1801861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalley            program_is_setuid = v[1];
1802861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalley            goto sanitize;
1803861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalley        }
1804861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalley    }
1805861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalley
1806861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalley    /* Kernel did not provide AT_SECURE - fall back on legacy test. */
1807861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalley    program_is_setuid = (getuid() != geteuid()) || (getgid() != getegid());
1808861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalley
1809861b42a2d83f72f11b4271b5b8431595a097c7c2Stephen Smalleysanitize:
1810be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    /* Sanitize environment if we're loading a setuid program */
1811be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    if (program_is_setuid)
1812be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner        linker_env_secure();
1813be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
18141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    debugger_init();
18151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1816be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    /* Get a few environment variables */
1817be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    {
18188c4f3ce8d084e3949d77ad8743593b5c83c3d41cNick Kralevich#if LINKER_DEBUG
1819be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner        const char* env;
1820be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner        env = linker_env_get("DEBUG"); /* XXX: TODO: Change to LD_DEBUG */
1821be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner        if (env)
1822be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner            debug_verbosity = atoi(env);
18238c4f3ce8d084e3949d77ad8743593b5c83c3d41cNick Kralevich#endif
1824be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
1825be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner        /* Normally, these are cleaned by linker_env_secure, but the test
1826be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner         * against program_is_setuid doesn't cost us anything */
1827be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner        if (!program_is_setuid) {
1828be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner            ldpath_env = linker_env_get("LD_LIBRARY_PATH");
1829be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner            ldpreload_env = linker_env_get("LD_PRELOAD");
18301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
18311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    INFO("[ android linker & debugger ]\n");
18341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    DEBUG("%5d elfdata @ 0x%08x\n", pid, (unsigned)elfdata);
18351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
183616084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    si = soinfo_alloc(argv[0]);
18371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if(si == 0) {
18381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        exit(-1);
18391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* bootstrap the link map, the main exe always needs to be first */
18421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_EXE;
18431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map = &(si->linkmap);
18441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = 0;
18461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_name = argv[0];
18471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_prev = NULL;
18481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_next = NULL;
18491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_map = map;
18511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
18521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1853bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell        /* gdb expects the linker to be in the debug shared object list.
1854bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * Without this, gdb has trouble locating the linker's ".text"
1855bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * and ".plt" sections. Gdb could also potentially use this to
1856bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1857bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * Don't use soinfo_alloc(), because the linker shouldn't
18581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         * be on the soinfo list.
18591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         */
1860be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
18611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    linker_soinfo.flags = 0;
1862bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell    linker_soinfo.base = linker_base;
186306f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng    /*
186406f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     * Set the dynamic field in the link map otherwise gdb will complain with
186506f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     * the following:
186606f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     *   warning: .dynamic section for "/system/bin/linker" is not at the
186706f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     *   expected address (wrong library or version mismatch?)
186806f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     */
186906f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng    Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
187006f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng    Elf32_Phdr *phdr =
187106f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng        (Elf32_Phdr *)((unsigned char *) linker_base + elf_hdr->e_phoff);
187206f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng    linker_soinfo.dynamic =
187306f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng        phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base);
18741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(&linker_soinfo);
18751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* extract information passed from the kernel */
18771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    while(vecs[0] != 0){
18781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(vecs[0]){
18791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case AT_PHDR:
18801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->phdr = (Elf32_Phdr*) vecs[1];
18811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case AT_PHNUM:
18831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->phnum = (int) vecs[1];
18841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case AT_ENTRY:
18861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->entry = vecs[1];
18871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
18891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        vecs += 2;
18901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18928180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    /* Compute the value of si->base. We can't rely on the fact that
18938180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * the first entry is the PHDR because this will not be true
18948180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * for certain executables (e.g. some in the NDK unit test suite)
18958180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     */
18968180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    int nn;
18978180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    si->base = 0;
1898b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    si->size = phdr_table_get_load_size(si->phdr, si->phnum);
1899bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    si->load_bias = 0;
19008180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    for ( nn = 0; nn < si->phnum; nn++ ) {
19018180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner        if (si->phdr[nn].p_type == PT_PHDR) {
1902bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner            si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr;
1903bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner            si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset;
19048180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner            break;
19058180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner        }
19068180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    }
19071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->dynamic = (unsigned *)-1;
19086774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    si->refcount = 1;
19091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19104688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
19114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_LIBRARY_PATH(ldpath_env);
19124688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_PRELOAD(ldpreload_env);
19134fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
191416084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    if(soinfo_link_image(si, 0)) {
19152e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin        char errmsg[] = "CANNOT LINK EXECUTABLE\n";
19162e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin        write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
19172e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin        write(2, errmsg, sizeof(errmsg));
19181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        exit(-1);
19191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
19201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1921326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng    for(i = 0; preloads[i] != NULL; i++) {
1922326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng        soinfo_call_constructors(preloads[i]);
1923326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng    }
1924326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng
192516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    soinfo_call_constructors(si);
1926e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
19274a9afcb10151b083cd2d75253385615f459172edIliyan Malchev#if ALLOW_SYMBOLS_FROM_MAIN
19284a9afcb10151b083cd2d75253385615f459172edIliyan Malchev    /* Set somain after we've loaded all the libraries in order to prevent
19294a9afcb10151b083cd2d75253385615f459172edIliyan Malchev     * linking of symbols back to the main image, which is not set up at that
19304a9afcb10151b083cd2d75253385615f459172edIliyan Malchev     * point yet.
19314a9afcb10151b083cd2d75253385615f459172edIliyan Malchev     */
19324a9afcb10151b083cd2d75253385615f459172edIliyan Malchev    somain = si;
19334a9afcb10151b083cd2d75253385615f459172edIliyan Malchev#endif
19344a9afcb10151b083cd2d75253385615f459172edIliyan Malchev
19351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING
19361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    gettimeofday(&t1,NULL);
19371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
19381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
19391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
19401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               ));
19411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
19431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
19441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           linker_stats.reloc[RELOC_ABSOLUTE],
19451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           linker_stats.reloc[RELOC_RELATIVE],
19461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           linker_stats.reloc[RELOC_COPY],
19471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           linker_stats.reloc[RELOC_SYMBOL]);
19481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
19501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
19511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned n;
19521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned i;
19531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned count = 0;
19541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        for(n = 0; n < 4096; n++){
19551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if(bitmask[n]){
19561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                unsigned x = bitmask[n];
19571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                for(i = 0; i < 8; i++){
19581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    if(x & 1) count++;
19591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    x >>= 1;
19601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                }
19611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
19621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
19631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
19641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
19651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES
19681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    fflush(stdout);
19691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    TRACE("[ %5d Ready to execute '%s' @ 0x%08x ]\n", pid, si->name,
19721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project          si->entry);
19731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si->entry;
19741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
1975468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1976468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1977468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Find the value of AT_BASE passed to us by the kernel. This is the load
1978468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * location of the linker.
1979468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
1980468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevichstatic unsigned find_linker_base(unsigned **elfdata) {
1981468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    int argc = (int) *elfdata;
1982468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    char **argv = (char**) (elfdata + 1);
1983468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    unsigned *vecs = (unsigned*) (argv + argc + 1);
1984468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    while (vecs[0] != 0) {
1985468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        vecs++;
1986468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    }
1987468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1988468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    /* The end of the environment block is marked by two NULL pointers */
1989468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    vecs++;
1990468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1991468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    while(vecs[0]) {
1992468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if (vecs[0] == AT_BASE) {
1993468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            return vecs[1];
1994468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        }
1995468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        vecs += 2;
1996468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    }
1997468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1998468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    return 0; // should never happen
1999468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
2000468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2001bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only
2002bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library
2003bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself.
2004bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *
2005bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input:
2006bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    elf    -> address of ELF header, assumed to be at the start of the file.
2007bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return:
2008bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    load bias, i.e. add the value of any p_vaddr in the file to get
2009bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    the corresponding address in memory.
2010bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */
2011bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turnerstatic Elf32_Addr
2012bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turnerget_elf_exec_load_bias(const Elf32_Ehdr* elf)
2013bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner{
2014bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    Elf32_Addr        offset     = elf->e_phoff;
2015bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
2016bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    const Elf32_Phdr* phdr_end   = phdr_table + elf->e_phnum;
2017bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    const Elf32_Phdr* phdr;
2018bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
2019bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    for (phdr = phdr_table; phdr < phdr_end; phdr++) {
2020bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner        if (phdr->p_type == PT_LOAD) {
2021bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner            return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
2022bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner        }
2023bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    }
2024bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    return 0;
2025bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner}
2026bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
2027468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
2028468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This
2029468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and
2030468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation().
2031468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich *
2032468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own
2033468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern
2034468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault.
2035468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
20364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesextern "C" unsigned __linker_init(unsigned **elfdata) {
2037468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    unsigned linker_addr = find_linker_base(elfdata);
2038468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
2039468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    Elf32_Phdr *phdr =
2040468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
2041468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2042468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    soinfo linker_so;
2043468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    memset(&linker_so, 0, sizeof(soinfo));
2044468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2045468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.base = linker_addr;
2046b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2047bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
2048468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.dynamic = (unsigned *) -1;
2049468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.phdr = phdr;
2050468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.phnum = elf_hdr->e_phnum;
2051468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.flags |= FLAG_LINKER;
2052468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
205316084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    if (soinfo_link_image(&linker_so, 0)) {
2054468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // It would be nice to print an error message, but if the linker
2055468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // can't link itself, there's no guarantee that we'll be able to
2056468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // call write() (because it involves a GOT reference).
2057468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        //
2058468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // This situation should never occur unless the linker itself
2059468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // is corrupt.
2060468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        exit(-1);
2061468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    }
2062468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2063468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    // We have successfully fixed our own relocations. It's safe to run
2064468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    // the main part of the linker now.
2065bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell    return __linker_init_post_relocation(elfdata, linker_addr);
2066468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
2067