linker.cpp revision cade4c36e7c9c62db3f476a0f9cfc329bac9acb7
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);            \
14920a24403bc333cd92996cd43cef2e6a79c72d055Chris Dearman        write(2, msg, strlen(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
354cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
355cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_path(const char* path, const char* delimiters,
356cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes                       const char** array, char* buf, size_t buf_size, size_t max_count) {
357cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (path == NULL) {
358cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return;
359cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
360cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
361cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t len = strlcpy(buf, path, buf_size);
362cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
363cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t i = 0;
364cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  char* buf_p = buf;
365cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
366cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (*array[i] != '\0') {
367cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      ++i;
368cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    }
369cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
370cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
371cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // Forget the last path if we had to truncate; this occurs if the 2nd to
372cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // last char isn't '\0' (i.e. wasn't originally a delimiter).
373cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
374cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    array[i - 1] = NULL;
375cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  } else {
376cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    array[i] = NULL;
377cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
378cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
379cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
380cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_LIBRARY_PATH(const char* path) {
381cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  parse_path(path, ":", gLdPaths,
382cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes             gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
383cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
384cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
385cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_PRELOAD(const char* path) {
386cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // We have historically supported ':' as well as ' ' in LD_PRELOAD.
387cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  parse_path(path, " :", gLdPreloadNames,
388cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes             gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
389cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
390cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
3914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#ifdef ANDROID_ARM_LINKER
3924688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* For a given PC, find the .so that it belongs to.
3941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the base address of the .ARM.exidx section
3951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for that .so, and the number of 8-byte entries
3961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in that section (via *pcount).
3971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Intended to be called by libc's __gnu_Unwind_Find_exidx().
3991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4003b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes * This function is exposed via dlfcn.cpp and libdl.so.
4011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
4021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
4031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
4051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned addr = (unsigned)pc;
4061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
407468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    for (si = solist; si != 0; si = si->next){
408468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if ((addr >= si->base) && (addr < (si->base + si->size))) {
409468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            *pcount = si->ARM_exidx_count;
410f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            return (_Unwind_Ptr)si->ARM_exidx;
4111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
4121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   *pcount = 0;
4141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return NULL;
4151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4164688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
417d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER)
4184688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
4191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Here, we only have to provide a callback to iterate across all the
4201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * loaded libraries. gcc_eh does the rest. */
4211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectint
422bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesdl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
4231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                void *data)
4241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int rv = 0;
426bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    for (soinfo* si = solist; si != NULL; si = si->next) {
427bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        dl_phdr_info dl_info;
4281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_addr = si->linkmap.l_addr;
4291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_name = si->linkmap.l_name;
4301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phdr = si->phdr;
4311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phnum = si->phnum;
432bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        rv = cb(&dl_info, sizeof(dl_phdr_info), data);
433bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        if (rv != 0) {
4341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
435bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        }
4361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return rv;
4381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
4401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
4411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
44216084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerstatic Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name)
4431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s;
4451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *symtab = si->symtab;
4461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const char *strtab = si->strtab;
4471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned n;
4481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
44961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE_TYPE(LOOKUP, "SEARCH %s in %s@0x%08x %08x %d\n",
4501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               name, si->name, si->base, hash, hash % si->nbucket);
4511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    n = hash % si->nbucket;
4521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){
4541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        s = symtab + n;
4551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if(strcmp(strtab + s->st_name, name)) continue;
4561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
457e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            /* only concern ourselves with global and weak symbol definitions */
4581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(ELF32_ST_BIND(s->st_info)){
4591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case STB_GLOBAL:
460e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan        case STB_WEAK:
461439fa8ea8c33a7945258dfe84a71ab2665efaa9aRobin Burchell            if(s->st_shndx == SHN_UNDEF)
462439fa8ea8c33a7945258dfe84a71ab2665efaa9aRobin Burchell                continue;
4631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
46461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(LOOKUP, "FOUND %s in %s (%08x) %d\n",
4651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                       name, si->name, s->st_value, s->st_size);
4661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return s;
4671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
4681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
470943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
4711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned elfhash(const char *_name)
4741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const unsigned char *name = (const unsigned char *) _name;
4761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned h = 0, g;
4771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    while(*name) {
4791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h = (h << 4) + *name++;
4801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        g = h & 0xf0000000;
4811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g;
4821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g >> 24;
4831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return h;
4851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic Elf32_Sym *
4885ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvelsoinfo_do_lookup(soinfo *si, const char *name, soinfo **lsi,
4895ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                 soinfo *needed[])
4906ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev{
491943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
492d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    Elf32_Sym *s = NULL;
4934fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    int i;
4946ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
495c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin    if (si != NULL && somain != NULL) {
4965ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
4975ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        /*
498c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * Local scope is executable scope. Just start looking into it right away
499c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * for the shortcut.
5005ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel         */
5015ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
502c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        if (si == somain) {
503c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
5045ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (s != NULL) {
505c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
5065ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                goto done;
5075ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
508c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        } else {
509c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
510c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
511c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
512c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with symbolic relocations disabled, the
513c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * first place to look to resolve external references is the main
514c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * executable.
515c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
516c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
517c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (!si->has_DT_SYMBOLIC) {
51861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                DEBUG("%s: looking up %s in executable %s\n",
51961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
520c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
521c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
522c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
523c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
524c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
525c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5265ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
527c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Look for symbols in the local scope (the object who is
528c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * searching). This happens with C++ templates on i386 for some
529c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * reason.
530c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             *
531c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Notes on weak symbols:
532c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * The ELF specs are ambiguous about treatment of weak definitions in
533c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * dynamic linking.  Some systems return the first definition found
534c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * and some the first non-weak definition.   This is system dependent.
535c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Here we return the first definition found for simplicity.  */
536c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
537c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
538c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (s != NULL) {
539c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
540c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                goto done;
541c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
542c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
543c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
544c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with -Bsymbolic and symbol is not found
545c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * in the local scope, try to find the symbol in the main executable.
546c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
547c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
548c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (si->has_DT_SYMBOLIC) {
54961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                DEBUG("%s: looking up %s in executable %s after local scope\n",
55061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
551c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
552c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
553c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
554c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
555c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
556c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5575ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
558d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    }
5596ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5604fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* Next, look for it in the preloads list */
5614fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    for(i = 0; preloads[i] != NULL; i++) {
5625ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_elf_lookup(preloads[i], elf_hash, name);
5635ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if(s != NULL) {
5645ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            *lsi = preloads[i];
5654fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            goto done;
5665ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5674fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
5684fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
56912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    for(i = 0; needed[i] != NULL; i++) {
57061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DEBUG("%s: looking up %s in %s\n",
57161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes              si->name, name, needed[i]->name);
5725ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_elf_lookup(needed[i], elf_hash, name);
5735ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if (s != NULL) {
5745ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            *lsi = needed[i];
57512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            goto done;
5765ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5776ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5786ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5796ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevdone:
5806ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    if(s != NULL) {
58161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = 0x%08x, "
582bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner                   "found in %s, base = 0x%08x, load bias = 0x%08x\n",
58361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                   si->name, name, s->st_value,
5845ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                   (*lsi)->name, (*lsi)->base, (*lsi)->load_bias);
5856ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        return s;
5866ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5876ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
588943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
5896ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev}
5906ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5916ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev/* This is used by dl_sym().  It performs symbol lookup only within the
5926ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev   specified soinfo object and not in any of its dependencies.
5936ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
59416084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' TurnerElf32_Sym *soinfo_lookup(soinfo *si, const char *name)
5951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
59616084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    return soinfo_elf_lookup(si, elfhash(name), name);
5971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
5996ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev/* This is used by dl_sym().  It performs a global symbol lookup.
6006ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
601cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott HughesElf32_Sym* lookup(const char* name, soinfo** found, soinfo* start) {
602cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  unsigned elf_hash = elfhash(name);
6031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
604cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (start == NULL) {
605cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    start = solist;
606cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6071698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer
608cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  Elf32_Sym* s = NULL;
609cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
610cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    s = soinfo_elf_lookup(si, elf_hash, name);
611cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (s != NULL) {
612cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      *found = si;
613cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      break;
6141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
615cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
617cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (s != NULL) {
618cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    TRACE_TYPE(LOOKUP, "%s s->st_value = 0x%08x, found->base = 0x%08x\n",
619cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes               name, s->st_value, (*found)->base);
620cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
622cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  return s;
6231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
625bda5da074eab4bdf374e1f4a19d480c62c72f5ffMathias Agopiansoinfo *find_containing_library(const void *addr)
626e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer{
627e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    soinfo *si;
628e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
629e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    for(si = solist; si != NULL; si = si->next)
630e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    {
631e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) {
632e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer            return si;
633e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        }
634e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
635e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
636e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    return NULL;
637e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
638e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
63916084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' TurnerElf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr)
640e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer{
641e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    unsigned int i;
642e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    unsigned soaddr = (unsigned)addr - si->base;
643e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
644e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    /* Search the library's symbol table for any defined symbol which
645e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer     * contains this address */
646e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    for(i=0; i<si->nchain; i++) {
647e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        Elf32_Sym *sym = &si->symtab[i];
648e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
649e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        if(sym->st_shndx != SHN_UNDEF &&
650e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer           soaddr >= sym->st_value &&
651e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer           soaddr < sym->st_value + sym->st_size) {
652e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer            return sym;
653e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer        }
654e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
655e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
656e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    return NULL;
657e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
658e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
6591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0
6601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void dump(soinfo *si)
6611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
6621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s = si->symtab;
6631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned n;
6641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
6651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for(n = 0; n < si->nchain; n++) {
66661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        TRACE("%04d> %08x: %02x %04x %08x %08x %s\n", n, s,
6671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               s->st_info, s->st_shndx, s->st_value, s->st_size,
6681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               si->strtab + s->st_name);
6691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        s++;
6701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
6731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
674124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library_on_path(const char* name, const char* const paths[]) {
675124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  char buf[512];
676124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  for (size_t i = 0; paths[i] != NULL; ++i) {
677124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    int n = format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
678124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
6799c94fc9fbefe55836c1e1b949850f7726e64c118Elliott Hughes      PRINT("Warning: ignoring very long library path: %s/%s\n", paths[i], name);
680124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      continue;
6811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
682124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
683124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (fd != -1) {
684124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      return fd;
685124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
686124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
687124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return -1;
6881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
690124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library(const char* name) {
69161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("[ opening %s ]\n", name);
6921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
693124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // If the name contains a slash, we should attempt to open it directly and not search the paths.
694124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (strchr(name, '/') != NULL) {
6956971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
6966971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    if (fd != -1) {
6976971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes      return fd;
6986971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    }
6996971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
700124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
7011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
702124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
703124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  int fd = open_library_on_path(name, gLdPaths);
704124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (fd == -1) {
705124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    fd = open_library_on_path(name, gSoPaths);
706124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
707124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return fd;
7081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7104688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Returns 'true' if the library is prelinked or on failure so we error out
7114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// either way. We no longer support prelinking.
7124688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic bool is_prelinked(int fd, const char* name)
7131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
7144688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    struct prelink_info_t {
7154688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        long mmap_addr;
7164688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        char tag[4]; // "PRE ".
7174688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    };
7184688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
719bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END);
7201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (sz < 0) {
7214688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("lseek failed: %s", strerror(errno));
7224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return true;
7231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
725bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    prelink_info_t info;
7268dfc073b1487ab25ab483ab346cee1d9e584adb1Elliott Hughes    int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info)));
7278dfc073b1487ab25ab483ab346cee1d9e584adb1Elliott Hughes    if (rc != sizeof(info)) {
72861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DL_ERR("could not read prelink_info_t structure for \"%s\": %s", name, strerror(errno));
7294688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return true;
7301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (memcmp(info.tag, "PRE ", 4) == 0) {
7334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("prelinked libraries no longer supported: %s", name);
7344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return true;
7351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    return false;
7371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
73916084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner/* verify_elf_header
74016084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner *      Verifies the content of an ELF header.
7411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
7421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Args:
7431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
7441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns:
7451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *       0 on success
7461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *      -1 if no valid ELF object is found @ base.
7471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
7481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int
74916084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerverify_elf_header(const Elf32_Ehdr* hdr)
7501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
7511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1;
7521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1;
7531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1;
7541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1;
755d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    if (hdr->e_type != ET_DYN) return -1;
7561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* TODO: Should we verify anything else in the header? */
758897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#ifdef ANDROID_ARM_LINKER
759897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang    if (hdr->e_machine != EM_ARM) return -1;
760897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#elif defined(ANDROID_X86_LINKER)
761897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang    if (hdr->e_machine != EM_386) return -1;
762d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_MIPS_LINKER)
763d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if (hdr->e_machine != EM_MIPS) return -1;
764897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#endif
7651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
7661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7684688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct scoped_fd {
7694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ~scoped_fd() {
7704688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (fd != -1) {
7714688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            close(fd);
7724688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
7734688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7744688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    int fd;
7754688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes};
7761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct soinfo_ptr {
7784688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo_ptr(const char* name) {
7794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        const char* bname = strrchr(name, '/');
7804688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        ptr = soinfo_alloc(bname ? bname + 1 : name);
7814688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7824688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ~soinfo_ptr() {
7834688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        soinfo_free(ptr);
7844688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7854688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo* release() {
7864688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        soinfo* result = ptr;
7874688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        ptr = NULL;
7884688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return result;
7894688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
7904688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo* ptr;
7914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes};
7921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7934688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// TODO: rewrite linker_phdr.h to use a class, then lose this.
7944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct phdr_ptr {
7954688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    phdr_ptr() : phdr_mmap(NULL) {}
7964688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ~phdr_ptr() {
7974688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (phdr_mmap != NULL) {
7984688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            phdr_table_unload(phdr_mmap, phdr_size);
7994688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
8004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
8014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    void* phdr_mmap;
8024688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr phdr_size;
8034688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes};
804b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner
805124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic soinfo* load_library(const char* name) {
8064688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Open the file.
8074688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    scoped_fd fd;
8084688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    fd.fd = open_library(name);
8094688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (fd.fd == -1) {
8104688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("library \"%s\" not found", name);
8111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
8122e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    }
8131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8144688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Read the ELF header.
8154688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Ehdr header[1];
8164688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header)));
81723363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ret < 0) {
8184688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't read file \"%s\": %s", name, strerror(errno));
8194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
82023363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
82123363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ret != (int)sizeof(header)) {
8224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("too small to be an ELF executable: %s", name);
8234688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
82423363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
82523363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (verify_elf_header(header) < 0) {
8264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("not a valid ELF executable: %s", name);
8274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
8281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Read the program header table.
8314688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    const Elf32_Phdr* phdr_table;
8324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    phdr_ptr phdr_holder;
8334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    ret = phdr_table_load(fd.fd, header->e_phoff, header->e_phnum,
8344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                          &phdr_holder.phdr_mmap, &phdr_holder.phdr_size, &phdr_table);
83523363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ret < 0) {
8364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't load program header table: %s: %s", name, strerror(errno));
8374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
8381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    size_t phdr_count = header->e_phnum;
8401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Get the load extents.
8424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count);
84361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("[ '%s' wants sz=0x%08x ]\n", name, ext_sz);
84423363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    if (ext_sz == 0) {
8454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("no loadable segments in file: %s", name);
8464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
84723363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
84823363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner
8494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // We no longer support pre-linked libraries.
8504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (is_prelinked(fd.fd, name)) {
8514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
85223363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
85316084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner
8544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Reserve address space for all loadable segments.
8554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    void* load_start = NULL;
8564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr load_size = 0;
8574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    Elf32_Addr load_bias = 0;
858b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    ret = phdr_table_reserve_memory(phdr_table,
859b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    phdr_count,
860b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    &load_start,
861b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    &load_size,
862b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                    &load_bias);
863b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (ret < 0) {
8644688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't reserve %d bytes in address space for \"%s\": %s",
8654688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               ext_sz, name, strerror(errno));
8664688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
867b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
8681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
86961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("[ allocated memory for %s @ %p (0x%08x) ]\n", name, load_start, load_size);
870b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner
871b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* Map all the segments in our address space with default protections */
872b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    ret = phdr_table_load_segments(phdr_table,
873b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                   phdr_count,
874b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner                                   load_bias,
8754688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                                   fd.fd);
876b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (ret < 0) {
8774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't map loadable segments for \"%s\": %s",
8784688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               name, strerror(errno));
8794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
880b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
8811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8824688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    soinfo_ptr si(name);
8834688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si.ptr == NULL) {
8844688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
8851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8874688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->base = (Elf32_Addr) load_start;
8884688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->size = load_size;
8894688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->load_bias = load_bias;
8904688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->flags = 0;
8914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->entry = 0;
8924688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->dynamic = (unsigned *)-1;
8934688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->phnum = phdr_count;
8944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    si.ptr->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias);
8954688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si.ptr->phdr == NULL) {
8964688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't find loaded PHDR for \"%s\"", name);
8974688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
89823363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
8994688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
9004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    return si.release();
9011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
9021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
90312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvelstatic soinfo *find_loaded_library(const char *name)
9041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
9051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
9066774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    const char *bname;
9076774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
90812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // TODO: don't use basename only for determining libraries
90912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // http://code.google.com/p/android/issues/detail?id=6670
91012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
91112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    bname = strrchr(name, '/');
91212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    bname = bname ? bname + 1 : name;
91312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
91412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    for(si = solist; si != NULL; si = si->next){
91512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        if(!strcmp(bname, si->name)) {
91612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            return si;
91712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        }
91812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    }
91912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    return NULL;
92012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel}
92112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
922d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic soinfo* find_library_internal(const char* name) {
923d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (name == NULL) {
924d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return somain;
925d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
926d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
927d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_loaded_library(name);
928d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
929d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_LINKED) {
930d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      return si;
931d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    }
932d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    DL_ERR("OOPS: recursive link to \"%s\"", si->name);
933d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return NULL;
934d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
93512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
93661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("[ '%s' has not been loaded yet.  Locating...]\n", name);
937d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  si = load_library(name);
938cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (si == NULL) {
939cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return NULL;
940cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
941cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
942cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // At this point we know that whatever is loaded @ base is a valid ELF
943cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // shared library whose segments are properly mapped in.
944cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  TRACE("[ init_library base=0x%08x sz=0x%08x name='%s') ]\n",
945cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes        si->base, si->size, si->name);
946cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
947cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (!soinfo_link_image(si)) {
948cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    munmap(reinterpret_cast<void*>(si->base), si->size);
949cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    soinfo_free(si);
950cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return NULL;
951d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
9526774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
953d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
954d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
955d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
956d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic soinfo* find_library(const char* name) {
957d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_library_internal(name);
958d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
959d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->refcount++;
960d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
961d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
962d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
963d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
964d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic int soinfo_unload(soinfo* si) {
965d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si->refcount == 1) {
96661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("unloading '%s'\n", si->name);
967d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallDestructors();
968d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
969d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    for (unsigned* d = si->dynamic; *d; d += 2) {
970d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      if (d[0] == DT_NEEDED) {
971d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        soinfo* lsi = find_loaded_library(si->strtab + d[1]);
972d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        if (lsi != NULL) {
97361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes          TRACE("%s needs to unload %s\n", si->name, lsi->name);
974d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          soinfo_unload(lsi);
975d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        } else {
976d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          // TODO: should we return -1 in this case?
977d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          DL_ERR("\"%s\": could not unload dependent library", si->name);
9781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
979d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
9801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
9811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
982d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    munmap(reinterpret_cast<void*>(si->base), si->size);
983d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    notify_gdb_of_unload(si);
984d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    soinfo_free(si);
985d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->refcount = 0;
986d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  } else {
987d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->refcount--;
9889c94fc9fbefe55836c1e1b949850f7726e64c118Elliott Hughes    TRACE("not unloading '%s', decrementing refcount to %d\n", si->name, si->refcount);
989d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
990d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return 0;
9911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
9921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
993cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesvoid do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
994cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (!get_AT_SECURE()) {
995cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    parse_LD_LIBRARY_PATH(ld_library_path);
996cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
997cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
998cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
999e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughessoinfo* do_dlopen(const char* name, int flags) {
1000e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes  if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
1001e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    DL_ERR("invalid flags to dlopen: %x", flags);
1002e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    return NULL;
1003e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes  }
1004d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
1005d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_library(name);
1006d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
1007d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
1008d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1009d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ);
1010d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
1011d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
10121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1013d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesint do_dlclose(soinfo* si) {
1014d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
1015d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  int result = soinfo_unload(si);
1016d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ);
1017d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return result;
10181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
10191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
10201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* TODO: don't use unsigned for addrs below. It works, but is not
10211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
10221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * long.
10231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
102412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvelstatic int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count,
102512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                           soinfo *needed[])
10261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
10271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *symtab = si->symtab;
10281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    const char *strtab = si->strtab;
10291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Sym *s;
10301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    Elf32_Rel *start = rel;
10315ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    soinfo *lsi;
10321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
10334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    for (size_t idx = 0; idx < count; ++idx, ++rel) {
10341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned type = ELF32_R_TYPE(rel->r_info);
10351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned sym = ELF32_R_SYM(rel->r_info);
1036bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner        unsigned reloc = (unsigned)(rel->r_offset + si->load_bias);
10371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned sym_addr = 0;
10381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        char *sym_name = NULL;
10391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
104061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DEBUG("Processing '%s' relocation at index %d\n", si->name, idx);
1041d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (type == 0) { // R_*_NONE
1042d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            continue;
1043d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
10441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if(sym != 0) {
1045d1b40d8c69dfca94185ea9231f6cb16f70c02e39Dima Zavin            sym_name = (char *)(strtab + symtab[sym].st_name);
10465ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1047e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            if(s == NULL) {
1048e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* We only allow an undefined symbol if this is a weak
1049e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   reference..   */
1050e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                s = &symtab[sym];
1051e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
1052e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes                    DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
1053e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1054e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1055e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1056e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* IHI0044C AAELF 4.5.1.1:
1057e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1058e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   Libraries are not searched to resolve weak references.
1059e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   It is not an error for a weak reference to remain
1060e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   unsatisfied.
1061e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1062e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   During linking, the value of an undefined weak reference is:
1063e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - Zero if the relocation type is absolute
1064e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - The address of the place if the relocation is pc-relative
1065bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes                   - The address of nominal base address if the relocation
1066e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                     type is base-relative.
1067e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                  */
1068e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1069e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                switch (type) {
1070e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_ARM_LINKER)
1071e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_JUMP_SLOT:
1072e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_GLOB_DAT:
1073e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_ABS32:
1074e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_RELATIVE:    /* Don't care. */
1075e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#elif defined(ANDROID_X86_LINKER)
1076d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                case R_386_JMP_SLOT:
1077e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_GLOB_DAT:
1078e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_32:
1079e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_RELATIVE:    /* Dont' care. */
1080e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_*_LINKER */
1081e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    /* sym_addr was initialized to be zero above or relocation
1082e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       code below does not care about value of sym_addr.
1083e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       No need to do anything.  */
1084e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
1085e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1086e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_X86_LINKER)
1087e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_PC32:
1088e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    sym_addr = reloc;
1089e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
1090e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_X86_LINKER */
1091e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1092e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_ARM_LINKER)
1093e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_COPY:
1094e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    /* Fall through.  Can't really copy if weak symbol is
1095e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       not found in run-time.  */
1096e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_ARM_LINKER */
1097e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                default:
10984688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("unknown weak reloc type %d @ %p (%d)",
10994688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                                 type, rel, (int) (rel - start));
1100e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1101e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1102e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            } else {
1103e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* We got a definition.  */
11041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0
1105d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                if((base == 0) && (si->base != 0)){
1106d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                        /* linking from libraries to main image is bad */
11074688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("cannot locate \"%s\"...",
11084688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                           strtab + symtab[sym].st_name);
1109d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                    return -1;
1110d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                }
11111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
11125ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                sym_addr = (unsigned)(s->st_value + lsi->load_bias);
1113d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1114bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocSymbol);
11151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        } else {
1116e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            s = NULL;
11171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
11181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* TODO: This is ugly. Split up the relocations by arch into
11201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * different files.
11211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
11221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(type){
11231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_ARM_LINKER)
11241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_JUMP_SLOT:
1125bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
11261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
112761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
11281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
11291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_GLOB_DAT:
1131bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
11321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
113361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
11341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
11351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_ABS32:
1137bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
11381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
113961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s\n", reloc, sym_addr, sym_name);
11401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) += sym_addr;
11411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
114234ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner        case R_ARM_REL32:
1143bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
114434ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            MARK(rel->r_offset);
114561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s\n",
114634ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner                       reloc, sym_addr, rel->r_offset, sym_name);
114734ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            *((unsigned*)reloc) += sym_addr - rel->r_offset;
114834ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            break;
11491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#elif defined(ANDROID_X86_LINKER)
1150d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case R_386_JMP_SLOT:
1151bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
11521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
115361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
11541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
11551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_GLOB_DAT:
1157bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
11581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
115961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
11601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) = sym_addr;
11611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1162d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_MIPS_LINKER)
1163d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    case R_MIPS_JUMP_SLOT:
1164bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
1165d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            MARK(rel->r_offset);
116661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name);
1167d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *((unsigned*)reloc) = sym_addr;
1168d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1169d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    case R_MIPS_REL32:
1170bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
1171d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            MARK(rel->r_offset);
117261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s\n",
1173d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                       reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1174d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            if (s) {
1175d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                *((unsigned*)reloc) += sym_addr;
1176d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            } else {
1177d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                *((unsigned*)reloc) += si->base;
1178d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1179d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
11801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_*_LINKER */
11811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_ARM_LINKER)
11831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_RELATIVE:
11841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#elif defined(ANDROID_X86_LINKER)
11851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_RELATIVE:
11861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_*_LINKER */
1187bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
11881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
11894688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            if (sym) {
119061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                DL_ERR("odd RELATIVE form...");
11911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                return -1;
11921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
119361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO RELATIVE %08x <- +%08x\n", reloc, si->base);
11941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned*)reloc) += si->base;
11951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_X86_LINKER)
11981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_32:
1199bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
12001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
12011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
120261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s\n", reloc, sym_addr, sym_name);
12031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned *)reloc) += (unsigned)sym_addr;
12041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
12051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_PC32:
1207bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
12081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
120961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s\n",
121061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                       reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
12111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *((unsigned *)reloc) += (unsigned)(sym_addr - reloc);
12121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
12131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_X86_LINKER */
12141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef ANDROID_ARM_LINKER
12161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_COPY:
1217d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            if ((si->flags & FLAG_EXE) == 0) {
1218d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                /*
1219d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1220d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 *
1221d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * Section 4.7.1.10 "Dynamic relocations"
1222d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * R_ARM_COPY may only appear in executable objects where e_type is
1223d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * set to ET_EXEC.
1224d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 *
1225d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1226d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * We should explicitly disallow ET_DYN executables from having
1227d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * R_ARM_COPY relocations.
1228d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 */
1229d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1230d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                return -1;
1231d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            }
1232bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocCopy);
12331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
123461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s\n", reloc, s->st_size, sym_addr, sym_name);
1235d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            if (reloc == sym_addr) {
12365ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
12375ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
12385ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (src == NULL) {
12395ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
12405ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
12415ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
12425ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (lsi->has_DT_SYMBOLIC) {
12435ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
12445ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                           "library %s (built with -Bsymbolic?)", si->name, lsi->name);
12455ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
12465ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
12475ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (s->st_size < src->st_size) {
12485ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
12495ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                           si->name, s->st_size, src->st_size);
12505ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
12515ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
12525ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
12535ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            } else {
12545ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
1255d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                return -1;
1256d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            }
12571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
12581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_ARM_LINKER */
12591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        default:
12614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("unknown reloc type %d @ %p (%d)",
12624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                   type, rel, (int) (rel - start));
12631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return -1;
12641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
12651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
12661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
12671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
12681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1269d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#ifdef ANDROID_MIPS_LINKER
1270bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic int mips_relocate_got(soinfo* si, soinfo* needed[]) {
1271d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned *got;
1272d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned local_gotno, gotsym, symtabno;
1273d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    Elf32_Sym *symtab, *sym;
1274d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned g;
1275d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1276d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got;
1277d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    local_gotno = si->mips_local_gotno;
1278d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    gotsym = si->mips_gotsym;
1279d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    symtabno = si->mips_symtabno;
1280d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    symtab = si->symtab;
1281d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1282d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    /*
1283d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * got[0] is address of lazy resolver function
1284d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * got[1] may be used for a GNU extension
1285bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes     * set it to a recognizable address in case someone calls it
1286d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * (should be _rtld_bind_start)
1287d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * FIXME: maybe this should be in a separate routine
1288d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     */
1289d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1290d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if ((si->flags & FLAG_LINKER) == 0) {
1291d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        g = 0;
1292d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        got[g++] = 0xdeadbeef;
1293d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (got[g] & 0x80000000) {
1294d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            got[g++] = 0xdeadfeed;
1295d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1296d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        /*
1297d7daacb46372132ae3f0121647074936c304b572Raghu Gandham         * Relocate the local GOT entries need to be relocated
1298d7daacb46372132ae3f0121647074936c304b572Raghu Gandham         */
1299d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        for (; g < local_gotno; g++) {
1300d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            got[g] += si->load_bias;
1301d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1302d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1303d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1304d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    /* Now for the global GOT entries */
1305d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    sym = symtab + gotsym;
1306d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got + local_gotno;
1307d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    for (g = gotsym; g < symtabno; g++, sym++, got++) {
1308d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        const char *sym_name;
1309d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        Elf32_Sym *s;
13105ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        soinfo *lsi;
1311d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1312d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        /* This is an undefined reference... try to locate it */
1313d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        sym_name = si->strtab + sym->st_name;
13145ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1315d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (s == NULL) {
1316d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* We only allow an undefined symbol if this is a weak
1317d7daacb46372132ae3f0121647074936c304b572Raghu Gandham               reference..   */
1318d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            s = &symtab[g];
1319d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
13204688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("cannot locate \"%s\"...", sym_name);
1321d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                return -1;
1322d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1323d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *got = 0;
1324d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1325d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        else {
1326d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* FIXME: is this sufficient?
1327d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             * For reference see NetBSD link loader
1328d7daacb46372132ae3f0121647074936c304b572Raghu 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
1329d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             */
13305ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel             *got = lsi->load_bias + s->st_value;
1331d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1332d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1333d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    return 0;
1334d7daacb46372132ae3f0121647074936c304b572Raghu Gandham}
1335d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1336d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
13378215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner/* Please read the "Initialization and Termination functions" functions.
13388215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * of the linker design note in bionic/linker/README.TXT to understand
13398215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * what the following code is doing.
13408215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
13418215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * The important things to remember are:
13428215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
13438215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_PREINIT_ARRAY must be called first for executables, and should
13448215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   not appear in shared libraries.
13458215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
13468215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_INIT should be called before DT_INIT_ARRAY if both are present
13478215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
13488215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_FINI should be called after DT_FINI_ARRAY if both are present
13498215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
13508215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_FINI_ARRAY must be parsed in reverse order.
13518215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner */
1352d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallArray(const char* array_name UNUSED, unsigned* array, int count, bool reverse) {
1353d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (array == NULL) {
1354d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1355d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
13568215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1357d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  int step = 1;
1358d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (reverse) {
1359d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    array += (count-1);
1360d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    step = -1;
1361d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
13628215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
136361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("[ Calling %s @ %p [%d] for '%s' ]\n", array_name, array, count, name);
13648215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1365d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  for (int n = count; n > 0; n--) {
136661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("[ Looking at %s[%d] *%p == 0x%08x ]\n", array_name, n, array, *array);
1367d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    void (*func)() = (void (*)()) *array;
1368d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    array += step;
1369d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (((int) func == 0) || ((int) func == -1)) {
1370d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      continue;
13711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
137261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("[ Calling func @ %p ]\n", func);
1373d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    func();
1374d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1375d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
137661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("[ Done calling %s for '%s' ]\n", array_name, name);
13771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
13781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1379d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallFunction(const char* function_name UNUSED, void (*function)()) {
1380d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (function == NULL) {
1381d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1382d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1383d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
138461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("[ Calling %s @ %p for '%s' ]\n", function_name, function, name);
1385d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  function();
138661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes  TRACE("[ Done calling %s for '%s' ]\n", function_name, name);
13879181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov}
13889181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
1389d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallPreInitConstructors() {
1390d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1391d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
1392e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1393d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallConstructors() {
1394d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (constructors_called) {
1395d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1396d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1397e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1398d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // We set constructors_called before actually calling the constructors, otherwise it doesn't
1399d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // protect against recursive constructor calls. One simple example of constructor recursion
1400d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1401d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 1. The program depends on libc, so libc's constructor is called here.
1402d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1403d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 3. dlopen() calls the constructors on the newly created
1404d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    soinfo for libc_malloc_debug_leak.so.
1405d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 4. The debug .so depends on libc, so CallConstructors is
1406d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    called again with the libc soinfo. If it doesn't trigger the early-
1407d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    out above, the libc constructor will be called again (recursively!).
1408d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  constructors_called = true;
1409d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1410d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (!(flags & FLAG_EXE) && preinit_array) {
1411d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    DL_ERR("shared library \"%s\" has a preinit_array table @ %p", name, preinit_array);
1412d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1413d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
14141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1415d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (dynamic) {
1416d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    for (unsigned* d = dynamic; *d; d += 2) {
1417d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      if (d[0] == DT_NEEDED) {
1418d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        soinfo* lsi = find_loaded_library(strtab + d[1]);
1419d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        if (lsi == NULL) {
1420d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          DL_ERR("\"%s\": could not initialize dependent library", name);
1421d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        } else {
1422d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          lsi->CallConstructors();
1423d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        }
1424d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
14251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1426d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
14271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1428d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_INIT", init_func);
1429d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
1430e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov}
14318215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1432d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallDestructors() {
1433d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
1434d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_FINI", fini_func);
14351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with
14381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   /dev/null. */
14395419b9474753d25dff947c7740532f86d130c0beElliott Hughesstatic int nullify_closed_stdio() {
14401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int dev_null, i, status;
14411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int return_value = 0;
14421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
144316084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
14441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null < 0) {
14454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("cannot open /dev/null: %s", strerror(errno));
14461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return -1;
14471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
144861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("[ Opened /dev/null file-descriptor=%d]\n", dev_null);
14491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If any of the stdio file descriptors is valid and not associated
14511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       with /dev/null, dup /dev/null to it.  */
14521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (i = 0; i < 3; i++) {
14531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* If it is /dev/null already, we are done. */
14544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (i == dev_null) {
14551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
14564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
14571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
145861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        TRACE("[ Nullifying stdio file descriptor %d]\n", i);
14594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
14601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        /* If file is opened, we are good. */
14624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status != -1) {
14634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            continue;
14644688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
14651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* The only error we allow is that the file descriptor does not
14671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           exist, in which case we dup /dev/null to it. */
14681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (errno != EBADF) {
14694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("fcntl failed: %s", strerror(errno));
14701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
14721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* Try dupping /dev/null to this stdio file descriptor and
14751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           repeat if there is a signal.  Note that any errors in closing
14761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           the stdio descriptor are lost.  */
14774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
14781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (status < 0) {
14794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("dup2 failed: %s", strerror(errno));
14801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
14821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If /dev/null is not one of the stdio file descriptors, close it. */
14861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null > 2) {
148761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        TRACE("[ Closing /dev/null file-descriptor=%d]\n", dev_null);
14884688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(close(dev_null));
14894688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status == -1) {
14904688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("close failed: %s", strerror(errno));
14911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return return_value;
14961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1498124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic bool soinfo_link_image(soinfo* si) {
1499f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee    /* "base" might wrap around UINT32_MAX. */
1500bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    Elf32_Addr base = si->load_bias;
1501b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    const Elf32_Phdr *phdr = si->phdr;
15021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int phnum = si->phnum;
1503b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    int relocating_linker = (si->flags & FLAG_LINKER) != 0;
150412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    soinfo **needed, **pneeded;
15051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1506b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can't debug anything until the linker is relocated */
1507b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (!relocating_linker) {
150861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        INFO("[ linking %s ]\n", si->name);
150961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DEBUG("si->base = 0x%08x si->flags = 0x%08x\n", si->base, si->flags);
1510b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
15111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
151263f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    /* Extract dynamic section */
1513124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    size_t dynamic_count;
151412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
151512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                                   &dynamic_count);
151663f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    if (si->dynamic == NULL) {
1517b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1518124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
1519b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
1520124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
152163f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    } else {
1522b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
152361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("dynamic = %p\n", si->dynamic);
1524b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
152563f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    }
152663f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
152763f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#ifdef ANDROID_ARM_LINKER
152863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    (void) phdr_table_get_arm_exidx(phdr, phnum, base,
152963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner                                    &si->ARM_exidx, &si->ARM_exidx_count);
153063f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif
153163f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
15321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* extract useful information from dynamic section */
1533124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    for (unsigned* d = si->dynamic; *d; ++d) {
153461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DEBUG("d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", d, d[0], d[1]);
15351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(*d++){
15361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_HASH:
1537f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->nbucket = ((unsigned *) (base + *d))[0];
1538f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->nchain = ((unsigned *) (base + *d))[1];
1539f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->bucket = (unsigned *) (base + *d + 8);
1540f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4);
15411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_STRTAB:
1543f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->strtab = (const char *) (base + *d);
15441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_SYMTAB:
1546f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->symtab = (Elf32_Sym *) (base + *d);
15471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTREL:
15491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if(*d != DT_REL) {
1550124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1551124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
15521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
15531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_JMPREL:
1555f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->plt_rel = (Elf32_Rel*) (base + *d);
15561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTRELSZ:
15581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->plt_rel_count = *d / 8;
15591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_REL:
1561f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->rel = (Elf32_Rel*) (base + *d);
15621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_RELSZ:
15641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->rel_count = *d / 8;
15651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTGOT:
15671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            /* Save this in case we decide to do lazy binding. We don't yet. */
1568f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->plt_got = (unsigned *)(base + *d);
15691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_DEBUG:
1571d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if !defined(ANDROID_MIPS_LINKER)
1572bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_DEBUG entry to the address of _r_debug for GDB
15731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            *d = (int) &_r_debug;
1574d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
15751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1576ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI         case DT_RELA:
1577124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1578124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
15791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT:
1580f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->init_func = (void (*)(void))(base + *d);
158161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("%s constructors (init func) found at %p\n", si->name, si->init_func);
15821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI:
1584f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->fini_func = (void (*)(void))(base + *d);
158561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("%s destructors (fini func) found at %p\n", si->name, si->fini_func);
15861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAY:
1588f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->init_array = (unsigned *)(base + *d);
158961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("%s constructors (init_array) found at %p\n", si->name, si->init_array);
15901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAYSZ:
15921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
15931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAY:
1595f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->fini_array = (unsigned *)(base + *d);
159661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("%s destructors (fini_array) found at %p\n", si->name, si->fini_array);
15971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAYSZ:
15991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
16001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAY:
1602f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            si->preinit_array = (unsigned *)(base + *d);
160361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("%s constructors (preinit_array) found at %p\n", si->name, si->preinit_array);
16041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAYSZ:
16061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr);
16071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_TEXTREL:
16095135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            si->has_text_relocations = true;
16101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16115ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_SYMBOLIC:
16125ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            si->has_DT_SYMBOLIC = true;
16135ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
16145ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel#if defined(DT_FLAGS)
16155ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_FLAGS:
16165ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (*d & DF_TEXTREL) {
16175ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_text_relocations = true;
16185ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
16195ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (*d & DF_SYMBOLIC) {
16205ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_DT_SYMBOLIC = true;
16215ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
16225ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
16235ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel#endif
1624d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if defined(ANDROID_MIPS_LINKER)
1625d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_NEEDED:
1626d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_STRSZ:
1627d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_SYMENT:
1628d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_RELENT:
1629d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             break;
1630d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_MAP:
1631bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
1632d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            {
1633bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes              r_debug** dp = (r_debug**) *d;
1634d7daacb46372132ae3f0121647074936c304b572Raghu Gandham              *dp = &_r_debug;
1635d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1636d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1637d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_VERSION:
1638d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_FLAGS:
1639d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_BASE_ADDRESS:
1640d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_UNREFEXTNO:
1641d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RWPLT:
1642d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1643d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1644d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_PLTGOT:
1645d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if 0
1646d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* not yet... */
1647d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_pltgot = (unsigned *)(si->base + *d);
1648d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1649d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1650d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1651d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_SYMTABNO:
1652d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_symtabno = *d;
1653d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1654d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1655d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_LOCAL_GOTNO:
1656d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_local_gotno = *d;
1657d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1658d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1659d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_GOTSYM:
1660d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            si->mips_gotsym = *d;
1661d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1662d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1663d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        default:
166461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("Unused DT entry: type 0x%08x arg 0x%08x\n", d[-1], d[0]);
1665d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1666d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
16671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
16681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
167061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    DEBUG("si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n",
167161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes          si->base, si->strtab, si->symtab);
16721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1673124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    // Sanity checks.
1674124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->nbucket == 0) {
1675124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1676124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1677124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1678124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->strtab == 0) {
1679124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1680124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1681124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1682124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->symtab == 0) {
1683124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1684124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
16851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16874fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* if this is the main executable, then load all of the preloads now */
1688d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_EXE) {
16894fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        memset(preloads, 0, sizeof(preloads));
1690d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1691d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            soinfo* lsi = find_library(gLdPreloadNames[i]);
1692d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            if (lsi == NULL) {
16934fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer                strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
16944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1695124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                       gLdPreloadNames[i], si->name, tmp_err_buf);
1696124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
16974fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            }
16984fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            preloads[i] = lsi;
16994fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        }
17004fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
17014fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
170212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    /* dynamic_count is an upper bound for the number of needed libs */
170312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    pneeded = needed = (soinfo**) alloca((1 + dynamic_count) * sizeof(soinfo*));
170412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
1705124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    for (unsigned* d = si->dynamic; *d; d += 2) {
1706d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        if (d[0] == DT_NEEDED) {
170761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes            DEBUG("%s needs %s\n", si->name, si->strtab + d[1]);
1708d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            soinfo* lsi = find_library(si->strtab + d[1]);
1709d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            if (lsi == NULL) {
17100353195f344666256dba474a15c9ba22cf0cccc9Dima Zavin                strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf));
17114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
17124688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                       si->strtab + d[1], si->name, tmp_err_buf);
1713124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
17141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
171512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            *pneeded++ = lsi;
17161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
17171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
171812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    *pneeded = NULL;
17191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17205135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
17215135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        /* Unprotect the segments, i.e. make them writable, to allow
17225135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * text relocations to work properly. We will later call
17235135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * phdr_table_protect_segments() after all of them are applied
17245135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * and all constructors are run.
17255135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         */
17265135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
17275135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't unprotect loadable segments for \"%s\": %s",
17285135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1729124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
17305135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
17315135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    }
17325135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich
1733124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->plt_rel) {
173461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DEBUG("[ relocating %s plt ]\n", si->name );
1735124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1736124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1737124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
17381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1739124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->rel) {
174061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        DEBUG("[ relocating %s ]\n", si->name );
1741124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        if(soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1742124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1743124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
17441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
17451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1746d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#ifdef ANDROID_MIPS_LINKER
1747124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (mips_relocate_got(si, needed)) {
1748124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1749d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1750d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1751d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
17521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_LINKED;
175361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    DEBUG("[ finished linking %s ]\n", si->name);
17541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17555135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
17565135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        /* All relocations are done, we can protect our segments back to
17575135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * read-only. */
17585135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
17595135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't protect segments for \"%s\": %s",
17605135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1761124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
17625135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
17631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
17641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1765b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can also turn on GNU RELRO protection */
1766b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
17674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
17684688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               si->name, strerror(errno));
1769124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
17709ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich    }
17719ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
1772124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    // If this is a setuid/setgid program, close the security hole described in
1773124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
177418a206c81d9743481e364384affd43306911283dElliott Hughes    if (get_AT_SECURE()) {
17754688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        nullify_closed_stdio();
17764688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
17771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    notify_gdb_of_load(si);
1778124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    return true;
17791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
17801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1781468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1782468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and
1783468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs
1784468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point.
1785468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
1786bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissellstatic unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base)
17871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
17881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    static soinfo linker_soinfo;
17891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int argc = (int) *elfdata;
17911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    char **argv = (char**) (elfdata + 1);
1792bb44055d0a3515410f45417d5b0e047cc60f0dc6Stephen Smalley    unsigned *vecs = (unsigned*) (argv + argc + 1);
17931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1794ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner    /* NOTE: we store the elfdata pointer on a special location
1795ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       of the temporary TLS area in order to pass it to
1796ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       the C Library's runtime initializer.
1797ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *
1798ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       The initializer must clear the slot and reset the TLS
1799ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       to point to a different location to ensure that no other
1800ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       shared library constructor can access it.
1801ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     */
18021a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    __libc_init_tls(elfdata);
18031a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov
18041a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING
18051a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    struct timeval t0, t1;
18061a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    gettimeofday(&t0, 0);
18071a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif
18081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
180918a206c81d9743481e364384affd43306911283dElliott Hughes    // Initialize environment functions, and get to the ELF aux vectors table.
1810be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    vecs = linker_env_init(vecs);
1811be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
18121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    debugger_init();
18131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
181418a206c81d9743481e364384affd43306911283dElliott Hughes    // Get a few environment variables.
181561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    const char* LD_DEBUG = linker_env_get("LD_DEBUG");
181661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    if (LD_DEBUG != NULL) {
181761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes      debug_verbosity = atoi(LD_DEBUG);
181818a206c81d9743481e364384affd43306911283dElliott Hughes    }
1819be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
182018a206c81d9743481e364384affd43306911283dElliott Hughes    // Normally, these are cleaned by linker_env_init, but the test
182118a206c81d9743481e364384affd43306911283dElliott Hughes    // doesn't cost us anything.
182218a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpath_env = NULL;
182318a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpreload_env = NULL;
182418a206c81d9743481e364384affd43306911283dElliott Hughes    if (!get_AT_SECURE()) {
182518a206c81d9743481e364384affd43306911283dElliott Hughes      ldpath_env = linker_env_get("LD_LIBRARY_PATH");
182618a206c81d9743481e364384affd43306911283dElliott Hughes      ldpreload_env = linker_env_get("LD_PRELOAD");
18271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    INFO("[ android linker & debugger ]\n");
183061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    DEBUG("elfdata @ 0x%08x\n", (unsigned)elfdata);
18311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
183218a206c81d9743481e364384affd43306911283dElliott Hughes    soinfo* si = soinfo_alloc(argv[0]);
183318a206c81d9743481e364384affd43306911283dElliott Hughes    if (si == NULL) {
183418a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
18351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1837d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    /* bootstrap the link map, the main exe always needs to be first */
18381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_EXE;
1839bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    link_map* map = &(si->linkmap);
18401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = 0;
18421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_name = argv[0];
18431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_prev = NULL;
18441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_next = NULL;
18451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_map = map;
18471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
18481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1849bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell        /* gdb expects the linker to be in the debug shared object list.
1850bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * Without this, gdb has trouble locating the linker's ".text"
1851bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * and ".plt" sections. Gdb could also potentially use this to
1852bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1853bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell         * Don't use soinfo_alloc(), because the linker shouldn't
18541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         * be on the soinfo list.
18551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project         */
1856be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name);
18571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    linker_soinfo.flags = 0;
1858bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell    linker_soinfo.base = linker_base;
185906f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng    /*
186006f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     * Set the dynamic field in the link map otherwise gdb will complain with
186106f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     * the following:
186206f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     *   warning: .dynamic section for "/system/bin/linker" is not at the
186306f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     *   expected address (wrong library or version mismatch?)
186406f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     */
186506f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng    Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
186606f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng    Elf32_Phdr *phdr =
186706f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng        (Elf32_Phdr *)((unsigned char *) linker_base + elf_hdr->e_phoff);
186812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
186912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel                                   &linker_soinfo.dynamic, NULL);
18701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(&linker_soinfo);
18711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1872d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    /* extract information passed from the kernel */
187318a206c81d9743481e364384affd43306911283dElliott Hughes    while (vecs[0] != 0){
18741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(vecs[0]){
18751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case AT_PHDR:
18761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->phdr = (Elf32_Phdr*) vecs[1];
18771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case AT_PHNUM:
18791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->phnum = (int) vecs[1];
18801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case AT_ENTRY:
18821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            si->entry = vecs[1];
18831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
18851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        vecs += 2;
18861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18888180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    /* Compute the value of si->base. We can't rely on the fact that
18898180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * the first entry is the PHDR because this will not be true
18908180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * for certain executables (e.g. some in the NDK unit test suite)
18918180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     */
18928180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    int nn;
18938180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    si->base = 0;
1894b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    si->size = phdr_table_get_load_size(si->phdr, si->phnum);
1895bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    si->load_bias = 0;
18968180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    for ( nn = 0; nn < si->phnum; nn++ ) {
18978180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner        if (si->phdr[nn].p_type == PT_PHDR) {
1898bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner            si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr;
1899bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner            si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset;
19008180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner            break;
19018180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner        }
19028180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    }
19031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->dynamic = (unsigned *)-1;
19046774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    si->refcount = 1;
19051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19064688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
19074688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_LIBRARY_PATH(ldpath_env);
19084688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_PRELOAD(ldpreload_env);
19094fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
19105ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    somain = si;
19115ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
1912124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (!soinfo_link_image(si)) {
191320a24403bc333cd92996cd43cef2e6a79c72d055Chris Dearman        const char* msg = "CANNOT LINK EXECUTABLE\n";
19142e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin        write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
191520a24403bc333cd92996cd43cef2e6a79c72d055Chris Dearman        write(2, msg, strlen(msg));
191618a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
19171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
19181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1919d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallPreInitConstructors();
19209181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
192118a206c81d9743481e364384affd43306911283dElliott Hughes    for (size_t i = 0; preloads[i] != NULL; ++i) {
1922d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        preloads[i]->CallConstructors();
1923326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng    }
1924326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng
1925c5db969aa4a24a76fc0964f2ff89533e832b47a6Chao-Ying Fu    /*After the link_image, the si->load_bias is initialized.
19269c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     *For so lib, the map->l_addr will be updated in notify_gdb_of_load.
19279c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     *We need to update this value for so exe here. So Unwind_Backtrace
19289c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     *for some arch like x86 could work correctly within so exe.
19299c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     */
1930c5db969aa4a24a76fc0964f2ff89533e832b47a6Chao-Ying Fu    map->l_addr = si->load_bias;
1931d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
1932e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
19331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING
19341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    gettimeofday(&t1,NULL);
19351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) (
19361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
19371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
19381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               ));
19391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
19411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0],
1942bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocAbsolute],
1943bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocRelative],
1944bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocCopy],
1945bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocSymbol]);
19461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
19481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
19491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned n;
19501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned i;
19511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned count = 0;
19521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        for(n = 0; n < 4096; n++){
19531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            if(bitmask[n]){
19541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                unsigned x = bitmask[n];
19551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                for(i = 0; i < 8; i++){
19561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    if(x & 1) count++;
19571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    x >>= 1;
19581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                }
19591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
19601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
19611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4);
19621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
19631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES
19661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    fflush(stdout);
19671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
196961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    TRACE("[ Ready to execute '%s' @ 0x%08x ]\n", si->name, si->entry);
19701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si->entry;
19711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
1972468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1973468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1974468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Find the value of AT_BASE passed to us by the kernel. This is the load
1975468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * location of the linker.
1976468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
1977468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevichstatic unsigned find_linker_base(unsigned **elfdata) {
1978468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    int argc = (int) *elfdata;
1979468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    char **argv = (char**) (elfdata + 1);
1980468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    unsigned *vecs = (unsigned*) (argv + argc + 1);
1981468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    while (vecs[0] != 0) {
1982468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        vecs++;
1983468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    }
1984468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1985468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    /* The end of the environment block is marked by two NULL pointers */
1986468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    vecs++;
1987468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1988468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    while(vecs[0]) {
1989468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if (vecs[0] == AT_BASE) {
1990468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            return vecs[1];
1991468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        }
1992468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        vecs += 2;
1993468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    }
1994468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1995468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    return 0; // should never happen
1996468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
1997468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1998bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only
1999bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library
2000bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself.
2001bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *
2002bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input:
2003bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    elf    -> address of ELF header, assumed to be at the start of the file.
2004bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return:
2005bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    load bias, i.e. add the value of any p_vaddr in the file to get
2006bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    the corresponding address in memory.
2007bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */
2008bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turnerstatic Elf32_Addr
2009bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turnerget_elf_exec_load_bias(const Elf32_Ehdr* elf)
2010bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner{
2011bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    Elf32_Addr        offset     = elf->e_phoff;
2012bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
2013bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    const Elf32_Phdr* phdr_end   = phdr_table + elf->e_phnum;
2014bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    const Elf32_Phdr* phdr;
2015bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
2016bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    for (phdr = phdr_table; phdr < phdr_end; phdr++) {
2017bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner        if (phdr->p_type == PT_LOAD) {
2018bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner            return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr;
2019bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner        }
2020bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    }
2021bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    return 0;
2022bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner}
2023bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
2024468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
2025468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This
2026468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and
2027468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation().
2028468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich *
2029468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own
2030468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern
2031468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault.
2032468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
20334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesextern "C" unsigned __linker_init(unsigned **elfdata) {
2034468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    unsigned linker_addr = find_linker_base(elfdata);
2035468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr;
2036468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    Elf32_Phdr *phdr =
2037468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff);
2038468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2039468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    soinfo linker_so;
2040468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    memset(&linker_so, 0, sizeof(soinfo));
2041468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2042468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.base = linker_addr;
2043b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
2044bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
2045468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.dynamic = (unsigned *) -1;
2046468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.phdr = phdr;
2047468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.phnum = elf_hdr->e_phnum;
2048468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    linker_so.flags |= FLAG_LINKER;
2049468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2050124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (!soinfo_link_image(&linker_so)) {
2051468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // It would be nice to print an error message, but if the linker
2052468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // can't link itself, there's no guarantee that we'll be able to
2053468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // call write() (because it involves a GOT reference).
2054468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        //
2055468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // This situation should never occur unless the linker itself
2056468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        // is corrupt.
205718a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
2058468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    }
2059468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2060468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    // We have successfully fixed our own relocations. It's safe to run
2061468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich    // the main part of the linker now.
20625419b9474753d25dff947c7740532f86d130c0beElliott Hughes    unsigned start_address = __linker_init_post_relocation(elfdata, linker_addr);
20635419b9474753d25dff947c7740532f86d130c0beElliott Hughes
2064d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    set_soinfo_pool_protection(PROT_READ);
2065d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
20665419b9474753d25dff947c7740532f86d130c0beElliott Hughes    // Return the address that the calling assembly stub should jump to.
20675419b9474753d25dff947c7740532f86d130c0beElliott Hughes    return start_address;
2068468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
2069