linker.cpp revision 9c94fc9fbefe55836c1e1b949850f7726e64c118
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>
341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdio.h>
351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdlib.h>
361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <string.h>
371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <sys/atomics.h>
384688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <sys/mman.h>
394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <sys/stat.h>
404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <unistd.h>
411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Private C library headers.
434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <private/bionic_tls.h>
444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <private/logd.h>
453b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes#include <private/ScopedPthreadMutexLocker.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
53bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley/* Assume average path length of 64 and max 8 paths */
54bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley#define LDPATH_BUFSIZE 512
55bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley#define LDPATH_MAX 8
56bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
574fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer#define LDPRELOAD_BUFSIZE 512
584fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer#define LDPRELOAD_MAX 8
594fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Do NOT use malloc() and friends or pthread_*() code here.
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Don't use printf() either; it's caused mysterious memory
641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * corruption in the past.
651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The linker runs before we bring up libc and it's easiest
661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to make sure it does not depend on any complex libc features
671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * open issues / todo:
691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - are we doing everything we should for ARM_COPY relocations?
711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - cleaner error reporting
721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - after linking, set as much stuff as possible to READONLY
731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   and NOEXEC
744688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes */
751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
76124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic bool soinfo_link_image(soinfo* si);
771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
78ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
79ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn// maps, each a single page in size. The pages are broken up into as many struct soinfo
80ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn// objects as will fit, and they're all threaded together on a free list.
81ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
82ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstruct soinfo_pool_t {
83ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo_pool_t* next;
84ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo info[SOINFO_PER_POOL];
85ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn};
86ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic struct soinfo_pool_t* gSoInfoPools = NULL;
87ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic soinfo* gSoInfoFreeList = NULL;
88ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic soinfo *solist = &libdl_info;
901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic soinfo *sonext = &libdl_info;
914a9afcb10151b083cd2d75253385615f459172edIliyan Malchevstatic soinfo *somain; /* main process, always the one after libdl_info */
921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
93124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* const gSoPaths[] = {
94124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  "/vendor/lib",
95124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  "/system/lib",
96124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  NULL
97124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes};
98124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes
99124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic char gLdPathsBuffer[LDPATH_BUFSIZE];
100124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* gLdPaths[LDPATH_MAX + 1];
101bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
102124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
103124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
1044fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1054fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischerstatic soinfo *preloads[LDPRELOAD_MAX + 1];
1064fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
10761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughesstatic int debug_verbosity;
1081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
109bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesenum RelocationKind {
110bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocAbsolute = 0,
111bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocRelative,
112bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocCopy,
113bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocSymbol,
114bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocMax
115bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
116be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
1171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
118bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstruct linker_stats_t {
119bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    int count[kRelocMax];
120bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
121bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
122bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic linker_stats_t linker_stats;
123bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
124bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind kind) {
125bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    ++linker_stats.count[kind];
126bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
127bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
128bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind) {
129bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
1301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
133bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic unsigned bitmask[4096];
134bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(offset) \
135bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    do { \
136bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
137bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    } while(0)
138bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
139bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(x) do {} while (0)
1401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// You shouldn't try to call memory-allocating functions in the dynamic linker.
1434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Guard against the most obvious ones.
1444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#define DISALLOW_ALLOCATION(return_type, name, ...)                             \
1454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    return_type name __VA_ARGS__                                                \
1464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    {                                                                           \
1474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
1484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes         __libc_android_log_write(ANDROID_LOG_FATAL, "linker", msg);            \
1494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        write(2, msg, sizeof(msg));                                             \
1504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        abort();                                                                \
1514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
1524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#define UNUSED __attribute__((unused))
1534688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
1544688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void, free, (void* u UNUSED));
1554688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
1564688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
1572e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1580353195f344666256dba474a15c9ba22cf0cccc9Dima Zavinstatic char tmp_err_buf[768];
1592e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavinstatic char __linker_dl_err_buf[768];
160e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes#define DL_ERR(fmt, x...) \
161e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes    do { \
1623b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes        format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), fmt, ##x); \
1639c94fc9fbefe55836c1e1b949850f7726e64c118Elliott Hughes        /* If LD_DEBUG is set high enough, send every dlerror(3) message to the log. */ \
1649c94fc9fbefe55836c1e1b949850f7726e64c118Elliott Hughes        DEBUG(fmt "\n", ##x); \
1652e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    } while(0)
1662e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1675419b9474753d25dff947c7740532f86d130c0beElliott Hughesconst char* linker_get_error() {
1685419b9474753d25dff947c7740532f86d130c0beElliott Hughes  return &__linker_dl_err_buf[0];
1692e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin}
1702e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
1721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This function is an empty stub where GDB locates a breakpoint to get notified
1731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about linker activity.
1741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1755419b9474753d25dff947c7740532f86d130c0beElliott Hughesextern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
1761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
177bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic r_debug _r_debug = {1, NULL, &rtld_db_dlactivity,
1781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                                  RT_CONSISTENT, 0};
179bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic link_map* r_debug_tail = 0;
1801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1813b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughesstatic pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
1821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
183bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void insert_soinfo_into_debug_map(soinfo * info) {
184bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    // Copy the necessary fields into the debug structure.
185bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    link_map* map = &(info->linkmap);
1861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = info->base;
1871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_name = (char*) info->name;
1885cf640c926e7d7b79934c97226433a3dc448e1ebThinker K.F Li    map->l_ld = (uintptr_t)info->dynamic;
1891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Stick the new library at the end of the list.
1911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * gdb tends to care more about libc than it does
1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * about leaf libraries, and ordering it this way
1931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * reduces the back-and-forth over the wire.
1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
1951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (r_debug_tail) {
1961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        r_debug_tail->l_next = map;
1971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = r_debug_tail;
1981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
1991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    } else {
2001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        _r_debug.r_map = map;
2011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = 0;
2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
207bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void remove_soinfo_from_debug_map(soinfo* info) {
208bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    link_map* map = &(info->linkmap);
2095e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
210bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (r_debug_tail == map) {
2115e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        r_debug_tail = map->l_prev;
212bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2135e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
214bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_prev) {
215bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_prev->l_next = map->l_next;
216bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
217bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_next) {
218bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_next->l_prev = map->l_prev;
219bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2205e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2215e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
222bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_load(soinfo* info) {
2231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (info->flags & FLAG_EXE) {
2241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        // GDB already knows about the main executable
2251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
2261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2283b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes    ScopedPthreadMutexLocker locker(&gDebugMutex);
2291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_ADD;
2311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(info);
2341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_CONSISTENT;
2361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2375e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2385e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
239bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_unload(soinfo* info) {
2405e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    if (info->flags & FLAG_EXE) {
2415e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        // GDB already knows about the main executable
2425e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        return;
2435e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    }
2445e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2453b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes    ScopedPthreadMutexLocker locker(&gDebugMutex);
2465e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2475e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    _r_debug.r_state = RT_DELETE;
2485e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2495e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2505e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    remove_soinfo_from_debug_map(info);
2515e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2525e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    _r_debug.r_state = RT_CONSISTENT;
2535e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
25618a206c81d9743481e364384affd43306911283dElliott Hughesvoid notify_gdb_of_libraries() {
2571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_ADD;
2581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_CONSISTENT;
2601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
263ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic bool ensure_free_list_non_empty() {
264ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (gSoInfoFreeList != NULL) {
265ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return true;
266ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
267ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
268ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Allocate a new pool.
269ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
270ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn                                                              PROT_READ|PROT_WRITE,
271ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn                                                              MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
272ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (pool == MAP_FAILED) {
273ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return false;
274ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
275ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
276ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Add the pool to our list of pools.
277ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  pool->next = gSoInfoPools;
278ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  gSoInfoPools = pool;
279ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
280ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Chain the entries in the new pool onto the free list.
281ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  gSoInfoFreeList = &pool->info[0];
282ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo* next = NULL;
283ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
284ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    pool->info[i].next = next;
285ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    next = &pool->info[i];
286ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
287ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
288ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  return true;
289ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn}
2901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
291d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic void set_soinfo_pool_protection(int protection) {
292d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
293d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (mprotect(p, sizeof(*p), protection) == -1) {
294d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      abort(); // Can't happen.
295d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    }
296d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
297d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
298d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
299ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic soinfo* soinfo_alloc(const char* name) {
300ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (strlen(name) >= SOINFO_NAME_LEN) {
301ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    DL_ERR("library name \"%s\" too long", name);
302ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return NULL;
303ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
304ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
305ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (!ensure_free_list_non_empty()) {
306ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    DL_ERR("out of memory when loading \"%s\"", name);
307ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return NULL;
308ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
3091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
310ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Take the head element off the free list.
311ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo* si = gSoInfoFreeList;
312ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  gSoInfoFreeList = gSoInfoFreeList->next;
3131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
314ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Initialize the new element.
315ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  memset(si, 0, sizeof(soinfo));
316ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  strlcpy(si->name, name, sizeof(si->name));
317ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext->next = si;
318ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext = si;
3191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
32061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("name %s: allocated soinfo @ %p\n", name, si);
321ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  return si;
3221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void soinfo_free(soinfo* si)
3251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si == NULL) {
3274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return;
3284688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
3294688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *prev = NULL, *trav;
3311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
33261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("name %s: freeing soinfo @ %p\n", si->name, si);
3331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(trav = solist; trav != NULL; trav = trav->next){
3351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (trav == si)
3361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
3371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        prev = trav;
3381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (trav == NULL) {
3401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* si was not ni solist */
3414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("name \"%s\" is not in solist!", si->name);
3421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
3431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
345be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    /* prev will never be NULL, because the first entry in solist is
3461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       always the static libdl_info.
3471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    */
3481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    prev->next = si->next;
3491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (si == sonext) sonext = prev;
350ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    si->next = gSoInfoFreeList;
351ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    gSoInfoFreeList = si;
3521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#ifdef ANDROID_ARM_LINKER
3554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* For a given PC, find the .so that it belongs to.
3571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the base address of the .ARM.exidx section
3581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for that .so, and the number of 8-byte entries
3591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in that section (via *pcount).
3601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Intended to be called by libc's __gnu_Unwind_Find_exidx().
3621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3633b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes * This function is exposed via dlfcn.cpp and libdl.so.
3641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
3651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
3661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
3681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned addr = (unsigned)pc;
3691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
370468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    for (si = solist; si != 0; si = si->next){
371468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if ((addr >= si->base) && (addr < (si->base + si->size))) {
372468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            *pcount = si->ARM_exidx_count;
373f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            return (_Unwind_Ptr)si->ARM_exidx;
3741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
3751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   *pcount = 0;
3771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return NULL;
3781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
380d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
3814688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Here, we only have to provide a callback to iterate across all the
3831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * loaded libraries. gcc_eh does the rest. */
3841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectint
385bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesdl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
3861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                void *data)
3871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int rv = 0;
389bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    for (soinfo* si = solist; si != NULL; si = si->next) {
390bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        dl_phdr_info dl_info;
3911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_addr = si->linkmap.l_addr;
3921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_name = si->linkmap.l_name;
3931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phdr = si->phdr;
3941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phnum = si->phnum;
395bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        rv = cb(&dl_info, sizeof(dl_phdr_info), data);
396bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        if (rv != 0) {
3971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
398bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        }
3991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return rv;
4011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4024688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
4031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
4041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
40516084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerstatic Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name)
4061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s;
4081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *symtab = si->symtab;
4091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const char *strtab = si->strtab;
4101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned n;
4111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
41261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE_TYPE(LOOKUP, "SEARCH %s in %s@0x%08x %08x %d\n",
4131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               name, si->name, si->base, hash, hash % si->nbucket);
4141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    n = hash % si->nbucket;
4151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
4171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        s = symtab + n;
4181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if(strcmp(strtab + s->st_name, name)) continue;
4191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
420e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            /* only concern ourselves with global and weak symbol definitions */
4211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(ELF32_ST_BIND(s->st_info)){
4221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case STB_GLOBAL:
423e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan        case STB_WEAK:
424439fa8ea8c33a7945258dfe84a71ab2665efaa9aRobin Burchell            if(s->st_shndx == SHN_UNDEF)
425439fa8ea8c33a7945258dfe84a71ab2665efaa9aRobin Burchell                continue;
4261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
42761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(LOOKUP, "FOUND %s in %s (%08x) %d\n",
4281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       name, si->name, s->st_value, s->st_size);
4291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return s;
4301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
4311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
433943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
4341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned elfhash(const char *_name)
4371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const unsigned char *name = (const unsigned char *) _name;
4391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned h = 0, g;
4401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    while(*name) {
4421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h = (h << 4) + *name++;
4431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        g = h & 0xf0000000;
4441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g;
4451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g >> 24;
4461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return h;
4481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic Elf32_Sym *
4515ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvelsoinfo_do_lookup(soinfo *si, const char *name, soinfo **lsi,
4525ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                 soinfo *needed[])
4536ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev{
454943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
455d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    Elf32_Sym *s = NULL;
4564fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    int i;
4576ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
458c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin    if (si != NULL && somain != NULL) {
4595ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
4605ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        /*
461c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * Local scope is executable scope. Just start looking into it right away
462c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * for the shortcut.
4635ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel         */
4645ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
465c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        if (si == somain) {
466c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
4675ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (s != NULL) {
468c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
4695ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                goto done;
4705ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
471c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        } else {
472c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
473c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
474c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
475c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with symbolic relocations disabled, the
476c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * first place to look to resolve external references is the main
477c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * executable.
478c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
479c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
480c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (!si->has_DT_SYMBOLIC) {
48161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                DEBUG("%s: looking up %s in executable %s\n",
48261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
483c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
484c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
485c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
486c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
487c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
488c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
4895ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
490c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Look for symbols in the local scope (the object who is
491c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * searching). This happens with C++ templates on i386 for some
492c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * reason.
493c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             *
494c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Notes on weak symbols:
495c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * The ELF specs are ambiguous about treatment of weak definitions in
496c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * dynamic linking.  Some systems return the first definition found
497c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * and some the first non-weak definition.   This is system dependent.
498c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Here we return the first definition found for simplicity.  */
499c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
500c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
501c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (s != NULL) {
502c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
503c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                goto done;
504c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
505c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
506c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
507c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with -Bsymbolic and symbol is not found
508c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * in the local scope, try to find the symbol in the main executable.
509c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
510c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
511c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (si->has_DT_SYMBOLIC) {
51261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                DEBUG("%s: looking up %s in executable %s after local scope\n",
51361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
514c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
515c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
516c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
517c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
518c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
519c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5205ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
521d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    }
5226ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5234fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* Next, look for it in the preloads list */
5244fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    for(i = 0; preloads[i] != NULL; i++) {
5255ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_elf_lookup(preloads[i], elf_hash, name);
5265ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if(s != NULL) {
5275ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            *lsi = preloads[i];
5284fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            goto done;
5295ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5304fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
5314fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
53212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    for(i = 0; needed[i] != NULL; i++) {
53361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DEBUG("%s: looking up %s in %s\n",
53461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes              si->name, name, needed[i]->name);
5355ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_elf_lookup(needed[i], elf_hash, name);
5365ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if (s != NULL) {
5375ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            *lsi = needed[i];
53812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            goto done;
5395ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5406ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5416ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5426ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevdone:
5436ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    if(s != NULL) {
54461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = 0x%08x, "
545bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner                   "found in %s, base = 0x%08x, load bias = 0x%08x\n",
54661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                   si->name, name, s->st_value,
5475ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                   (*lsi)->name, (*lsi)->base, (*lsi)->load_bias);
5486ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        return s;
5496ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5506ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
551943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
5526ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev}
5536ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5546ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev/* This is used by dl_sym().  It performs symbol lookup only within the
5556ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev   specified soinfo object and not in any of its dependencies.
5566ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
55716084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' TurnerElf32_Sym *soinfo_lookup(soinfo *si, const char *name)
5581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
55916084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    return soinfo_elf_lookup(si, elfhash(name), name);
5601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5626ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev/* This is used by dl_sym().  It performs a global symbol lookup.
5636ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
5641698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt FischerElf32_Sym *lookup(const char *name, soinfo **found, soinfo *start)
5651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
566943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
5671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s = NULL;
5681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
5691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5701698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer    if(start == NULL) {
5711698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer        start = solist;
5721698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer    }
5731698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer
5741698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer    for(si = start; (s == NULL) && (si != NULL); si = si->next)
5751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
5766ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        if(si->flags & FLAG_ERROR)
5771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
57816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner        s = soinfo_elf_lookup(si, elf_hash, name);
5791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (s != NULL) {
5809ea64da6c511e8f9f4edae4c10c20879957631abIliyan Malchev            *found = si;
5811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
5821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
5831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
5841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5856ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    if(s != NULL) {
58661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        TRACE_TYPE(LOOKUP, "%s s->st_value = 0x%08x, si->base = 0x%08x\n",
58761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                   name, s->st_value, si->base);
5881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return s;
5891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
5901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
591943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
5921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
594bda5da074eab4bdf374e1f4a19d480c62c72f5ffMathias Agopiansoinfo *find_containing_library(const void *addr)
595e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer{
596e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    soinfo *si;
597e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
598e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    for(si = solist; si != NULL; si = si->next)
599e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    {
600e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
601e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer            return si;
602e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        }
603e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
604e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
605e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    return NULL;
606e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
607e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
60816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' TurnerElf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr)
609e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer{
610e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    unsigned int i;
611e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    unsigned soaddr = (unsigned)addr - si->base;
612e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
613e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    /* Search the library's symbol table for any defined symbol which
614e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer     * contains this address */
615e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    for(i=0; i<si->nchain; i++) {
616e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        Elf32_Sym *sym = &si->symtab[i];
617e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
618e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        if(sym->st_shndx != SHN_UNDEF &&
619e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer           soaddr >= sym->st_value &&
620e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer           soaddr < sym->st_value + sym->st_size) {
621e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer            return sym;
622e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        }
623e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
624e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
625e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    return NULL;
626e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
627e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
6281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0
6291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void dump(soinfo *si)
6301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
6311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s = si->symtab;
6321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned n;
6331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(n = 0; n < si->nchain; n++) {
63561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        TRACE("%04d> %08x: %02x %04x %08x %08x %s\n", n, s,
6361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               s->st_info, s->st_shndx, s->st_value, s->st_size,
6371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               si->strtab + s->st_name);
6381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        s++;
6391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
6421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
643124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library_on_path(const char* name, const char* const paths[]) {
644124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  char buf[512];
645124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  for (size_t i = 0; paths[i] != NULL; ++i) {
646124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    int n = format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
647124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
6489c94fc9fbefe55836c1e1b949850f7726e64c118Elliott Hughes      PRINT("Warning: ignoring very long library path: %s/%s\n", paths[i], name);
649124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      continue;
6501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
651124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
652124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (fd != -1) {
653124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      return fd;
654124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
655124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
656124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return -1;
6571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
659124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library(const char* name) {
66061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("[ opening %s ]\n", name);
6611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
662124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // If the name contains a slash, we should attempt to open it directly and not search the paths.
663124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (strchr(name, '/') != NULL) {
6646971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
6656971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    if (fd != -1) {
6666971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes      return fd;
6676971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    }
6686971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
669124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
6701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
671124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
672124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  int fd = open_library_on_path(name, gLdPaths);
673124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (fd == -1) {
674124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    fd = open_library_on_path(name, gSoPaths);
675124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
676124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return fd;
6771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Returns 'true' if the library is prelinked or on failure so we error out
6804688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// either way. We no longer support prelinking.
6814688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic bool is_prelinked(int fd, const char* name)
6821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
6834688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    struct prelink_info_t {
6844688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        long mmap_addr;
6854688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        char tag[4]; // "PRE ".
6864688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    };
6874688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
688bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
6891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (sz < 0) {
6904688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("lseek failed: %s", strerror(errno));
6914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return true;
6921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
694bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    prelink_info_t info;
6958dfc073b1487ab25ab483ab346cee1d9e584adb1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
6968dfc073b1487ab25ab483ab346cee1d9e584adb1Elliott Hughes    if (rc != sizeof(info)) {
69761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DL_ERR("could not read prelink_info_t structure for \"%s\": %s", name, strerror(errno));
6984688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return true;
6991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (memcmp(info.tag, "PRE ", 4) == 0) {
7024688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("prelinked libraries no longer supported: %s", name);
7034688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return true;
7041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7054688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    return false;
7061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
70816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner/* verify_elf_header
70916084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner *      Verifies the content of an ELF header.
7101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
7111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Args:
7121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
7131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns:
7141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *       0 on success
7151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *      -1 if no valid ELF object is found @ base.
7161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
7171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
71816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerverify_elf_header(const Elf32_Ehdr* hdr)
7191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
7201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
7211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
7221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
7231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
724d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    if (hdr->e_type != ET_DYN) return -1;
7251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* TODO: Should we verify anything else in the header? */
727897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#ifdef ANDROID_ARM_LINKER
728897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang    if (hdr->e_machine != EM_ARM) return -1;
729897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#elif defined(ANDROID_X86_LINKER)
730897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang    if (hdr->e_machine != EM_386) return -1;
731d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_MIPS_LINKER)
732d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if (hdr->e_machine != EM_MIPS) return -1;
733897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#endif
7341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
7351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct scoped_fd {
7384688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ~scoped_fd() {
7394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (fd != -1) {
7404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            close(fd);
7414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
7424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    int fd;
7444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes};
7451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct soinfo_ptr {
7474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo_ptr(const char* name) {
7484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        const char* bname = strrchr(name, '/');
7494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        ptr = soinfo_alloc(bname ? bname + 1 : name);
7504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ~soinfo_ptr() {
7524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        soinfo_free(ptr);
7534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo* release() {
7554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        soinfo* result = ptr;
7564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        ptr = NULL;
7574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return result;
7584688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo* ptr;
7604688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes};
7611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// TODO: rewrite linker_phdr.h to use a class, then lose this.
7634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct phdr_ptr {
7644688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    phdr_ptr() : phdr_mmap(NULL) {}
7654688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ~phdr_ptr() {
7664688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (phdr_mmap != NULL) {
7674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            phdr_table_unload(phdr_mmap, phdr_size);
7684688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
7694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7704688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    void* phdr_mmap;
7714688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr phdr_size;
7724688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes};
773b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner
774124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic soinfo* load_library(const char* name) {
7754688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Open the file.
7764688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    scoped_fd fd;
7774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    fd.fd = open_library(name);
7784688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (fd.fd == -1) {
7794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("library \"%s\" not found", name);
7801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
7812e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    }
7821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7834688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Read the ELF header.
7844688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Ehdr header[1];
7854688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header)));
78623363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ret < 0) {
7874688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't read file \"%s\": %s", name, strerror(errno));
7884688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
78923363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
79023363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ret != (int)sizeof(header)) {
7914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("too small to be an ELF executable: %s", name);
7924688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
79323363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
79423363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (verify_elf_header(header) < 0) {
7954688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("not a valid ELF executable: %s", name);
7964688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
7971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7994688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Read the program header table.
8004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    const Elf32_Phdr* phdr_table;
8014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    phdr_ptr phdr_holder;
8024688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ret = phdr_table_load(fd.fd, header->e_phoff, header->e_phnum,
8034688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                          &phdr_holder.phdr_mmap, &phdr_holder.phdr_size, &phdr_table);
80423363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ret < 0) {
8054688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't load program header table: %s: %s", name, strerror(errno));
8064688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
8071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8084688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    size_t phdr_count = header->e_phnum;
8091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8104688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Get the load extents.
8114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
81261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("[ '%s' wants sz=0x%08x ]\n", name, ext_sz);
81323363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ext_sz == 0) {
8144688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("no loadable segments in file: %s", name);
8154688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
81623363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
81723363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner
8184688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // We no longer support pre-linked libraries.
8194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (is_prelinked(fd.fd, name)) {
8204688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
82123363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
82216084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner
8234688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Reserve address space for all loadable segments.
8244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    void* load_start = NULL;
8254688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr load_size = 0;
8264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr load_bias = 0;
827b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    ret = phdr_table_reserve_memory(phdr_table,
828b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    phdr_count,
829b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    &load_start,
830b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    &load_size,
831b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    &load_bias);
832b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (ret < 0) {
8334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't reserve %d bytes in address space for \"%s\": %s",
8344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               ext_sz, name, strerror(errno));
8354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
836b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
8371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
83861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("[ allocated memory for %s @ %p (0x%08x) ]\n", name, load_start, load_size);
839b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner
840b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* Map all the segments in our address space with default protections */
841b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    ret = phdr_table_load_segments(phdr_table,
842b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                   phdr_count,
843b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                   load_bias,
8444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                                   fd.fd);
845b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (ret < 0) {
8464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't map loadable segments for \"%s\": %s",
8474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               name, strerror(errno));
8484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
849b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
8501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo_ptr si(name);
8524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si.ptr == NULL) {
8534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
8541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->base = (Elf32_Addr) load_start;
8574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->size = load_size;
8584688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->load_bias = load_bias;
8594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->flags = 0;
8604688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->entry = 0;
8614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->dynamic = (unsigned *)-1;
8624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->phnum = phdr_count;
8634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
8644688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si.ptr->phdr == NULL) {
8654688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't find loaded PHDR for \"%s\"", name);
8664688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
86723363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
8684688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
8694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    return si.release();
8701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
872d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic soinfo* init_library(soinfo* si) {
873d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // At this point we know that whatever is loaded @ base is a valid ELF
874d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // shared library whose segments are properly mapped in.
87561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("[ init_library base=0x%08x sz=0x%08x name='%s') ]\n",
87661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        si->base, si->size, si->name);
8771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
878d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (!soinfo_link_image(si)) {
879d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    munmap((void *)si->base, si->size);
880d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return NULL;
881d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
8821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
883d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
8841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
88612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvelstatic soinfo *find_loaded_library(const char *name)
8871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
8881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
8896774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    const char *bname;
8906774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
89112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // TODO: don't use basename only for determining libraries
89212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // http://code.google.com/p/android/issues/detail?id=6670
89312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
89412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    bname = strrchr(name, '/');
89512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    bname = bname ? bname + 1 : name;
89612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
89712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    for(si = solist; si != NULL; si = si->next){
89812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        if(!strcmp(bname, si->name)) {
89912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            return si;
90012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        }
90112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    }
90212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    return NULL;
90312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel}
90412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
905d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic soinfo* find_library_internal(const char* name) {
906d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (name == NULL) {
907d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return somain;
908d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
909d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
910d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_loaded_library(name);
911d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
912d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_ERROR) {
913d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      DL_ERR("\"%s\" failed to load previously", name);
914d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      return NULL;
915d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    }
916d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_LINKED) {
917d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      return si;
918d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    }
919d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    DL_ERR("OOPS: recursive link to \"%s\"", si->name);
920d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return NULL;
921d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
92212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
92361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("[ '%s' has not been loaded yet.  Locating...]\n", name);
924d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  si = load_library(name);
925d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
926d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si = init_library(si);
927d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
9286774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
929d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
930d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
931d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
932d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic soinfo* find_library(const char* name) {
933d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_library_internal(name);
934d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
935d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->refcount++;
936d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
937d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
938d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
939d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
940d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic int soinfo_unload(soinfo* si) {
941d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si->refcount == 1) {
94261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("unloading '%s'\n", si->name);
943d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallDestructors();
944d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
945d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    for (unsigned* d = si->dynamic; *d; d += 2) {
946d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      if (d[0] == DT_NEEDED) {
947d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        soinfo* lsi = find_loaded_library(si->strtab + d[1]);
948d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        if (lsi != NULL) {
94961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes          TRACE("%s needs to unload %s\n", si->name, lsi->name);
950d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          soinfo_unload(lsi);
951d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        } else {
952d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          // TODO: should we return -1 in this case?
953d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          DL_ERR("\"%s\": could not unload dependent library", si->name);
9541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
955d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
9561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
9571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
958d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    munmap(reinterpret_cast<void*>(si->base), si->size);
959d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    notify_gdb_of_unload(si);
960d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    soinfo_free(si);
961d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->refcount = 0;
962d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  } else {
963d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->refcount--;
9649c94fc9fbefe55836c1e1b949850f7726e64c118Elliott Hughes    TRACE("not unloading '%s', decrementing refcount to %d\n", si->name, si->refcount);
965d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
966d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return 0;
9671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
9681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
969d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughessoinfo* do_dlopen(const char* name) {
970d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
971d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_library(name);
972d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
973d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
974d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
975d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ);
976d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
977d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
9781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
979d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesint do_dlclose(soinfo* si) {
980d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
981d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  int result = soinfo_unload(si);
982d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ);
983d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return result;
9841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
9851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* TODO: don't use unsigned for addrs below. It works, but is not
9871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
9881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * long.
9891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
99012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvelstatic int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count,
99112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                           soinfo *needed[])
9921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
9931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *symtab = si->symtab;
9941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const char *strtab = si->strtab;
9951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s;
9961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Rel *start = rel;
9975ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    soinfo *lsi;
9981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9994688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    for (size_t idx = 0; idx < count; ++idx, ++rel) {
10001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned type = ELF32_R_TYPE(rel->r_info);
10011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned sym = ELF32_R_SYM(rel->r_info);
1002bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner        unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
10031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned sym_addr = 0;
10041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        char *sym_name = NULL;
10051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
100661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DEBUG("Processing '%s' relocation at index %d\n", si->name, idx);
1007d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (type == 0) { // R_*_NONE
1008d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            continue;
1009d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
10101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if(sym != 0) {
1011d1b40d8c69dfca94185ea9231f6cb16f70c02e39Dima Zavin            sym_name = (char *)(strtab + symtab[sym].st_name);
10125ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1013e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            if(s == NULL) {
1014e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* We only allow an undefined symbol if this is a weak
1015e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   reference..   */
1016e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                s = &symtab[sym];
1017e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
1018e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes                    DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
1019e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1020e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1021e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1022e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* IHI0044C AAELF 4.5.1.1:
1023e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1024e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   Libraries are not searched to resolve weak references.
1025e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   It is not an error for a weak reference to remain
1026e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   unsatisfied.
1027e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1028e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   During linking, the value of an undefined weak reference is:
1029e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - Zero if the relocation type is absolute
1030e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - The address of the place if the relocation is pc-relative
1031bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes                   - The address of nominal base address if the relocation
1032e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                     type is base-relative.
1033e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                  */
1034e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1035e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                switch (type) {
1036e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_ARM_LINKER)
1037e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_JUMP_SLOT:
1038e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_GLOB_DAT:
1039e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_ABS32:
1040e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_RELATIVE:    /* Don't care. */
1041e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#elif defined(ANDROID_X86_LINKER)
1042d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                case R_386_JMP_SLOT:
1043e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_GLOB_DAT:
1044e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_32:
1045e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_RELATIVE:    /* Dont' care. */
1046e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_*_LINKER */
1047e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    /* sym_addr was initialized to be zero above or relocation
1048e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       code below does not care about value of sym_addr.
1049e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       No need to do anything.  */
1050e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
1051e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1052e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_X86_LINKER)
1053e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_PC32:
1054e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    sym_addr = reloc;
1055e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
1056e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_X86_LINKER */
1057e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1058e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_ARM_LINKER)
1059e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_COPY:
1060e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    /* Fall through.  Can't really copy if weak symbol is
1061e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       not found in run-time.  */
1062e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_ARM_LINKER */
1063e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                default:
10644688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("unknown weak reloc type %d @ %p (%d)",
10654688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                                 type, rel, (int) (rel - start));
1066e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1067e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1068e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            } else {
1069e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* We got a definition.  */
10701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0
1071d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                if((base == 0) && (si->base != 0)){
1072d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                        /* linking from libraries to main image is bad */
10734688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("cannot locate \"%s\"...",
10744688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                           strtab + symtab[sym].st_name);
1075d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                    return -1;
1076d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                }
10771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
10785ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                sym_addr = (unsigned)(s->st_value + lsi->load_bias);
1079d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1080bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocSymbol);
10811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        } else {
1082e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            s = NULL;
10831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
10841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
10851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* TODO: This is ugly. Split up the relocations by arch into
10861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * different files.
10871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
10881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(type){
10891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_ARM_LINKER)
10901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_JUMP_SLOT:
1091bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
10921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
109361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
10941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
10951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
10961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_GLOB_DAT:
1097bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
10981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
109961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
11001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
11011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_ABS32:
1103bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
11041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
110561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s\n", reloc, sym_addr, sym_name);
11061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) += sym_addr;
11071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
110834ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner        case R_ARM_REL32:
1109bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
111034ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            MARK(rel->r_offset);
111161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s\n",
111234ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner                       reloc, sym_addr, rel->r_offset, sym_name);
111334ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            *((unsigned*)reloc) += sym_addr - rel->r_offset;
111434ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            break;
11151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#elif defined(ANDROID_X86_LINKER)
1116d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case R_386_JMP_SLOT:
1117bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
11181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
111961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
11201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
11211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_GLOB_DAT:
1123bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
11241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
112561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
11261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
11271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1128d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_MIPS_LINKER)
1129d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    case R_MIPS_JUMP_SLOT:
1130bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
1131d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            MARK(rel->r_offset);
113261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
1133d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *((unsigned*)reloc) = sym_addr;
1134d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1135d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    case R_MIPS_REL32:
1136bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
1137d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            MARK(rel->r_offset);
113861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s\n",
1139d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                       reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1140d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            if (s) {
1141d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                *((unsigned*)reloc) += sym_addr;
1142d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            } else {
1143d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                *((unsigned*)reloc) += si->base;
1144d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1145d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
11461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_*_LINKER */
11471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_ARM_LINKER)
11491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_RELATIVE:
11501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#elif defined(ANDROID_X86_LINKER)
11511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_RELATIVE:
11521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_*_LINKER */
1153bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
11541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
11554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            if (sym) {
115661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                DL_ERR("odd RELATIVE form...");
11571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                return -1;
11581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
115961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO RELATIVE %08x <- +%08x\n", reloc, si->base);
11601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) += si->base;
11611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_X86_LINKER)
11641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_32:
1165bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
11661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
11671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
116861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s\n", reloc, sym_addr, sym_name);
11691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned *)reloc) += (unsigned)sym_addr;
11701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_PC32:
1173bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
11741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
117561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s\n",
117661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                       reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
11771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
11781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_X86_LINKER */
11801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef ANDROID_ARM_LINKER
11821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_COPY:
1183d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            if ((si->flags & FLAG_EXE) == 0) {
1184d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                /*
1185d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1186d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 *
1187d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * Section 4.7.1.10 "Dynamic relocations"
1188d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * R_ARM_COPY may only appear in executable objects where e_type is
1189d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * set to ET_EXEC.
1190d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 *
1191d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1192d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * We should explicitly disallow ET_DYN executables from having
1193d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * R_ARM_COPY relocations.
1194d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 */
1195d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1196d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                return -1;
1197d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            }
1198bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocCopy);
11991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
120061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s\n", reloc, s->st_size, sym_addr, sym_name);
1201d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            if (reloc == sym_addr) {
12025ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
12035ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
12045ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (src == NULL) {
12055ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
12065ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
12075ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
12085ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (lsi->has_DT_SYMBOLIC) {
12095ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
12105ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                           "library %s (built with -Bsymbolic?)", si->name, lsi->name);
12115ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
12125ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
12135ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (s->st_size < src->st_size) {
12145ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
12155ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                           si->name, s->st_size, src->st_size);
12165ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
12175ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
12185ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
12195ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            } else {
12205ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
1221d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                return -1;
1222d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            }
12231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
12241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_ARM_LINKER */
12251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        default:
12274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("unknown reloc type %d @ %p (%d)",
12284688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                   type, rel, (int) (rel - start));
12291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return -1;
12301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
12311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
12321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
12331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
12341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1235d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#ifdef ANDROID_MIPS_LINKER
1236bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic int mips_relocate_got(soinfo* si, soinfo* needed[]) {
1237d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned *got;
1238d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned local_gotno, gotsym, symtabno;
1239d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    Elf32_Sym *symtab, *sym;
1240d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned g;
1241d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1242d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got;
1243d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    local_gotno = si->mips_local_gotno;
1244d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    gotsym = si->mips_gotsym;
1245d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    symtabno = si->mips_symtabno;
1246d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    symtab = si->symtab;
1247d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1248d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    /*
1249d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * got[0] is address of lazy resolver function
1250d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * got[1] may be used for a GNU extension
1251bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes     * set it to a recognizable address in case someone calls it
1252d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * (should be _rtld_bind_start)
1253d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * FIXME: maybe this should be in a separate routine
1254d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     */
1255d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1256d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if ((si->flags & FLAG_LINKER) == 0) {
1257d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        g = 0;
1258d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        got[g++] = 0xdeadbeef;
1259d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (got[g] & 0x80000000) {
1260d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            got[g++] = 0xdeadfeed;
1261d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1262d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        /*
1263d7daacb46372132ae3f0121647074936c304b572Raghu Gandham         * Relocate the local GOT entries need to be relocated
1264d7daacb46372132ae3f0121647074936c304b572Raghu Gandham         */
1265d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        for (; g < local_gotno; g++) {
1266d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            got[g] += si->load_bias;
1267d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1268d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1269d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1270d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    /* Now for the global GOT entries */
1271d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    sym = symtab + gotsym;
1272d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got + local_gotno;
1273d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    for (g = gotsym; g < symtabno; g++, sym++, got++) {
1274d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        const char *sym_name;
1275d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        Elf32_Sym *s;
12765ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        soinfo *lsi;
1277d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1278d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        /* This is an undefined reference... try to locate it */
1279d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        sym_name = si->strtab + sym->st_name;
12805ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1281d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (s == NULL) {
1282d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* We only allow an undefined symbol if this is a weak
1283d7daacb46372132ae3f0121647074936c304b572Raghu Gandham               reference..   */
1284d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            s = &symtab[g];
1285d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
12864688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("cannot locate \"%s\"...", sym_name);
1287d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                return -1;
1288d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1289d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *got = 0;
1290d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1291d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        else {
1292d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* FIXME: is this sufficient?
1293d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             * For reference see NetBSD link loader
1294d7daacb46372132ae3f0121647074936c304b572Raghu 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
1295d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             */
12965ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel             *got = lsi->load_bias + s->st_value;
1297d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1298d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1299d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    return 0;
1300d7daacb46372132ae3f0121647074936c304b572Raghu Gandham}
1301d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1302d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
13038215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner/* Please read the "Initialization and Termination functions" functions.
13048215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * of the linker design note in bionic/linker/README.TXT to understand
13058215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * what the following code is doing.
13068215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
13078215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * The important things to remember are:
13088215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
13098215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_PREINIT_ARRAY must be called first for executables, and should
13108215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   not appear in shared libraries.
13118215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
13128215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_INIT should be called before DT_INIT_ARRAY if both are present
13138215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
13148215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_FINI should be called after DT_FINI_ARRAY if both are present
13158215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
13168215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_FINI_ARRAY must be parsed in reverse order.
13178215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner */
1318d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallArray(const char* array_name UNUSED, unsigned* array, int count, bool reverse) {
1319d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (array == NULL) {
1320d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1321d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
13228215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1323d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  int step = 1;
1324d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (reverse) {
1325d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    array += (count-1);
1326d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    step = -1;
1327d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
13288215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
132961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("[ Calling %s @ %p [%d] for '%s' ]\n", array_name, array, count, name);
13308215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1331d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  for (int n = count; n > 0; n--) {
133261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("[ Looking at %s[%d] *%p == 0x%08x ]\n", array_name, n, array, *array);
1333d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    void (*func)() = (void (*)()) *array;
1334d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    array += step;
1335d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (((int) func == 0) || ((int) func == -1)) {
1336d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      continue;
13371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
133861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("[ Calling func @ %p ]\n", func);
1339d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    func();
1340d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1341d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
134261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("[ Done calling %s for '%s' ]\n", array_name, name);
13431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
13441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1345d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallFunction(const char* function_name UNUSED, void (*function)()) {
1346d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (function == NULL) {
1347d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1348d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1349d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
135061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("[ Calling %s @ %p for '%s' ]\n", function_name, function, name);
1351d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  function();
135261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("[ Done calling %s for '%s' ]\n", function_name, name);
13539181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov}
13549181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
1355d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallPreInitConstructors() {
1356d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1357d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
1358e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1359d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallConstructors() {
1360d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (constructors_called) {
1361d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1362d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1363e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1364d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // We set constructors_called before actually calling the constructors, otherwise it doesn't
1365d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // protect against recursive constructor calls. One simple example of constructor recursion
1366d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1367d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 1. The program depends on libc, so libc's constructor is called here.
1368d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1369d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 3. dlopen() calls the constructors on the newly created
1370d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    soinfo for libc_malloc_debug_leak.so.
1371d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 4. The debug .so depends on libc, so CallConstructors is
1372d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    called again with the libc soinfo. If it doesn't trigger the early-
1373d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    out above, the libc constructor will be called again (recursively!).
1374d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  constructors_called = true;
1375d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1376d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (!(flags & FLAG_EXE) && preinit_array) {
1377d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    DL_ERR("shared library \"%s\" has a preinit_array table @ %p", name, preinit_array);
1378d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1379d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
13801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1381d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (dynamic) {
1382d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    for (unsigned* d = dynamic; *d; d += 2) {
1383d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      if (d[0] == DT_NEEDED) {
1384d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        soinfo* lsi = find_loaded_library(strtab + d[1]);
1385d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        if (lsi == NULL) {
1386d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          DL_ERR("\"%s\": could not initialize dependent library", name);
1387d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        } else {
1388d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          lsi->CallConstructors();
1389d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        }
1390d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
13911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1392d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
13931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1394d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_INIT", init_func);
1395d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
1396e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov}
13978215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1398d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallDestructors() {
1399d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
1400d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_FINI", fini_func);
14011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with
14041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   /dev/null. */
14055419b9474753d25dff947c7740532f86d130c0beElliott Hughesstatic int nullify_closed_stdio() {
14061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int dev_null, i, status;
14071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int return_value = 0;
14081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
140916084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
14101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null < 0) {
14114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("cannot open /dev/null: %s", strerror(errno));
14121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return -1;
14131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
141461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("[ Opened /dev/null file-descriptor=%d]\n", dev_null);
14151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If any of the stdio file descriptors is valid and not associated
14171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       with /dev/null, dup /dev/null to it.  */
14181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (i = 0; i < 3; i++) {
14191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* If it is /dev/null already, we are done. */
14204688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (i == dev_null) {
14211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
14224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
14231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
142461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        TRACE("[ Nullifying stdio file descriptor %d]\n", i);
14254688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
14261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        /* If file is opened, we are good. */
14284688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status != -1) {
14294688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            continue;
14304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
14311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* The only error we allow is that the file descriptor does not
14331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           exist, in which case we dup /dev/null to it. */
14341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (errno != EBADF) {
14354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("fcntl failed: %s", strerror(errno));
14361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
14381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* Try dupping /dev/null to this stdio file descriptor and
14411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           repeat if there is a signal.  Note that any errors in closing
14421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           the stdio descriptor are lost.  */
14434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
14441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (status < 0) {
14454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("dup2 failed: %s", strerror(errno));
14461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
14481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If /dev/null is not one of the stdio file descriptors, close it. */
14521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null > 2) {
145361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        TRACE("[ Closing /dev/null file-descriptor=%d]\n", dev_null);
14544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(close(dev_null));
14554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status == -1) {
14564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("close failed: %s", strerror(errno));
14571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return return_value;
14621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1464124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic bool soinfo_link_image(soinfo* si) {
1465124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    si->flags |= FLAG_ERROR;
1466124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes
1467f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee    /* "base" might wrap around UINT32_MAX. */
1468bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    Elf32_Addr base = si->load_bias;
1469b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    const Elf32_Phdr *phdr = si->phdr;
14701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int phnum = si->phnum;
1471b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    int relocating_linker = (si->flags & FLAG_LINKER) != 0;
147212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    soinfo **needed, **pneeded;
14731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1474b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can't debug anything until the linker is relocated */
1475b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (!relocating_linker) {
147661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        INFO("[ linking %s ]\n", si->name);
147761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DEBUG("si->base = 0x%08x si->flags = 0x%08x\n", si->base, si->flags);
1478b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
14791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
148063f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    /* Extract dynamic section */
1481124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    size_t dynamic_count;
148212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
148312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                                   &dynamic_count);
148463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    if (si->dynamic == NULL) {
1485b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1486124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
1487b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
1488124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
148963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    } else {
1490b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
149161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("dynamic = %p\n", si->dynamic);
1492b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
149363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    }
149463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
149563f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#ifdef ANDROID_ARM_LINKER
149663f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    (void) phdr_table_get_arm_exidx(phdr, phnum, base,
149763f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner                                    &si->ARM_exidx, &si->ARM_exidx_count);
149863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif
149963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
15001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* extract useful information from dynamic section */
1501124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    for (unsigned* d = si->dynamic; *d; ++d) {
150261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DEBUG("d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", d, d[0], d[1]);
15031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(*d++){
15041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_HASH:
1505f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->nbucket = ((unsigned *) (base + *d))[0];
1506f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->nchain = ((unsigned *) (base + *d))[1];
1507f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->bucket = (unsigned *) (base + *d + 8);
1508f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
15091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_STRTAB:
1511f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->strtab = (const char *) (base + *d);
15121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_SYMTAB:
1514f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->symtab = (Elf32_Sym *) (base + *d);
15151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTREL:
15171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if(*d != DT_REL) {
1518124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1519124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
15201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
15211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_JMPREL:
1523f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->plt_rel = (Elf32_Rel*) (base + *d);
15241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTRELSZ:
15261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->plt_rel_count = *d / 8;
15271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_REL:
1529f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->rel = (Elf32_Rel*) (base + *d);
15301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_RELSZ:
15321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->rel_count = *d / 8;
15331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTGOT:
15351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            /* Save this in case we decide to do lazy binding. We don't yet. */
1536f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->plt_got = (unsigned *)(base + *d);
15371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_DEBUG:
1539d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if !defined(ANDROID_MIPS_LINKER)
1540bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_DEBUG entry to the address of _r_debug for GDB
15411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *d = (int) &_r_debug;
1542d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
15431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1544ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI         case DT_RELA:
1545124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1546124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
15471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT:
1548f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->init_func = (void (*)(void))(base + *d);
154961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("%s constructors (init func) found at %p\n", si->name, si->init_func);
15501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI:
1552f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->fini_func = (void (*)(void))(base + *d);
155361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("%s destructors (fini func) found at %p\n", si->name, si->fini_func);
15541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAY:
1556f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->init_array = (unsigned *)(base + *d);
155761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("%s constructors (init_array) found at %p\n", si->name, si->init_array);
15581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAYSZ:
15601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
15611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAY:
1563f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->fini_array = (unsigned *)(base + *d);
156461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("%s destructors (fini_array) found at %p\n", si->name, si->fini_array);
15651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAYSZ:
15671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
15681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAY:
1570f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->preinit_array = (unsigned *)(base + *d);
157161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("%s constructors (preinit_array) found at %p\n", si->name, si->preinit_array);
15721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAYSZ:
15741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
15751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_TEXTREL:
15775135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            si->has_text_relocations = true;
15781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15795ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_SYMBOLIC:
15805ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            si->has_DT_SYMBOLIC = true;
15815ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
15825ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel#if defined(DT_FLAGS)
15835ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_FLAGS:
15845ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (*d & DF_TEXTREL) {
15855ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_text_relocations = true;
15865ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
15875ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (*d & DF_SYMBOLIC) {
15885ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_DT_SYMBOLIC = true;
15895ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
15905ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
15915ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel#endif
1592d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if defined(ANDROID_MIPS_LINKER)
1593d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_NEEDED:
1594d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_STRSZ:
1595d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_SYMENT:
1596d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_RELENT:
1597d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             break;
1598d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_MAP:
1599bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
1600d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            {
1601bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes              r_debug** dp = (r_debug**) *d;
1602d7daacb46372132ae3f0121647074936c304b572Raghu Gandham              *dp = &_r_debug;
1603d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1604d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1605d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_VERSION:
1606d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_FLAGS:
1607d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_BASE_ADDRESS:
1608d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_UNREFEXTNO:
1609d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RWPLT:
1610d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1611d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1612d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_PLTGOT:
1613d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if 0
1614d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* not yet... */
1615d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_pltgot = (unsigned *)(si->base + *d);
1616d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1617d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1618d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1619d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_SYMTABNO:
1620d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_symtabno = *d;
1621d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1622d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1623d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_LOCAL_GOTNO:
1624d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_local_gotno = *d;
1625d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1626d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1627d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_GOTSYM:
1628d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_gotsym = *d;
1629d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1630d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1631d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        default:
163261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("Unused DT entry: type 0x%08x arg 0x%08x\n", d[-1], d[0]);
1633d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1634d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
16351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
16361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
163861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    DEBUG("si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
163961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes          si->base, si->strtab, si->symtab);
16401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1641124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    // Sanity checks.
1642124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->nbucket == 0) {
1643124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1644124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1645124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1646124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->strtab == 0) {
1647124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1648124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1649124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1650124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->symtab == 0) {
1651124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1652124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
16531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16554fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* if this is the main executable, then load all of the preloads now */
1656d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_EXE) {
16574fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        memset(preloads, 0, sizeof(preloads));
1658d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1659d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            soinfo* lsi = find_library(gLdPreloadNames[i]);
1660d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            if (lsi == NULL) {
16614fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer                strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
16624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1663124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                       gLdPreloadNames[i], si->name, tmp_err_buf);
1664124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
16654fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            }
16664fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            preloads[i] = lsi;
16674fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        }
16684fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
16694fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
167012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    /* dynamic_count is an upper bound for the number of needed libs */
167112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    pneeded = needed = (soinfo**) alloca((1 + dynamic_count) * sizeof(soinfo*));
167212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
1673124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    for (unsigned* d = si->dynamic; *d; d += 2) {
1674d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        if (d[0] == DT_NEEDED) {
167561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("%s needs %s\n", si->name, si->strtab + d[1]);
1676d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            soinfo* lsi = find_library(si->strtab + d[1]);
1677d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            if (lsi == NULL) {
16780353195f344666256dba474a15c9ba22cf0cccc9Dima Zavin                strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
16794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
16804688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                       si->strtab + d[1], si->name, tmp_err_buf);
1681124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
16821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
168312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            *pneeded++ = lsi;
16841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
16851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
168612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    *pneeded = NULL;
16871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16885135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
16895135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        /* Unprotect the segments, i.e. make them writable, to allow
16905135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * text relocations to work properly. We will later call
16915135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * phdr_table_protect_segments() after all of them are applied
16925135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * and all constructors are run.
16935135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         */
16945135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
16955135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't unprotect loadable segments for \"%s\": %s",
16965135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1697124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
16985135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
16995135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    }
17005135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich
1701124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->plt_rel) {
170261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DEBUG("[ relocating %s plt ]\n", si->name );
1703124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1704124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1705124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
17061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1707124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->rel) {
170861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DEBUG("[ relocating %s ]\n", si->name );
1709124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        if(soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1710124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1711124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
17121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
17131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1714d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#ifdef ANDROID_MIPS_LINKER
1715124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (mips_relocate_got(si, needed)) {
1716124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1717d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1718d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1719d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
17201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_LINKED;
172161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    DEBUG("[ finished linking %s ]\n", si->name);
17221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17235135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
17245135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        /* All relocations are done, we can protect our segments back to
17255135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * read-only. */
17265135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
17275135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't protect segments for \"%s\": %s",
17285135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1729124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
17305135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
17311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
17321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1733b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can also turn on GNU RELRO protection */
1734b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
17354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
17364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               si->name, strerror(errno));
1737124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
17389ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich    }
17399ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
1740124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    // If this is a setuid/setgid program, close the security hole described in
1741124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
174218a206c81d9743481e364384affd43306911283dElliott Hughes    if (get_AT_SECURE()) {
17434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        nullify_closed_stdio();
17444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
17451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    notify_gdb_of_load(si);
1746124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    si->flags &= ~FLAG_ERROR;
1747124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    return true;
17481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
17491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void parse_path(const char* path, const char* delimiters,
17514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                       const char** array, char* buf, size_t buf_size, size_t max_count)
1752bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley{
17534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (path == NULL) {
17544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return;
17554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
1756bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
17574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    size_t len = strlcpy(buf, path, buf_size);
1758bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
17594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    size_t i = 0;
17604688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    char* buf_p = buf;
17614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
17624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (*array[i] != '\0') {
1763bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley            ++i;
17644688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
1765bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    }
1766bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
17674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Forget the last path if we had to truncate; this occurs if the 2nd to
17684688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // last char isn't '\0' (i.e. wasn't originally a delimiter).
17694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
17704688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        array[i - 1] = NULL;
1771bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    } else {
17724688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        array[i] = NULL;
1773bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley    }
1774bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley}
1775bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
17764688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void parse_LD_LIBRARY_PATH(const char* path) {
1777124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    parse_path(path, ":", gLdPaths,
1778124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes               gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
17794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes}
17804fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
17814688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void parse_LD_PRELOAD(const char* path) {
17824688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // We have historically supported ':' as well as ' ' in LD_PRELOAD.
1783124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    parse_path(path, " :", gLdPreloadNames,
1784124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes               gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
17854fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer}
17864fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1787468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1788468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and
1789468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs
1790468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point.
1791468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
1792bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissellstatic unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base)
17931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
17941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    static soinfo linker_soinfo;
17951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int argc = (int) *elfdata;
17971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    char **argv = (char**) (elfdata + 1);
1798bb44055d0a3515410f45417d5b0e047cc60f0dc6Stephen Smalley    unsigned *vecs = (unsigned*) (argv + argc + 1);
17991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1800ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner    /* NOTE: we store the elfdata pointer on a special location
1801ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       of the temporary TLS area in order to pass it to
1802ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       the C Library's runtime initializer.
1803ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *
1804ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       The initializer must clear the slot and reset the TLS
1805ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       to point to a different location to ensure that no other
1806ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       shared library constructor can access it.
1807ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     */
18081a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    __libc_init_tls(elfdata);
18091a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov
18101a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING
18111a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    struct timeval t0, t1;
18121a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    gettimeofday(&t0, 0);
18131a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif
18141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
181518a206c81d9743481e364384affd43306911283dElliott Hughes    // Initialize environment functions, and get to the ELF aux vectors table.
1816be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    vecs = linker_env_init(vecs);
1817be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
18181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    debugger_init();
18191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
182018a206c81d9743481e364384affd43306911283dElliott Hughes    // Get a few environment variables.
182161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    const char* LD_DEBUG = linker_env_get("LD_DEBUG");
182261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    if (LD_DEBUG != NULL) {
182361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes      debug_verbosity = atoi(LD_DEBUG);
182418a206c81d9743481e364384affd43306911283dElliott Hughes    }
1825be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
182618a206c81d9743481e364384affd43306911283dElliott Hughes    // Normally, these are cleaned by linker_env_init, but the test
182718a206c81d9743481e364384affd43306911283dElliott Hughes    // doesn't cost us anything.
182818a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpath_env = NULL;
182918a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpreload_env = NULL;
183018a206c81d9743481e364384affd43306911283dElliott Hughes    if (!get_AT_SECURE()) {
183118a206c81d9743481e364384affd43306911283dElliott Hughes      ldpath_env = linker_env_get("LD_LIBRARY_PATH");
183218a206c81d9743481e364384affd43306911283dElliott Hughes      ldpreload_env = linker_env_get("LD_PRELOAD");
18331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    INFO("[ android linker & debugger ]\n");
183661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    DEBUG("elfdata @ 0x%08x\n", (unsigned)elfdata);
18371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
183818a206c81d9743481e364384affd43306911283dElliott Hughes    soinfo* si = soinfo_alloc(argv[0]);
183918a206c81d9743481e364384affd43306911283dElliott Hughes    if (si == NULL) {
184018a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
18411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1843d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    /* bootstrap the link map, the main exe always needs to be first */
18441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_EXE;
1845bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    link_map* map = &(si->linkmap);
18461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = 0;
18481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_name = argv[0];
18491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_prev = NULL;
18501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_next = NULL;
18511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_map = map;
18531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
18541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1855bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell        /* gdb expects the linker to be in the debug shared object list.
1856bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * Without this, gdb has trouble locating the linker's ".text"
1857bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * and ".plt" sections. Gdb could also potentially use this to
1858bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1859bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * Don't use soinfo_alloc(), because the linker shouldn't
18601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         * be on the soinfo list.
18611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         */
1862be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
18631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    linker_soinfo.flags = 0;
1864bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell    linker_soinfo.base = linker_base;
186506f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng    /*
186606f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     * Set the dynamic field in the link map otherwise gdb will complain with
186706f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     * the following:
186806f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     *   warning: .dynamic section for "/system/bin/linker" is not at the
186906f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     *   expected address (wrong library or version mismatch?)
187006f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     */
187106f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng    Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
187206f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng    Elf32_Phdr *phdr =
187306f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng        (Elf32_Phdr *)((unsigned char *) linker_base + elf_hdr->e_phoff);
187412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
187512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                                   &linker_soinfo.dynamic, NULL);
18761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(&linker_soinfo);
18771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1878d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    /* extract information passed from the kernel */
187918a206c81d9743481e364384affd43306911283dElliott Hughes    while (vecs[0] != 0){
18801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(vecs[0]){
18811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case AT_PHDR:
18821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->phdr = (Elf32_Phdr*) vecs[1];
18831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case AT_PHNUM:
18851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->phnum = (int) vecs[1];
18861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case AT_ENTRY:
18881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->entry = vecs[1];
18891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
18911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        vecs += 2;
18921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18948180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    /* Compute the value of si->base. We can't rely on the fact that
18958180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * the first entry is the PHDR because this will not be true
18968180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * for certain executables (e.g. some in the NDK unit test suite)
18978180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     */
18988180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    int nn;
18998180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    si->base = 0;
1900b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    si->size = phdr_table_get_load_size(si->phdr, si->phnum);
1901bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    si->load_bias = 0;
19028180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    for ( nn = 0; nn < si->phnum; nn++ ) {
19038180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner        if (si->phdr[nn].p_type == PT_PHDR) {
1904bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner            si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr;
1905bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner            si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset;
19068180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner            break;
19078180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner        }
19088180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    }
19091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->dynamic = (unsigned *)-1;
19106774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    si->refcount = 1;
19111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19124688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
19134688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_LIBRARY_PATH(ldpath_env);
19144688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_PRELOAD(ldpreload_env);
19154fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
19165ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    somain = si;
19175ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
1918124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (!soinfo_link_image(si)) {
19192e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin        char errmsg[] = "CANNOT LINK EXECUTABLE\n";
19202e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin        write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
19212e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin        write(2, errmsg, sizeof(errmsg));
192218a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
19231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
19241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1925d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallPreInitConstructors();
19269181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
192718a206c81d9743481e364384affd43306911283dElliott Hughes    for (size_t i = 0; preloads[i] != NULL; ++i) {
1928d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        preloads[i]->CallConstructors();
1929326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng    }
1930326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng
19319c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin    /*After the link_image, the si->base is initialized.
19329c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     *For so lib, the map->l_addr will be updated in notify_gdb_of_load.
19339c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     *We need to update this value for so exe here. So Unwind_Backtrace
19349c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     *for some arch like x86 could work correctly within so exe.
19359c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     */
19369c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin    map->l_addr = si->base;
1937d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
1938e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
19391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING
19401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    gettimeofday(&t1,NULL);
19411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
19421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
19431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
19441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               ));
19451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
19471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
1948bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocAbsolute],
1949bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocRelative],
1950bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocCopy],
1951bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocSymbol]);
19521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
19541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
19551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned n;
19561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned i;
19571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned count = 0;
19581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        for(n = 0; n < 4096; n++){
19591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if(bitmask[n]){
19601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                unsigned x = bitmask[n];
19611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                for(i = 0; i < 8; i++){
19621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    if(x & 1) count++;
19631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    x >>= 1;
19641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                }
19651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
19661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
19671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
19681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
19691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES
19721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    fflush(stdout);
19731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
197561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("[ Ready to execute '%s' @ 0x%08x ]\n", si->name, si->entry);
19761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si->entry;
19771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
1978468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1979468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1980468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Find the value of AT_BASE passed to us by the kernel. This is the load
1981468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * location of the linker.
1982468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
1983468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevichstatic unsigned find_linker_base(unsigned **elfdata) {
1984468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    int argc = (int) *elfdata;
1985468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    char **argv = (char**) (elfdata + 1);
1986468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    unsigned *vecs = (unsigned*) (argv + argc + 1);
1987468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    while (vecs[0] != 0) {
1988468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        vecs++;
1989468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    }
1990468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1991468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    /* The end of the environment block is marked by two NULL pointers */
1992468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    vecs++;
1993468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1994468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    while(vecs[0]) {
1995468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if (vecs[0] == AT_BASE) {
1996468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            return vecs[1];
1997468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        }
1998468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        vecs += 2;
1999468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    }
2000468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2001468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    return 0; // should never happen
2002468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
2003468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2004bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only
2005bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library
2006bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself.
2007bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *
2008bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input:
2009bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    elf    -> address of ELF header, assumed to be at the start of the file.
2010bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return:
2011bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    load bias, i.e. add the value of any p_vaddr in the file to get
2012bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    the corresponding address in memory.
2013bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */
2014bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turnerstatic Elf32_Addr
2015bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turnerget_elf_exec_load_bias(const Elf32_Ehdr* elf)
2016bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner{
2017bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    Elf32_Addr        offset     = elf->e_phoff;
2018bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
2019bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    const Elf32_Phdr* phdr_end   = phdr_table + elf->e_phnum;
2020bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    const Elf32_Phdr* phdr;
2021bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
2022bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    for (phdr = phdr_table; phdr < phdr_end; phdr++) {
2023bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner        if (phdr->p_type == PT_LOAD) {
2024bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner            return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
2025bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner        }
2026bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    }
2027bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    return 0;
2028bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner}
2029bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
2030468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
2031468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This
2032468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and
2033468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation().
2034468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich *
2035468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own
2036468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern
2037468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault.
2038468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
20394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesextern "C" unsigned __linker_init(unsigned **elfdata) {
2040468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    unsigned linker_addr = find_linker_base(elfdata);
2041468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
2042468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    Elf32_Phdr *phdr =
2043468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
2044468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2045468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    soinfo linker_so;
2046468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    memset(&linker_so, 0, sizeof(soinfo));
2047468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2048468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.base = linker_addr;
2049b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2050bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
2051468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.dynamic = (unsigned *) -1;
2052468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.phdr = phdr;
2053468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.phnum = elf_hdr->e_phnum;
2054468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.flags |= FLAG_LINKER;
2055468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2056124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (!soinfo_link_image(&linker_so)) {
2057468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // It would be nice to print an error message, but if the linker
2058468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // can't link itself, there's no guarantee that we'll be able to
2059468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // call write() (because it involves a GOT reference).
2060468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        //
2061468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // This situation should never occur unless the linker itself
2062468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // is corrupt.
206318a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
2064468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    }
2065468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2066468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    // We have successfully fixed our own relocations. It's safe to run
2067468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    // the main part of the linker now.
20685419b9474753d25dff947c7740532f86d130c0beElliott Hughes    unsigned start_address = __linker_init_post_relocation(elfdata, linker_addr);
20695419b9474753d25dff947c7740532f86d130c0beElliott Hughes
2070d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    set_soinfo_pool_protection(PROT_READ);
2071d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
20725419b9474753d25dff947c7740532f86d130c0beElliott Hughes    // Return the address that the calling assembly stub should jump to.
20735419b9474753d25dff947c7740532f86d130c0beElliott Hughes    return start_address;
2074468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
2075