linker.cpp revision 8d3e91d4f842911366155845afb3cfbdad0b4cad
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>
4442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes#include <private/KernelArgumentBlock.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"
5023363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner#include "linker_phdr.h"
511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
52bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley/* Assume average path length of 64 and max 8 paths */
53bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley#define LDPATH_BUFSIZE 512
54bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley#define LDPATH_MAX 8
55bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
564fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer#define LDPRELOAD_BUFSIZE 512
574fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer#define LDPRELOAD_MAX 8
584fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Do NOT use malloc() and friends or pthread_*() code here.
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Don't use printf() either; it's caused mysterious memory
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * corruption in the past.
641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The linker runs before we bring up libc and it's easiest
651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to make sure it does not depend on any complex libc features
661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * open issues / todo:
681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - are we doing everything we should for ARM_COPY relocations?
701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - cleaner error reporting
711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - after linking, set as much stuff as possible to READONLY
721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   and NOEXEC
734688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes */
741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
75124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic bool soinfo_link_image(soinfo* si);
761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
77ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
78ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn// maps, each a single page in size. The pages are broken up into as many struct soinfo
79ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn// objects as will fit, and they're all threaded together on a free list.
80ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
81ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstruct soinfo_pool_t {
82ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo_pool_t* next;
83ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo info[SOINFO_PER_POOL];
84ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn};
85ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic struct soinfo_pool_t* gSoInfoPools = NULL;
86ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic soinfo* gSoInfoFreeList = NULL;
87ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
88d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* solist = &libdl_info;
89d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* sonext = &libdl_info;
90d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* somain; /* main process, always the one after libdl_info */
911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
92124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* const gSoPaths[] = {
93124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  "/vendor/lib",
94124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  "/system/lib",
95124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  NULL
96124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes};
97124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes
98124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic char gLdPathsBuffer[LDPATH_BUFSIZE];
99124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* gLdPaths[LDPATH_MAX + 1];
100bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
101124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
102124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
1034fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
104d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
1054fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
106650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes__LIBC_HIDDEN__ int gLdDebugVerbosity;
1071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1080d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes__LIBC_HIDDEN__ abort_msg_t* gAbortMessage = NULL; // For debuggerd.
1090d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes
110bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesenum RelocationKind {
111bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocAbsolute = 0,
112bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocRelative,
113bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocCopy,
114bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocSymbol,
115bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocMax
116bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
117be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
1181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
119bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstruct linker_stats_t {
120bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    int count[kRelocMax];
121bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
122bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
123bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic linker_stats_t linker_stats;
124bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
125bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind kind) {
126bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    ++linker_stats.count[kind];
127bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
128bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
129bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind) {
130bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
1311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
134bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic unsigned bitmask[4096];
135bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(offset) \
136bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    do { \
137bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
138bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    } while(0)
139bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
140bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(x) do {} while (0)
1411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// You shouldn't try to call memory-allocating functions in the dynamic linker.
1444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Guard against the most obvious ones.
1458f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#define DISALLOW_ALLOCATION(return_type, name, ...) \
1468f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    return_type name __VA_ARGS__ \
1478f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    { \
1484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
1498f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        __libc_format_log(ANDROID_LOG_FATAL, "linker", "%s", msg); \
1508f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        write(2, msg, strlen(msg)); \
1518f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        abort(); \
1524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
1534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#define UNUSED __attribute__((unused))
1544688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
1554688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void, free, (void* u UNUSED));
1564688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
1574688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
1582e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1590353195f344666256dba474a15c9ba22cf0cccc9Dima Zavinstatic char tmp_err_buf[768];
1602e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavinstatic char __linker_dl_err_buf[768];
1612e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
162650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hugheschar* linker_get_error_buffer() {
1635419b9474753d25dff947c7740532f86d130c0beElliott Hughes  return &__linker_dl_err_buf[0];
1642e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin}
1652e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
166650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughessize_t linker_get_error_buffer_size() {
167650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  return sizeof(__linker_dl_err_buf);
168650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes}
169650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
1701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
1711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This function is an empty stub where GDB locates a breakpoint to get notified
1721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about linker activity.
1731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1745419b9474753d25dff947c7740532f86d130c0beElliott Hughesextern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
1751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1760d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughesstatic r_debug _r_debug = {1, NULL, &rtld_db_dlactivity, RT_CONSISTENT, 0};
177ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughesstatic link_map_t* r_debug_tail = 0;
1781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1793b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughesstatic pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
1801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
181bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void insert_soinfo_into_debug_map(soinfo * info) {
182bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    // Copy the necessary fields into the debug structure.
183ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    link_map_t* map = &(info->link_map);
1841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = info->base;
1851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_name = (char*) info->name;
1865cf640c926e7d7b79934c97226433a3dc448e1ebThinker K.F Li    map->l_ld = (uintptr_t)info->dynamic;
1871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Stick the new library at the end of the list.
1891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * gdb tends to care more about libc than it does
1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * about leaf libraries, and ordering it this way
1911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * reduces the back-and-forth over the wire.
1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
1931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (r_debug_tail) {
1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        r_debug_tail->l_next = map;
1951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = r_debug_tail;
1961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
1971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    } else {
1981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        _r_debug.r_map = map;
1991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = 0;
2001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
205bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void remove_soinfo_from_debug_map(soinfo* info) {
206ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    link_map_t* map = &(info->link_map);
2075e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
208bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (r_debug_tail == map) {
2095e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        r_debug_tail = map->l_prev;
210bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2115e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
212bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_prev) {
213bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_prev->l_next = map->l_next;
214bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
215bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_next) {
216bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_next->l_prev = map->l_prev;
217bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2185e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2195e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
220bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_load(soinfo* info) {
2211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (info->flags & FLAG_EXE) {
2221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        // GDB already knows about the main executable
2231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
2241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2263b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes    ScopedPthreadMutexLocker locker(&gDebugMutex);
2271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_ADD;
2291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(info);
2321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_CONSISTENT;
2341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2355e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2365e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
237bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_unload(soinfo* info) {
2385e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    if (info->flags & FLAG_EXE) {
2395e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        // GDB already knows about the main executable
2405e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        return;
2415e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    }
2425e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2433b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes    ScopedPthreadMutexLocker locker(&gDebugMutex);
2445e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2455e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    _r_debug.r_state = RT_DELETE;
2465e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2475e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2485e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    remove_soinfo_from_debug_map(info);
2495e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2505e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    _r_debug.r_state = RT_CONSISTENT;
2515e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
25418a206c81d9743481e364384affd43306911283dElliott Hughesvoid notify_gdb_of_libraries() {
2551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_ADD;
2561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_CONSISTENT;
2581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
261ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic bool ensure_free_list_non_empty() {
262ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (gSoInfoFreeList != NULL) {
263ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return true;
264ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
265ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
266ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Allocate a new pool.
267ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
268ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn                                                              PROT_READ|PROT_WRITE,
269ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn                                                              MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
270ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (pool == MAP_FAILED) {
271ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return false;
272ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
273ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
274ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Add the pool to our list of pools.
275ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  pool->next = gSoInfoPools;
276ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  gSoInfoPools = pool;
277ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
278ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Chain the entries in the new pool onto the free list.
279ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  gSoInfoFreeList = &pool->info[0];
280ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo* next = NULL;
281ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
282ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    pool->info[i].next = next;
283ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    next = &pool->info[i];
284ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
285ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
286ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  return true;
287ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn}
2881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
289d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic void set_soinfo_pool_protection(int protection) {
290d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
291d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (mprotect(p, sizeof(*p), protection) == -1) {
292d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      abort(); // Can't happen.
293d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    }
294d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
295d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
296d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
297ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic soinfo* soinfo_alloc(const char* name) {
298ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (strlen(name) >= SOINFO_NAME_LEN) {
299ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    DL_ERR("library name \"%s\" too long", name);
300ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return NULL;
301ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
302ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
303ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (!ensure_free_list_non_empty()) {
304ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    DL_ERR("out of memory when loading \"%s\"", name);
305ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return NULL;
306ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
3071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
308ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Take the head element off the free list.
309ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo* si = gSoInfoFreeList;
310ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  gSoInfoFreeList = gSoInfoFreeList->next;
3111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
312ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Initialize the new element.
313ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  memset(si, 0, sizeof(soinfo));
314ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  strlcpy(si->name, name, sizeof(si->name));
315ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext->next = si;
316ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext = si;
3171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
318ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("name %s: allocated soinfo @ %p", name, si);
319ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  return si;
3201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void soinfo_free(soinfo* si)
3231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si == NULL) {
3254688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return;
3264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
3274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *prev = NULL, *trav;
3291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
330ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("name %s: freeing soinfo @ %p", si->name, si);
3311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
332d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (trav = solist; trav != NULL; trav = trav->next) {
3331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (trav == si)
3341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
3351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        prev = trav;
3361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (trav == NULL) {
338d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        /* si was not in solist */
3394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("name \"%s\" is not in solist!", si->name);
3401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
3411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
343be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    /* prev will never be NULL, because the first entry in solist is
3441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       always the static libdl_info.
3451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    */
3461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    prev->next = si->next;
347d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si == sonext) {
348d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        sonext = prev;
349d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
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;
428ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        dl_info.dlpi_addr = si->link_map.l_addr;
429ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        dl_info.dlpi_name = si->link_map.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
442d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic Elf32_Sym* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
443d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    Elf32_Sym* s;
444d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    Elf32_Sym* symtab = si->symtab;
445d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    const char* strtab = si->strtab;
4461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned n;
4471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
448ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE_TYPE(LOOKUP, "SEARCH %s in %s@0x%08x %08x %d",
4491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               name, si->name, si->base, hash, hash % si->nbucket);
4501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    n = hash % si->nbucket;
4511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
452d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
4531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        s = symtab + n;
454d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (strcmp(strtab + s->st_name, name)) continue;
4551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
456e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            /* only concern ourselves with global and weak symbol definitions */
4571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(ELF32_ST_BIND(s->st_info)){
4581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case STB_GLOBAL:
459e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan        case STB_WEAK:
460d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (s->st_shndx == SHN_UNDEF) {
461439fa8ea8c33a7945258dfe84a71ab2665efaa9aRobin Burchell                continue;
462d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            }
4631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
464ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(LOOKUP, "FOUND %s in %s (%08x) %d",
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
473d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic unsigned elfhash(const char* _name) {
474d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    const unsigned char* name = (const unsigned char*) _name;
4751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned h = 0, g;
4761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    while(*name) {
4781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h = (h << 4) + *name++;
4791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        g = h & 0xf0000000;
4801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g;
4811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g >> 24;
4821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return h;
4841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
486d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic Elf32_Sym* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
487943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
488d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    Elf32_Sym* s = NULL;
4896ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
490c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin    if (si != NULL && somain != NULL) {
4915ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
4925ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        /*
493c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * Local scope is executable scope. Just start looking into it right away
494c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * for the shortcut.
4955ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel         */
4965ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
497c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        if (si == somain) {
498c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
4995ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (s != NULL) {
500c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
5015ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                goto done;
5025ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
503c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        } else {
504c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
505c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
506c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
507c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with symbolic relocations disabled, the
508c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * first place to look to resolve external references is the main
509c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * executable.
510c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
511c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
512c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (!si->has_DT_SYMBOLIC) {
513ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s",
51461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
515c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
516c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
517c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
518c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
519c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
520c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5215ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
522c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Look for symbols in the local scope (the object who is
523c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * searching). This happens with C++ templates on i386 for some
524c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * reason.
525c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             *
526c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Notes on weak symbols:
527c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * The ELF specs are ambiguous about treatment of weak definitions in
528c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * dynamic linking.  Some systems return the first definition found
529c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * and some the first non-weak definition.   This is system dependent.
530c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Here we return the first definition found for simplicity.  */
531c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
532c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
533c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (s != NULL) {
534c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
535c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                goto done;
536c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
537c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
538c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
539c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with -Bsymbolic and symbol is not found
540c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * in the local scope, try to find the symbol in the main executable.
541c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
542c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
543c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (si->has_DT_SYMBOLIC) {
544ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s after local scope",
54561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
546c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
547c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
548c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
549c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
550c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
551c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5525ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
553d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    }
5546ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5554fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* Next, look for it in the preloads list */
556d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (int i = 0; gLdPreloads[i] != NULL; i++) {
557d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name);
558d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (s != NULL) {
559d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            *lsi = gLdPreloads[i];
5604fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            goto done;
5615ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5624fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
5634fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
564d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (int i = 0; needed[i] != NULL; i++) {
565ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        DEBUG("%s: looking up %s in %s",
56661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes              si->name, name, needed[i]->name);
5675ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_elf_lookup(needed[i], elf_hash, name);
5685ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if (s != NULL) {
5695ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            *lsi = needed[i];
57012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            goto done;
5715ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5726ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5736ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5746ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevdone:
575d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (s != NULL) {
57661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes        TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = 0x%08x, "
577ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                   "found in %s, base = 0x%08x, load bias = 0x%08x",
57861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                   si->name, name, s->st_value,
5795ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                   (*lsi)->name, (*lsi)->base, (*lsi)->load_bias);
5806ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        return s;
5816ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5826ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
583943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
5846ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev}
5856ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
586d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3).  It performs symbol lookup only within the
5876ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev   specified soinfo object and not in any of its dependencies.
588d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom
589d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
590d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   that it should do a breadth first search through the dependency
591d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   tree. This agrees with the ELF spec (aka System V Application
592d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   Binary Interface) where in Chapter 5 it discuss resolving "Shared
593d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   Object Dependencies" in breadth first search order.
5946ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
595d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian CarlstromElf32_Sym* dlsym_handle_lookup(soinfo* si, const char* name)
5961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
59716084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    return soinfo_elf_lookup(si, elfhash(name), name);
5981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
600d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3) to performs a global symbol lookup. If the
601d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   start value is null (for RTLD_DEFAULT), the search starts at the
602d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   beginning of the global solist. Otherwise the search starts at the
603d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   specified soinfo (for RTLD_NEXT).
6046ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
605d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian CarlstromElf32_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
606cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  unsigned elf_hash = elfhash(name);
6071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
608cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (start == NULL) {
609cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    start = solist;
610cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6111698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer
612cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  Elf32_Sym* s = NULL;
613cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
614cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    s = soinfo_elf_lookup(si, elf_hash, name);
615cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (s != NULL) {
616cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      *found = si;
617cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      break;
6181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
619cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
621cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (s != NULL) {
622ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE_TYPE(LOOKUP, "%s s->st_value = 0x%08x, found->base = 0x%08x",
623cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes               name, s->st_value, (*found)->base);
624cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
626cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  return s;
6271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
629fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Chengsoinfo* find_containing_library(const void* p) {
630fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  Elf32_Addr address = reinterpret_cast<Elf32_Addr>(p);
631fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (soinfo* si = solist; si != NULL; si = si->next) {
632fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (address >= si->base && address - si->base < si->size) {
633fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return si;
634e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
635fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
636fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return NULL;
637e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
638e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
639d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian CarlstromElf32_Sym* dladdr_find_symbol(soinfo* si, const void* addr) {
640fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  Elf32_Addr soaddr = reinterpret_cast<Elf32_Addr>(addr) - si->base;
641fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
642fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // Search the library's symbol table for any defined symbol which
643fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // contains this address.
644fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (size_t i = 0; i < si->nchain; ++i) {
645fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    Elf32_Sym* sym = &si->symtab[i];
646fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (sym->st_shndx != SHN_UNDEF &&
647fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr >= sym->st_value &&
648fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr < sym->st_value + sym->st_size) {
649fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return sym;
650e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
651fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
652e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
653fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return NULL;
654e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
655e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
6561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0
657d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic void dump(soinfo* si)
6581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
659d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    Elf32_Sym* s = si->symtab;
660d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (unsigned n = 0; n < si->nchain; n++) {
661ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("%04d> %08x: %02x %04x %08x %08x %s", n, s,
6621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               s->st_info, s->st_shndx, s->st_value, s->st_size,
6631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               si->strtab + s->st_name);
6641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        s++;
6651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
6681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
669124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library_on_path(const char* name, const char* const paths[]) {
670124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  char buf[512];
671124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  for (size_t i = 0; paths[i] != NULL; ++i) {
6721e980b6bc8315d00a07312b25486531247abd98cElliott Hughes    int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
673124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
674ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes      PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
675124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      continue;
6761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
677124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
678124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (fd != -1) {
679124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      return fd;
680124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
681124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
682124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return -1;
6831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
685124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library(const char* name) {
686ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ opening %s ]", name);
6871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
688124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // If the name contains a slash, we should attempt to open it directly and not search the paths.
689124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (strchr(name, '/') != NULL) {
6906971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
6916971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    if (fd != -1) {
6926971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes      return fd;
6936971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    }
6946971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
695124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
6961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
697124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
698124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  int fd = open_library_on_path(name, gLdPaths);
699124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (fd == -1) {
700124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    fd = open_library_on_path(name, gSoPaths);
701124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
702124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return fd;
7031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
705124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic soinfo* load_library(const char* name) {
7064688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Open the file.
707650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    int fd = open_library(name);
708650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    if (fd == -1) {
7094688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("library \"%s\" not found", name);
7101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
7112e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    }
7121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
713650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    // Read the ELF header and load the segments.
714650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    ElfReader elf_reader(name, fd);
715650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    if (!elf_reader.Load()) {
7164688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
71723363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
71823363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner
719650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    const char* bname = strrchr(name, '/');
720650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
721650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    if (si == NULL) {
7224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
72323363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
724650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->base = elf_reader.load_start();
725650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->size = elf_reader.load_size();
726650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->load_bias = elf_reader.load_bias();
727650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->flags = 0;
728650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->entry = 0;
729650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->dynamic = NULL;
730650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phnum = elf_reader.phdr_count();
731650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phdr = elf_reader.loaded_phdr();
732650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    return si;
7331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
73512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvelstatic soinfo *find_loaded_library(const char *name)
7361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
7371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
7386774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    const char *bname;
7396774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
74012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // TODO: don't use basename only for determining libraries
74112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // http://code.google.com/p/android/issues/detail?id=6670
74212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
74312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    bname = strrchr(name, '/');
74412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    bname = bname ? bname + 1 : name;
74512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
746d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (si = solist; si != NULL; si = si->next) {
747d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (!strcmp(bname, si->name)) {
74812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            return si;
74912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        }
75012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    }
75112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    return NULL;
75212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel}
75312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
754d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic soinfo* find_library_internal(const char* name) {
755d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (name == NULL) {
756d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return somain;
757d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
758d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
759d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_loaded_library(name);
760d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
761d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_LINKED) {
762d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      return si;
763d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    }
764d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    DL_ERR("OOPS: recursive link to \"%s\"", si->name);
765d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return NULL;
766d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
76712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
768ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ '%s' has not been loaded yet.  Locating...]", name);
769d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  si = load_library(name);
770cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (si == NULL) {
771cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return NULL;
772cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
773cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
774cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // At this point we know that whatever is loaded @ base is a valid ELF
775cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // shared library whose segments are properly mapped in.
776ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ init_library base=0x%08x sz=0x%08x name='%s' ]",
777cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes        si->base, si->size, si->name);
778cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
779cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (!soinfo_link_image(si)) {
780cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    munmap(reinterpret_cast<void*>(si->base), si->size);
781cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    soinfo_free(si);
782cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return NULL;
783d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
7846774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
785d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
786d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
787d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
788d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic soinfo* find_library(const char* name) {
789d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_library_internal(name);
790d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
791ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count++;
792d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
793d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
794d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
795d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
796d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic int soinfo_unload(soinfo* si) {
797ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  if (si->ref_count == 1) {
798ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("unloading '%s'", si->name);
799d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallDestructors();
800d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
8012d4b9b7cff78b9a378d7fc0f28c702dc43002020Brian Carlstrom    for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
802d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom      if (d->d_tag == DT_NEEDED) {
803d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        const char* library_name = si->strtab + d->d_un.d_val;
804d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        soinfo* lsi = find_loaded_library(library_name);
805d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        if (lsi != NULL) {
806ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes          TRACE("%s needs to unload %s", si->name, lsi->name);
807d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          soinfo_unload(lsi);
808d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        } else {
809d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          // TODO: should we return -1 in this case?
810d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          DL_ERR("\"%s\": could not unload dependent library", si->name);
8111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
812d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
8131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
815d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    munmap(reinterpret_cast<void*>(si->base), si->size);
816d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    notify_gdb_of_unload(si);
817d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    soinfo_free(si);
818ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 0;
819d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  } else {
820ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count--;
821ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("not unloading '%s', decrementing ref_count to %d", si->name, si->ref_count);
822d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
823d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return 0;
8241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
826cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesvoid do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
827cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (!get_AT_SECURE()) {
828cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    parse_LD_LIBRARY_PATH(ld_library_path);
829cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
830cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
831cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
832e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughessoinfo* do_dlopen(const char* name, int flags) {
833e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes  if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
834e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    DL_ERR("invalid flags to dlopen: %x", flags);
835e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    return NULL;
836e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes  }
837d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
838d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_library(name);
839d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
840d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
841d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
842d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ);
843d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
844d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
8451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
846d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesint do_dlclose(soinfo* si) {
847d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
848d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  int result = soinfo_unload(si);
849d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ);
850d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return result;
8511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* TODO: don't use unsigned for addrs below. It works, but is not
8541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned
8551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * long.
8561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
857d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic int soinfo_relocate(soinfo* si, Elf32_Rel* rel, unsigned count,
858d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                           soinfo* needed[])
8591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
860d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    Elf32_Sym* symtab = si->symtab;
861d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    const char* strtab = si->strtab;
862d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    Elf32_Sym* s;
863d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    Elf32_Rel* start = rel;
864d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo* lsi;
8651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8664688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    for (size_t idx = 0; idx < count; ++idx, ++rel) {
8671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned type = ELF32_R_TYPE(rel->r_info);
8681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned sym = ELF32_R_SYM(rel->r_info);
869fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        Elf32_Addr reloc = static_cast<Elf32_Addr>(rel->r_offset + si->load_bias);
870fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        Elf32_Addr sym_addr = 0;
871d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        char* sym_name = NULL;
8721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
873ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        DEBUG("Processing '%s' relocation at index %d", si->name, idx);
874d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (type == 0) { // R_*_NONE
875d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            continue;
876d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
877d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (sym != 0) {
878d1b40d8c69dfca94185ea9231f6cb16f70c02e39Dima Zavin            sym_name = (char *)(strtab + symtab[sym].st_name);
8795ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            s = soinfo_do_lookup(si, sym_name, &lsi, needed);
880d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (s == NULL) {
881e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* We only allow an undefined symbol if this is a weak
882e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   reference..   */
883e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                s = &symtab[sym];
884e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
885e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes                    DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
886e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
887e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
888e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
889e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* IHI0044C AAELF 4.5.1.1:
890e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
891e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   Libraries are not searched to resolve weak references.
892e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   It is not an error for a weak reference to remain
893e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   unsatisfied.
894e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
895e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   During linking, the value of an undefined weak reference is:
896e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - Zero if the relocation type is absolute
897e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - The address of the place if the relocation is pc-relative
898bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes                   - The address of nominal base address if the relocation
899e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                     type is base-relative.
900e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                  */
901e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
902e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                switch (type) {
903e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_ARM_LINKER)
904e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_JUMP_SLOT:
905e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_GLOB_DAT:
906e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_ABS32:
907e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_RELATIVE:    /* Don't care. */
908e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#elif defined(ANDROID_X86_LINKER)
909d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                case R_386_JMP_SLOT:
910e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_GLOB_DAT:
911e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_32:
912e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_RELATIVE:    /* Dont' care. */
913e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_*_LINKER */
914e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    /* sym_addr was initialized to be zero above or relocation
915e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       code below does not care about value of sym_addr.
916e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       No need to do anything.  */
917e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
918e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
919e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_X86_LINKER)
920e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_PC32:
921e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    sym_addr = reloc;
922e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
923e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_X86_LINKER */
924e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
925e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_ARM_LINKER)
926e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_COPY:
927e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    /* Fall through.  Can't really copy if weak symbol is
928e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                       not found in run-time.  */
929e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_ARM_LINKER */
930e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                default:
9314688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("unknown weak reloc type %d @ %p (%d)",
9324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                                 type, rel, (int) (rel - start));
933e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
934e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
935e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            } else {
936e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* We got a definition.  */
9371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0
938d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                if ((base == 0) && (si->base != 0)) {
939d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                        /* linking from libraries to main image is bad */
9404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                    DL_ERR("cannot locate \"%s\"...",
9414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                           strtab + symtab[sym].st_name);
942d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                    return -1;
943d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                }
9441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
945fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng                sym_addr = static_cast<Elf32_Addr>(s->st_value + lsi->load_bias);
946d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
947bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocSymbol);
9481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        } else {
949e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            s = NULL;
9501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
9511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* TODO: This is ugly. Split up the relocations by arch into
9531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * different files.
9541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
9551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        switch(type){
9561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_ARM_LINKER)
9571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_JUMP_SLOT:
958bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
9591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
960ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
961fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng            *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
9621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
9631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_GLOB_DAT:
964bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
9651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
966ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
967fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng            *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
9681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
9691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_ABS32:
970bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
9711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
972ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
973fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng            *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr;
9741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
97534ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner        case R_ARM_REL32:
976bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
97734ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            MARK(rel->r_offset);
978ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
97934ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner                       reloc, sym_addr, rel->r_offset, sym_name);
980fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng            *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr - rel->r_offset;
98134ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            break;
9821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#elif defined(ANDROID_X86_LINKER)
983d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case R_386_JMP_SLOT:
984bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
9851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
986ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
987fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng            *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
9881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
9891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_GLOB_DAT:
990bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
9911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
992ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
993fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng            *reinterpret_cast<Elf32_Addr*>(reloc) = sym_addr;
9941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
995d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_MIPS_LINKER)
996d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    case R_MIPS_REL32:
997bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
998d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            MARK(rel->r_offset);
999ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s",
1000d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                       reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
1001d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            if (s) {
1002fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng                *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr;
1003d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            } else {
1004fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng                *reinterpret_cast<Elf32_Addr*>(reloc) += si->base;
1005d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1006d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
10071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_*_LINKER */
10081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
10091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_ARM_LINKER)
10101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_RELATIVE:
10111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#elif defined(ANDROID_X86_LINKER)
10121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_RELATIVE:
10131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_*_LINKER */
1014bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
10151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
10164688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            if (sym) {
101761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                DL_ERR("odd RELATIVE form...");
10181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                return -1;
10191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
1020ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO RELATIVE %08x <- +%08x", reloc, si->base);
1021fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng            *reinterpret_cast<Elf32_Addr*>(reloc) += si->base;
10221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
10231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
10241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_X86_LINKER)
10251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_32:
1026bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
10271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
10281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1029ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
1030fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng            *reinterpret_cast<Elf32_Addr*>(reloc) += sym_addr;
10311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
10321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
10331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_386_PC32:
1034bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
10351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1036ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
103761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                       reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
1038fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng            *reinterpret_cast<Elf32_Addr*>(reloc) += (sym_addr - reloc);
10391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
10401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_X86_LINKER */
10411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
10421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef ANDROID_ARM_LINKER
10431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_COPY:
1044d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            if ((si->flags & FLAG_EXE) == 0) {
1045d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                /*
1046d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1047d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 *
1048d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * Section 4.7.1.10 "Dynamic relocations"
1049d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * R_ARM_COPY may only appear in executable objects where e_type is
1050d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * set to ET_EXEC.
1051d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 *
1052d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1053d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * We should explicitly disallow ET_DYN executables from having
1054d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * R_ARM_COPY relocations.
1055d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 */
1056d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1057d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                return -1;
1058d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            }
1059bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocCopy);
10601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1061ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s", reloc, s->st_size, sym_addr, sym_name);
1062d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            if (reloc == sym_addr) {
10635ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
10645ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
10655ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (src == NULL) {
10665ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
10675ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
10685ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
10695ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (lsi->has_DT_SYMBOLIC) {
10705ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
10715ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                           "library %s (built with -Bsymbolic?)", si->name, lsi->name);
10725ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
10735ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
10745ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (s->st_size < src->st_size) {
10755ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
10765ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                           si->name, s->st_size, src->st_size);
10775ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
10785ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
10795ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
10805ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            } else {
10815ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
1082d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                return -1;
1083d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            }
10841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
10851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_ARM_LINKER */
10861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
10871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        default:
10884688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("unknown reloc type %d @ %p (%d)",
10894688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                   type, rel, (int) (rel - start));
10901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return -1;
10911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
10921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
10931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
10941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
10951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1096d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#ifdef ANDROID_MIPS_LINKER
1097bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic int mips_relocate_got(soinfo* si, soinfo* needed[]) {
1098d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned *got;
1099d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned local_gotno, gotsym, symtabno;
1100d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    Elf32_Sym *symtab, *sym;
1101d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    unsigned g;
1102d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1103d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got;
1104d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    local_gotno = si->mips_local_gotno;
1105d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    gotsym = si->mips_gotsym;
1106d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    symtabno = si->mips_symtabno;
1107d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    symtab = si->symtab;
1108d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1109d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    /*
1110d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * got[0] is address of lazy resolver function
1111d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * got[1] may be used for a GNU extension
1112bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes     * set it to a recognizable address in case someone calls it
1113d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * (should be _rtld_bind_start)
1114d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * FIXME: maybe this should be in a separate routine
1115d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     */
1116d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1117d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if ((si->flags & FLAG_LINKER) == 0) {
1118d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        g = 0;
1119d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        got[g++] = 0xdeadbeef;
1120d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (got[g] & 0x80000000) {
1121d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            got[g++] = 0xdeadfeed;
1122d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1123d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        /*
1124d7daacb46372132ae3f0121647074936c304b572Raghu Gandham         * Relocate the local GOT entries need to be relocated
1125d7daacb46372132ae3f0121647074936c304b572Raghu Gandham         */
1126d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        for (; g < local_gotno; g++) {
1127d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            got[g] += si->load_bias;
1128d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1129d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1130d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1131d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    /* Now for the global GOT entries */
1132d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    sym = symtab + gotsym;
1133d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got + local_gotno;
1134d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    for (g = gotsym; g < symtabno; g++, sym++, got++) {
1135d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        const char* sym_name;
1136d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        Elf32_Sym* s;
1137d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        soinfo* lsi;
1138d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1139d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        /* This is an undefined reference... try to locate it */
1140d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        sym_name = si->strtab + sym->st_name;
11415ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1142d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (s == NULL) {
1143d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* We only allow an undefined symbol if this is a weak
1144d7daacb46372132ae3f0121647074936c304b572Raghu Gandham               reference..   */
1145d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            s = &symtab[g];
1146d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            if (ELF32_ST_BIND(s->st_info) != STB_WEAK) {
11474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("cannot locate \"%s\"...", sym_name);
1148d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                return -1;
1149d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1150d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *got = 0;
1151d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1152d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        else {
1153d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* FIXME: is this sufficient?
1154d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             * For reference see NetBSD link loader
1155d7daacb46372132ae3f0121647074936c304b572Raghu 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
1156d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             */
11575ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel             *got = lsi->load_bias + s->st_value;
1158d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1159d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1160d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    return 0;
1161d7daacb46372132ae3f0121647074936c304b572Raghu Gandham}
1162d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1163d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
11648215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner/* Please read the "Initialization and Termination functions" functions.
11658215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * of the linker design note in bionic/linker/README.TXT to understand
11668215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * what the following code is doing.
11678215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
11688215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * The important things to remember are:
11698215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
11708215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_PREINIT_ARRAY must be called first for executables, and should
11718215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   not appear in shared libraries.
11728215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
11738215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_INIT should be called before DT_INIT_ARRAY if both are present
11748215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
11758215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_FINI should be called after DT_FINI_ARRAY if both are present
11768215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *
11778215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner *   DT_FINI_ARRAY must be parsed in reverse order.
11788215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner */
1179ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughesvoid soinfo::CallArray(const char* array_name UNUSED, linker_function_t* functions, size_t count, bool reverse) {
1180ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  if (functions == NULL) {
1181d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1182d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
11838215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1184ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Calling %s (size %d) @ %p for '%s' ]", array_name, count, functions, name);
11858215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1186ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int begin = reverse ? (count - 1) : 0;
1187ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int end = reverse ? -1 : count;
1188ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int step = reverse ? -1 : 1;
11898215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1190ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  for (int i = begin; i != end; i += step) {
1191ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1192ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    CallFunction("function", functions[i]);
1193d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1194d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1195ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s for '%s' ]", array_name, name);
11961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
11971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1198ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughesvoid soinfo::CallFunction(const char* function_name UNUSED, linker_function_t function) {
1199db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
1200d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1201d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1202d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1203ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
1204d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  function();
1205ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
1206db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes
1207db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1208db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // are still writable. This happens with our debug malloc (see http://b/7941716).
1209db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
12109181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov}
12119181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
1212d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallPreInitConstructors() {
1213d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1214d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
1215e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1216d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallConstructors() {
1217d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (constructors_called) {
1218d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1219d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1220e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1221d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // We set constructors_called before actually calling the constructors, otherwise it doesn't
1222d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // protect against recursive constructor calls. One simple example of constructor recursion
1223d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1224d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 1. The program depends on libc, so libc's constructor is called here.
1225d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1226d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 3. dlopen() calls the constructors on the newly created
1227d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    soinfo for libc_malloc_debug_leak.so.
1228d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 4. The debug .so depends on libc, so CallConstructors is
1229d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    called again with the libc soinfo. If it doesn't trigger the early-
1230d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    out above, the libc constructor will be called again (recursively!).
1231d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  constructors_called = true;
1232d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1233d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (!(flags & FLAG_EXE) && preinit_array) {
1234d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    DL_ERR("shared library \"%s\" has a preinit_array table @ %p", name, preinit_array);
1235d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1236d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
12371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1238d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom  if (dynamic != NULL) {
1239d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (Elf32_Dyn* d = dynamic; d->d_tag != DT_NULL; ++d) {
1240d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom      if (d->d_tag == DT_NEEDED) {
1241d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        const char* library_name = strtab + d->d_un.d_val;
1242d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        soinfo* lsi = find_loaded_library(library_name);
1243d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        if (lsi == NULL) {
1244d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          DL_ERR("\"%s\": could not initialize dependent library", name);
1245d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        } else {
1246d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes          lsi->CallConstructors();
1247d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        }
1248d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
12491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1250d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
12511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1252d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_INIT", init_func);
1253d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
1254e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov}
12558215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1256d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallDestructors() {
1257d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
1258d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_FINI", fini_func);
12591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
12601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with
12621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   /dev/null. */
12635419b9474753d25dff947c7740532f86d130c0beElliott Hughesstatic int nullify_closed_stdio() {
12641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int dev_null, i, status;
12651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int return_value = 0;
12661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
126716084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
12681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null < 0) {
12694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("cannot open /dev/null: %s", strerror(errno));
12701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return -1;
12711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1272ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
12731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If any of the stdio file descriptors is valid and not associated
12751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       with /dev/null, dup /dev/null to it.  */
12761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (i = 0; i < 3; i++) {
12771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* If it is /dev/null already, we are done. */
12784688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (i == dev_null) {
12791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
12804688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
12811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1282ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Nullifying stdio file descriptor %d]", i);
12834688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
12841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12854688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        /* If file is opened, we are good. */
12864688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status != -1) {
12874688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            continue;
12884688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
12891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* The only error we allow is that the file descriptor does not
12911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           exist, in which case we dup /dev/null to it. */
12921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (errno != EBADF) {
12934688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("fcntl failed: %s", strerror(errno));
12941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
12951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
12961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
12971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* Try dupping /dev/null to this stdio file descriptor and
12991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           repeat if there is a signal.  Note that any errors in closing
13001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           the stdio descriptor are lost.  */
13014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
13021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (status < 0) {
13034688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("dup2 failed: %s", strerror(errno));
13041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
13051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
13061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
13071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If /dev/null is not one of the stdio file descriptors, close it. */
13101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null > 2) {
1311ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
13124688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(close(dev_null));
13134688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status == -1) {
13144688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("close failed: %s", strerror(errno));
13151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
13161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
13171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return return_value;
13201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
13211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1322124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic bool soinfo_link_image(soinfo* si) {
1323f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee    /* "base" might wrap around UINT32_MAX. */
1324bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    Elf32_Addr base = si->load_bias;
1325b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    const Elf32_Phdr *phdr = si->phdr;
13261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int phnum = si->phnum;
1327d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
13281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1329b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can't debug anything until the linker is relocated */
1330b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (!relocating_linker) {
1331ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        INFO("[ linking %s ]", si->name);
1332ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        DEBUG("si->base = 0x%08x si->flags = 0x%08x", si->base, si->flags);
1333b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
13341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
133563f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    /* Extract dynamic section */
1336124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    size_t dynamic_count;
1337cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman    Elf32_Word dynamic_flags;
133812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
1339cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman                                   &dynamic_count, &dynamic_flags);
134063f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    if (si->dynamic == NULL) {
1341b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1342124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
1343b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
1344124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
134563f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    } else {
1346b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1347ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("dynamic = %p", si->dynamic);
1348b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
134963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    }
135063f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
135163f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#ifdef ANDROID_ARM_LINKER
135263f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    (void) phdr_table_get_arm_exidx(phdr, phnum, base,
135363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner                                    &si->ARM_exidx, &si->ARM_exidx_count);
135463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif
135563f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
13561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* extract useful information from dynamic section */
1357d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    uint32_t needed_count = 0;
1358d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1359ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        DEBUG("d = %p, d[0](tag) = 0x%08x d[1](val) = 0x%08x", d, d->d_tag, d->d_un.d_val);
1360d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        switch(d->d_tag){
13611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_HASH:
1362d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->nbucket = ((unsigned *) (base + d->d_un.d_ptr))[0];
1363d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->nchain = ((unsigned *) (base + d->d_un.d_ptr))[1];
1364d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->bucket = (unsigned *) (base + d->d_un.d_ptr + 8);
1365d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->chain = (unsigned *) (base + d->d_un.d_ptr + 8 + si->nbucket * 4);
13661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
13671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_STRTAB:
1368d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->strtab = (const char *) (base + d->d_un.d_ptr);
13691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
13701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_SYMTAB:
1371d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->symtab = (Elf32_Sym *) (base + d->d_un.d_ptr);
13721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
13731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTREL:
1374d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val != DT_REL) {
1375124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1376124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
13771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
13781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
13791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_JMPREL:
1380d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->plt_rel = (Elf32_Rel*) (base + d->d_un.d_ptr);
13811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
13821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTRELSZ:
1383d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->plt_rel_count = d->d_un.d_val / sizeof(Elf32_Rel);
13841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
13851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_REL:
1386d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->rel = (Elf32_Rel*) (base + d->d_un.d_ptr);
13871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
13881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_RELSZ:
1389d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->rel_count = d->d_un.d_val / sizeof(Elf32_Rel);
13901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
13911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTGOT:
13921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            /* Save this in case we decide to do lazy binding. We don't yet. */
1393d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->plt_got = (unsigned *)(base + d->d_un.d_ptr);
13941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
13951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_DEBUG:
1396bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_DEBUG entry to the address of _r_debug for GDB
1397cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman            // if the dynamic table is writable
139899c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            if ((dynamic_flags & PF_W) != 0) {
1399d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                d->d_un.d_val = (int) &_r_debug;
140099c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            }
14011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1402ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI         case DT_RELA:
1403124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1404124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
14051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT:
1406ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
1407ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("%s constructors (init func) found at %p", si->name, si->init_func);
14081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI:
1410ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
1411ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("%s destructors (fini func) found at %p", si->name, si->fini_func);
14121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAY:
1414ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
1415ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("%s constructors (init_array) found at %p", si->name, si->init_array);
14161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAYSZ:
1418d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
14191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAY:
1421ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
1422ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("%s destructors (fini_array) found at %p", si->name, si->fini_array);
14231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAYSZ:
1425d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
14261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAY:
1428ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
1429ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("%s constructors (preinit_array) found at %p", si->name, si->preinit_array);
14301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAYSZ:
1432d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf32_Addr);
14331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_TEXTREL:
14355135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            si->has_text_relocations = true;
14361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14375ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_SYMBOLIC:
14385ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            si->has_DT_SYMBOLIC = true;
14395ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
1440d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        case DT_NEEDED:
1441d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            ++needed_count;
1442d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            break;
1443d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom#if defined DT_FLAGS
1444d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        // TODO: why is DT_FLAGS not defined?
14455ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_FLAGS:
1446d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_TEXTREL) {
14475ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_text_relocations = true;
14485ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
1449d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_SYMBOLIC) {
14505ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_DT_SYMBOLIC = true;
14515ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
14525ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
14535ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel#endif
1454d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if defined(ANDROID_MIPS_LINKER)
1455d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_STRSZ:
1456d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_SYMENT:
1457d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_RELENT:
1458d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             break;
1459d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_MAP:
1460bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
1461d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            {
1462d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom              r_debug** dp = (r_debug**) d->d_un.d_ptr;
1463d7daacb46372132ae3f0121647074936c304b572Raghu Gandham              *dp = &_r_debug;
1464d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1465d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1466d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_VERSION:
1467d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_FLAGS:
1468d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_BASE_ADDRESS:
1469d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_UNREFEXTNO:
1470d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1471d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1472d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_SYMTABNO:
1473d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_symtabno = d->d_un.d_val;
1474d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1475d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1476d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_LOCAL_GOTNO:
1477d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_local_gotno = d->d_un.d_val;
1478d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1479d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1480d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_GOTSYM:
1481d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_gotsym = d->d_un.d_val;
1482d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1483d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1484d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        default:
1485ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("Unused DT entry: type 0x%08x arg 0x%08x", d->d_tag, d->d_un.d_val);
1486d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1487d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
14881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1491ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    DEBUG("si->base = 0x%08x, si->strtab = %p, si->symtab = %p",
149261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes          si->base, si->strtab, si->symtab);
14931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1494124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    // Sanity checks.
1495d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (relocating_linker && needed_count != 0) {
1496d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1497d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        return false;
1498d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
1499124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->nbucket == 0) {
1500124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1501124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1502124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1503124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->strtab == 0) {
1504124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1505124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1506124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1507124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->symtab == 0) {
1508124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1509124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
15101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
15111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15124fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* if this is the main executable, then load all of the preloads now */
1513d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_EXE) {
1514d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        memset(gLdPreloads, 0, sizeof(gLdPreloads));
1515d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1516d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            soinfo* lsi = find_library(gLdPreloadNames[i]);
1517d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            if (lsi == NULL) {
1518650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes                strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
15194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1520124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                       gLdPreloadNames[i], si->name, tmp_err_buf);
1521124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
15224fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            }
1523d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            gLdPreloads[i] = lsi;
15244fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        }
15254fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
15264fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1527d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo** needed = (soinfo**) alloca((1 + needed_count) * sizeof(soinfo*));
1528d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo** pneeded = needed;
152912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
1530d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (Elf32_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1531d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (d->d_tag == DT_NEEDED) {
1532d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            const char* library_name = si->strtab + d->d_un.d_val;
1533ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("%s needs %s", si->name, library_name);
1534d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            soinfo* lsi = find_library(library_name);
1535d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            if (lsi == NULL) {
1536650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes                strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
15374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1538d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                       library_name, si->name, tmp_err_buf);
1539124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
15401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
154112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            *pneeded++ = lsi;
15421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
15431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
154412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    *pneeded = NULL;
15451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15465135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
15475135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        /* Unprotect the segments, i.e. make them writable, to allow
15485135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * text relocations to work properly. We will later call
15495135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * phdr_table_protect_segments() after all of them are applied
15505135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * and all constructors are run.
15515135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         */
15525135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
15535135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't unprotect loadable segments for \"%s\": %s",
15545135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1555124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
15565135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
15575135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    }
15585135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich
1559d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si->plt_rel != NULL) {
1560ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        DEBUG("[ relocating %s plt ]", si->name );
1561d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1562124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1563124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
15641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1565d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si->rel != NULL) {
1566ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        DEBUG("[ relocating %s ]", si->name );
1567d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1568124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1569124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
15701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
15711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1572d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#ifdef ANDROID_MIPS_LINKER
1573124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (mips_relocate_got(si, needed)) {
1574124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1575d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1576d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1577d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
15781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_LINKED;
1579ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    DEBUG("[ finished linking %s ]", si->name);
15801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15815135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
15825135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        /* All relocations are done, we can protect our segments back to
15835135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich         * read-only. */
15845135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
15855135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't protect segments for \"%s\": %s",
15865135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1587124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
15885135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
15891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
15901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1591b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can also turn on GNU RELRO protection */
1592b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
15934688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
15944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               si->name, strerror(errno));
1595124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
15969ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich    }
15979ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
15981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    notify_gdb_of_load(si);
1599124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    return true;
16001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
16011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1602468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1603468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and
1604468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs
1605468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point.
1606468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
1607fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Chengstatic Elf32_Addr __linker_init_post_relocation(KernelArgumentBlock& args, Elf32_Addr linker_base) {
160842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    /* NOTE: we store the args pointer on a special location
1609ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       of the temporary TLS area in order to pass it to
1610ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       the C Library's runtime initializer.
1611ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *
1612ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       The initializer must clear the slot and reset the TLS
1613ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       to point to a different location to ensure that no other
1614ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       shared library constructor can access it.
1615ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     */
1616d3920b3a996b358e48232f417aa0a1e44a60f155Elliott Hughes  __libc_init_tls(args);
16171a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov
16181a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING
16191a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    struct timeval t0, t1;
16201a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    gettimeofday(&t0, 0);
16211a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif
16221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
162318a206c81d9743481e364384affd43306911283dElliott Hughes    // Initialize environment functions, and get to the ELF aux vectors table.
162442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    linker_env_init(args);
1625be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
16268d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // If this is a setuid/setgid program, close the security hole described in
16278d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
16288d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    if (get_AT_SECURE()) {
16298d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich        nullify_closed_stdio();
16308d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    }
16318d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich
1632d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    debuggerd_init();
16331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
163418a206c81d9743481e364384affd43306911283dElliott Hughes    // Get a few environment variables.
163561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    const char* LD_DEBUG = linker_env_get("LD_DEBUG");
163661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    if (LD_DEBUG != NULL) {
1637650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes      gLdDebugVerbosity = atoi(LD_DEBUG);
163818a206c81d9743481e364384affd43306911283dElliott Hughes    }
1639be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
164018a206c81d9743481e364384affd43306911283dElliott Hughes    // Normally, these are cleaned by linker_env_init, but the test
164118a206c81d9743481e364384affd43306911283dElliott Hughes    // doesn't cost us anything.
164218a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpath_env = NULL;
164318a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpreload_env = NULL;
164418a206c81d9743481e364384affd43306911283dElliott Hughes    if (!get_AT_SECURE()) {
164518a206c81d9743481e364384affd43306911283dElliott Hughes      ldpath_env = linker_env_get("LD_LIBRARY_PATH");
164618a206c81d9743481e364384affd43306911283dElliott Hughes      ldpreload_env = linker_env_get("LD_PRELOAD");
16471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1649ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    INFO("[ android linker & debugger ]");
16501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
165142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    soinfo* si = soinfo_alloc(args.argv[0]);
165218a206c81d9743481e364384affd43306911283dElliott Hughes    if (si == NULL) {
165318a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
16541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1656d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    /* bootstrap the link map, the main exe always needs to be first */
16571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_EXE;
1658ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    link_map_t* map = &(si->link_map);
16591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = 0;
166142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    map->l_name = args.argv[0];
16621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_prev = NULL;
16631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_next = NULL;
16641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_map = map;
16661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
16671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1668d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    /* gdb expects the linker to be in the debug shared object list.
1669d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * Without this, gdb has trouble locating the linker's ".text"
1670d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * and ".plt" sections. Gdb could also potentially use this to
1671d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1672d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * Don't use soinfo_alloc(), because the linker shouldn't
1673d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * be on the soinfo list.
167406f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     */
1675d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    {
1676d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        static soinfo linker_soinfo;
1677d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
1678d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        linker_soinfo.flags = 0;
1679d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        linker_soinfo.base = linker_base;
1680d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom
1681d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        /*
1682d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         * Set the dynamic field in the link map otherwise gdb will complain with
1683d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         * the following:
1684d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         *   warning: .dynamic section for "/system/bin/linker" is not at the
1685d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         *   expected address (wrong library or version mismatch?)
1686d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         */
1687d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base;
1688d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        Elf32_Phdr *phdr = (Elf32_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff);
1689d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1690d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                                       &linker_soinfo.dynamic, NULL, NULL);
1691d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        insert_soinfo_into_debug_map(&linker_soinfo);
1692d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
16931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
169442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // Extract information passed from the kernel.
169542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->phdr = reinterpret_cast<Elf32_Phdr*>(args.getauxval(AT_PHDR));
169642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->phnum = args.getauxval(AT_PHNUM);
169742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->entry = args.getauxval(AT_ENTRY);
16981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16998180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    /* Compute the value of si->base. We can't rely on the fact that
17008180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * the first entry is the PHDR because this will not be true
17018180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * for certain executables (e.g. some in the NDK unit test suite)
17028180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     */
17038180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    si->base = 0;
1704b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    si->size = phdr_table_get_load_size(si->phdr, si->phnum);
1705bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    si->load_bias = 0;
1706ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    for (size_t i = 0; i < si->phnum; ++i) {
170742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      if (si->phdr[i].p_type == PT_PHDR) {
170842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes        si->load_bias = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_vaddr;
170942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes        si->base = reinterpret_cast<Elf32_Addr>(si->phdr) - si->phdr[i].p_offset;
171042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes        break;
171142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      }
17128180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    }
1713d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    si->dynamic = NULL;
1714ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 1;
17151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17164688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
17174688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_LIBRARY_PATH(ldpath_env);
17184688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_PRELOAD(ldpreload_env);
17194fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
17205ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    somain = si;
17215ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
1722124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (!soinfo_link_image(si)) {
1723650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes        __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
172418a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
17251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
17261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1727d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallPreInitConstructors();
17289181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
1729d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
1730d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        gLdPreloads[i]->CallConstructors();
1731326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng    }
1732326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng
1733d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    /* After the link_image, the si->load_bias is initialized.
1734d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1735d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * We need to update this value for so exe here. So Unwind_Backtrace
1736d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * for some arch like x86 could work correctly within so exe.
17379c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     */
1738c5db969aa4a24a76fc0964f2ff89533e832b47a6Chao-Ying Fu    map->l_addr = si->load_bias;
1739d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
1740e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
17411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING
17421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    gettimeofday(&t1,NULL);
1743ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
17441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
17451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
17461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               ));
17471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
17481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
1749ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
1750bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocAbsolute],
1751bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocRelative],
1752bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocCopy],
1753bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocSymbol]);
17541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
17551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
17561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
17571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned n;
17581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned i;
17591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned count = 0;
1760d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        for (n = 0; n < 4096; n++) {
1761d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (bitmask[n]) {
17621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                unsigned x = bitmask[n];
1763d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                for (i = 0; i < 8; i++) {
1764d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    if (x & 1) {
1765d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                        count++;
1766d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    }
17671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    x >>= 1;
17681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                }
17691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
17701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
1771ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
17721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
17731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
17741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES
17761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    fflush(stdout);
17771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
17781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1779ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ Ready to execute '%s' @ 0x%08x ]", si->name, si->entry);
17801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si->entry;
17811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
1782468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1783bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only
1784bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library
1785bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself.
1786bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *
1787bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input:
1788bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    elf    -> address of ELF header, assumed to be at the start of the file.
1789bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return:
1790bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    load bias, i.e. add the value of any p_vaddr in the file to get
1791bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    the corresponding address in memory.
1792bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */
1793fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Chengstatic Elf32_Addr get_elf_exec_load_bias(const Elf32_Ehdr* elf) {
1794fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  Elf32_Addr        offset     = elf->e_phoff;
1795fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset);
1796fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  const Elf32_Phdr* phdr_end   = phdr_table + elf->e_phnum;
1797fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
1798fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (const Elf32_Phdr* phdr = phdr_table; phdr < phdr_end; phdr++) {
1799fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (phdr->p_type == PT_LOAD) {
1800fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return reinterpret_cast<Elf32_Addr>(elf) + phdr->p_offset - phdr->p_vaddr;
1801bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    }
1802fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
1803fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return 0;
1804bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner}
1805bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
1806468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1807468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This
1808468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and
1809468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation().
1810468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich *
1811468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own
1812468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern
1813468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault.
1814468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
1815fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Chengextern "C" Elf32_Addr __linker_init(void* raw_args) {
181642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  KernelArgumentBlock args(raw_args);
181742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
1818fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  Elf32_Addr linker_addr = args.getauxval(AT_BASE);
181942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
18200d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes  Elf32_Ehdr* elf_hdr = (Elf32_Ehdr*) linker_addr;
18210d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes  Elf32_Phdr* phdr = (Elf32_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff);
182242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
182342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  soinfo linker_so;
182442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  memset(&linker_so, 0, sizeof(soinfo));
182542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
182642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.base = linker_addr;
182742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
182842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
1829d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom  linker_so.dynamic = NULL;
183042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phdr = phdr;
183142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phnum = elf_hdr->e_phnum;
183242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.flags |= FLAG_LINKER;
183342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
183442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  if (!soinfo_link_image(&linker_so)) {
183542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // It would be nice to print an error message, but if the linker
183642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // can't link itself, there's no guarantee that we'll be able to
183742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // call write() (because it involves a GOT reference).
183842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    //
183942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // This situation should never occur unless the linker itself
184042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // is corrupt.
184142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    exit(EXIT_FAILURE);
184242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  }
1843468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
184442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // We have successfully fixed our own relocations. It's safe to run
184542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // the main part of the linker now.
18460d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes  args.abort_message_ptr = &gAbortMessage;
1847fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  Elf32_Addr start_address = __linker_init_post_relocation(args, linker_addr);
18485419b9474753d25dff947c7740532f86d130c0beElliott Hughes
184942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  set_soinfo_pool_protection(PROT_READ);
1850d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
185142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // Return the address that the calling assembly stub should jump to.
185242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  return start_address;
1853468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
1854