linker.cpp revision 12bbb9164578b6512b8b07a3fb093858244b7c7b
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>
320266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes#include <inttypes.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.
43eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/bionic_tls.h"
44eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/KernelArgumentBlock.h"
45eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott 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
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Do NOT use malloc() and friends or pthread_*() code here.
551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Don't use printf() either; it's caused mysterious memory
561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * corruption in the past.
571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The linker runs before we bring up libc and it's easiest
581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to make sure it does not depend on any complex libc features
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * open issues / todo:
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - are we doing everything we should for ARM_COPY relocations?
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - cleaner error reporting
641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - after linking, set as much stuff as possible to READONLY
651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   and NOEXEC
664688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes */
671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
68124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic bool soinfo_link_image(soinfo* si);
690266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
71ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous
72ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn// maps, each a single page in size. The pages are broken up into as many struct soinfo
73ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn// objects as will fit, and they're all threaded together on a free list.
74ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo))
75ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstruct soinfo_pool_t {
76ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo_pool_t* next;
77ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo info[SOINFO_PER_POOL];
78ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn};
79ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic struct soinfo_pool_t* gSoInfoPools = NULL;
80ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic soinfo* gSoInfoFreeList = NULL;
81ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
82d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* solist = &libdl_info;
83d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* sonext = &libdl_info;
84d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* somain; /* main process, always the one after libdl_info */
851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
86a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughesstatic const char* const gDefaultLdPaths[] = {
874eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__LP64__)
88011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes  "/vendor/lib64",
89011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes  "/system/lib64",
90011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes#else
91124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  "/vendor/lib",
92124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  "/system/lib",
93011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes#endif
94124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  NULL
95124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes};
96124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes
97a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPATH_BUFSIZE (LDPATH_MAX*64)
98a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPATH_MAX 8
99a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes
100a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
101a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPRELOAD_MAX 8
102a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes
103124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic char gLdPathsBuffer[LDPATH_BUFSIZE];
104124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* gLdPaths[LDPATH_MAX + 1];
105bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
106124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
107124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
1084fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
109d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
1104fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
111650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes__LIBC_HIDDEN__ int gLdDebugVerbosity;
1121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1130d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes__LIBC_HIDDEN__ abort_msg_t* gAbortMessage = NULL; // For debuggerd.
1140d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes
115bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesenum RelocationKind {
116bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocAbsolute = 0,
117bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocRelative,
118bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocCopy,
119bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocSymbol,
120bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocMax
121bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
122be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
1231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
124bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstruct linker_stats_t {
125bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    int count[kRelocMax];
126bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
127bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
128bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic linker_stats_t linker_stats;
129bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
130bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind kind) {
131bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    ++linker_stats.count[kind];
132bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
133bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
134bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind) {
135bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
1361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
139bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic unsigned bitmask[4096];
140e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__LP64__)
141e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#define MARK(offset) \
142e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    do { \
143e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        if ((((offset) >> 12) >> 5) < 4096) \
144e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            bitmask[((offset) >> 12) >> 5] |= (1 << (((offset) >> 12) & 31)); \
145faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    } while (0)
146e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#else
147bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(offset) \
148bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    do { \
149bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
150faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    } while (0)
151e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#endif
152bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
153bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(x) do {} while (0)
1541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// You shouldn't try to call memory-allocating functions in the dynamic linker.
1574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Guard against the most obvious ones.
1588f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#define DISALLOW_ALLOCATION(return_type, name, ...) \
1598f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    return_type name __VA_ARGS__ \
1608f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    { \
1614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
1628f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        __libc_format_log(ANDROID_LOG_FATAL, "linker", "%s", msg); \
1638f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        write(2, msg, strlen(msg)); \
1648f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        abort(); \
1654688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
166812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, malloc, (size_t u __unused));
167812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void, free, (void* u __unused));
168812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
169812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
1702e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1710353195f344666256dba474a15c9ba22cf0cccc9Dima Zavinstatic char tmp_err_buf[768];
1722e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavinstatic char __linker_dl_err_buf[768];
1732e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
174650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hugheschar* linker_get_error_buffer() {
1755419b9474753d25dff947c7740532f86d130c0beElliott Hughes  return &__linker_dl_err_buf[0];
1762e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin}
1772e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
178650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughessize_t linker_get_error_buffer_size() {
179650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  return sizeof(__linker_dl_err_buf);
180650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes}
181650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
1821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
1831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This function is an empty stub where GDB locates a breakpoint to get notified
1841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about linker activity.
1851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1865419b9474753d25dff947c7740532f86d130c0beElliott Hughesextern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
1871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1883a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic r_debug _r_debug = {1, NULL, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
1893a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic link_map* r_debug_tail = 0;
1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1913b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughesstatic pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1933a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic void insert_soinfo_into_debug_map(soinfo* info) {
194bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    // Copy the necessary fields into the debug structure.
1953a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(info->link_map_head);
196ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov    map->l_addr = info->load_bias;
197faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    map->l_name = reinterpret_cast<char*>(info->name);
1983a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    map->l_ld = info->dynamic;
1991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Stick the new library at the end of the list.
2011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * gdb tends to care more about libc than it does
2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * about leaf libraries, and ordering it this way
2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * reduces the back-and-forth over the wire.
2041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (r_debug_tail) {
2061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        r_debug_tail->l_next = map;
2071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = r_debug_tail;
2081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    } else {
2101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        _r_debug.r_map = map;
2111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = 0;
2121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
2151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
217bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void remove_soinfo_from_debug_map(soinfo* info) {
2183a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(info->link_map_head);
2195e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
220bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (r_debug_tail == map) {
2215e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        r_debug_tail = map->l_prev;
222bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2235e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
224bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_prev) {
225bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_prev->l_next = map->l_next;
226bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
227bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_next) {
228bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_next->l_prev = map->l_prev;
229bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2305e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2315e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
232bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_load(soinfo* info) {
2331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (info->flags & FLAG_EXE) {
2341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        // GDB already knows about the main executable
2351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
2361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2383b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes    ScopedPthreadMutexLocker locker(&gDebugMutex);
2391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2403a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_ADD;
2411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(info);
2441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2453a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_CONSISTENT;
2461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2475e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2485e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
249bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_unload(soinfo* info) {
2505e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    if (info->flags & FLAG_EXE) {
2515e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        // GDB already knows about the main executable
2525e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        return;
2535e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    }
2545e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2553b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes    ScopedPthreadMutexLocker locker(&gDebugMutex);
2565e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2573a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_DELETE;
2585e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2595e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2605e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    remove_soinfo_from_debug_map(info);
2615e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2623a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_CONSISTENT;
2635e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
26618a206c81d9743481e364384affd43306911283dElliott Hughesvoid notify_gdb_of_libraries() {
2673a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  _r_debug.r_state = r_debug::RT_ADD;
2683a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  rtld_db_dlactivity();
2693a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  _r_debug.r_state = r_debug::RT_CONSISTENT;
2703a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  rtld_db_dlactivity();
2711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
273ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic bool ensure_free_list_non_empty() {
274ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (gSoInfoFreeList != NULL) {
275ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return true;
276ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
277ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
278ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Allocate a new pool.
279ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
280ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn                                                              PROT_READ|PROT_WRITE,
281ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn                                                              MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
282ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (pool == MAP_FAILED) {
283ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return false;
284ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
285ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
286ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Add the pool to our list of pools.
287ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  pool->next = gSoInfoPools;
288ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  gSoInfoPools = pool;
289ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
290ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Chain the entries in the new pool onto the free list.
291ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  gSoInfoFreeList = &pool->info[0];
292ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo* next = NULL;
293ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
294ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    pool->info[i].next = next;
295ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    next = &pool->info[i];
296ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
297ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
298ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  return true;
299ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn}
3001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
301d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic void set_soinfo_pool_protection(int protection) {
302d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
303d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (mprotect(p, sizeof(*p), protection) == -1) {
304d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      abort(); // Can't happen.
305d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    }
306d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
307d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
308d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
309ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic soinfo* soinfo_alloc(const char* name) {
310ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (strlen(name) >= SOINFO_NAME_LEN) {
311ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    DL_ERR("library name \"%s\" too long", name);
312ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return NULL;
313ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
314ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
315ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (!ensure_free_list_non_empty()) {
316ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    DL_ERR("out of memory when loading \"%s\"", name);
317ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return NULL;
318ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
3191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
320ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Take the head element off the free list.
321ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo* si = gSoInfoFreeList;
322ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  gSoInfoFreeList = gSoInfoFreeList->next;
3231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
324ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Initialize the new element.
325ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  memset(si, 0, sizeof(soinfo));
326ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  strlcpy(si->name, name, sizeof(si->name));
327ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext->next = si;
328ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext = si;
3291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
330ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("name %s: allocated soinfo @ %p", name, si);
331ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  return si;
3321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
334faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesstatic void soinfo_free(soinfo* si) {
3354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si == NULL) {
3364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return;
3374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
3384688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *prev = NULL, *trav;
3401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
341ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("name %s: freeing soinfo @ %p", si->name, si);
3421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
343d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (trav = solist; trav != NULL; trav = trav->next) {
3441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (trav == si)
3451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
3461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        prev = trav;
3471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (trav == NULL) {
349d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        /* si was not in solist */
3504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("name \"%s\" is not in solist!", si->name);
3511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
3521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
354be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    /* prev will never be NULL, because the first entry in solist is
3551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       always the static libdl_info.
3561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    */
3571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    prev->next = si->next;
358d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si == sonext) {
359d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        sonext = prev;
360d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
361ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    si->next = gSoInfoFreeList;
362ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    gSoInfoFreeList = si;
3631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
365cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
366cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_path(const char* path, const char* delimiters,
367cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes                       const char** array, char* buf, size_t buf_size, size_t max_count) {
368cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (path == NULL) {
369cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return;
370cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
371cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
372cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t len = strlcpy(buf, path, buf_size);
373cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
374cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t i = 0;
375cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  char* buf_p = buf;
376cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
377cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (*array[i] != '\0') {
378cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      ++i;
379cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    }
380cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
381cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
382cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // Forget the last path if we had to truncate; this occurs if the 2nd to
383cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // last char isn't '\0' (i.e. wasn't originally a delimiter).
384cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
385cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    array[i - 1] = NULL;
386cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  } else {
387cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    array[i] = NULL;
388cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
389cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
390cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
391cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_LIBRARY_PATH(const char* path) {
392cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  parse_path(path, ":", gLdPaths,
393cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes             gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
394cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
395cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
396cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_PRELOAD(const char* path) {
397cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // We have historically supported ':' as well as ' ' in LD_PRELOAD.
398cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  parse_path(path, " :", gLdPreloadNames,
399cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes             gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
400cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
401cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
4024eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
4034688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
4041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* For a given PC, find the .so that it belongs to.
4051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the base address of the .ARM.exidx section
4061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for that .so, and the number of 8-byte entries
4071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in that section (via *pcount).
4081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Intended to be called by libc's __gnu_Unwind_Find_exidx().
4101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4113b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes * This function is exposed via dlfcn.cpp and libdl.so.
4121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
413faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
4141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned addr = (unsigned)pc;
4151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
416faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    for (soinfo* si = solist; si != 0; si = si->next) {
417468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if ((addr >= si->base) && (addr < (si->base + si->size))) {
418468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            *pcount = si->ARM_exidx_count;
419f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            return (_Unwind_Ptr)si->ARM_exidx;
4201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
4211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
422faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    *pcount = 0;
4231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return NULL;
4241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4254688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
42624053a461e7a20f34002262c1bb122023134989dChristopher Ferris#endif
4274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
4281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Here, we only have to provide a callback to iterate across all the
4291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * loaded libraries. gcc_eh does the rest. */
430faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesint dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
4311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int rv = 0;
432bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    for (soinfo* si = solist; si != NULL; si = si->next) {
433bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        dl_phdr_info dl_info;
4343a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes        dl_info.dlpi_addr = si->link_map_head.l_addr;
4353a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes        dl_info.dlpi_name = si->link_map_head.l_name;
4361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phdr = si->phdr;
4371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phnum = si->phnum;
438bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        rv = cb(&dl_info, sizeof(dl_phdr_info), data);
439bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        if (rv != 0) {
4401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
441bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        }
4421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return rv;
4441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
4460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Sym)* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
4470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* symtab = si->symtab;
4480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  const char* strtab = si->strtab;
4491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4500266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd",
4510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes             name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
4521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4530266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
4540266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s = symtab + n;
4550266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    if (strcmp(strtab + s->st_name, name)) continue;
4561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4570266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    /* only concern ourselves with global and weak symbol definitions */
4580266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    switch (ELF_ST_BIND(s->st_info)) {
4590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      case STB_GLOBAL:
4600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      case STB_WEAK:
4610266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if (s->st_shndx == SHN_UNDEF) {
4620266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        continue;
4630266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      }
4641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4650266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
4660266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                 name, si->name, reinterpret_cast<void*>(s->st_value),
4670266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                 static_cast<size_t>(s->st_size));
4680266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      return s;
4691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4700266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  }
4711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4720266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  return NULL;
4731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
475d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic unsigned elfhash(const char* _name) {
476faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    const unsigned char* name = reinterpret_cast<const unsigned char*>(_name);
4771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned h = 0, g;
4781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
479faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    while (*name) {
4801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h = (h << 4) + *name++;
4811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        g = h & 0xf0000000;
4821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g;
4831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g >> 24;
4841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return h;
4861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4880266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Sym)* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
489943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
4900266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s = NULL;
4916ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
492c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin    if (si != NULL && somain != NULL) {
4935ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        /*
494c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * Local scope is executable scope. Just start looking into it right away
495c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * for the shortcut.
4965ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel         */
4975ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
498c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        if (si == somain) {
499c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
5005ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (s != NULL) {
501c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
5025ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                goto done;
5035ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
504c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        } else {
505c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
506c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
507c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
508c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with symbolic relocations disabled, the
509c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * first place to look to resolve external references is the main
510c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * executable.
511c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
512c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
513c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (!si->has_DT_SYMBOLIC) {
514ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s",
51561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
516c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
517c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
518c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
519c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
520c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
521c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5225ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
523c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Look for symbols in the local scope (the object who is
5244eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes             * searching). This happens with C++ templates on x86 for some
525c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * reason.
526c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             *
527c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Notes on weak symbols:
528c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * The ELF specs are ambiguous about treatment of weak definitions in
529c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * dynamic linking.  Some systems return the first definition found
530c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * and some the first non-weak definition.   This is system dependent.
531c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Here we return the first definition found for simplicity.  */
532c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
533c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
534c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (s != NULL) {
535c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
536c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                goto done;
537c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
538c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
539c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
540c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with -Bsymbolic and symbol is not found
541c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * in the local scope, try to find the symbol in the main executable.
542c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
543c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
544c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (si->has_DT_SYMBOLIC) {
545ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s after local scope",
54661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
547c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
548c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
549c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
550c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
551c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
552c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5535ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
554d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    }
5556ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5564fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* Next, look for it in the preloads list */
557d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (int i = 0; gLdPreloads[i] != NULL; i++) {
558d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name);
559d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (s != NULL) {
560d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            *lsi = gLdPreloads[i];
5614fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            goto done;
5625ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5634fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
5644fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
565d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (int i = 0; needed[i] != NULL; i++) {
566ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        DEBUG("%s: looking up %s in %s",
56761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes              si->name, name, needed[i]->name);
5685ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_elf_lookup(needed[i], elf_hash, name);
5695ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if (s != NULL) {
5705ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            *lsi = needed[i];
57112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            goto done;
5725ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5736ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5746ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5756ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevdone:
576d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (s != NULL) {
577c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
578c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   "found in %s, base = %p, load bias = %p",
579c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   si->name, name, reinterpret_cast<void*>(s->st_value),
580c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   (*lsi)->name, reinterpret_cast<void*>((*lsi)->base),
581c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   reinterpret_cast<void*>((*lsi)->load_bias));
5826ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        return s;
5836ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5846ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
585943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
5866ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev}
5876ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
588d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3).  It performs symbol lookup only within the
5896ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev   specified soinfo object and not in any of its dependencies.
590d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom
591d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
592d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   that it should do a breadth first search through the dependency
593d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   tree. This agrees with the ELF spec (aka System V Application
594d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   Binary Interface) where in Chapter 5 it discuss resolving "Shared
595d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   Object Dependencies" in breadth first search order.
5966ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
5970266ae5f884d72da58f33a072e865ba131234a5eElliott HughesElfW(Sym)* dlsym_handle_lookup(soinfo* si, const char* name) {
59816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    return soinfo_elf_lookup(si, elfhash(name), name);
5991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
601d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3) to performs a global symbol lookup. If the
602d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   start value is null (for RTLD_DEFAULT), the search starts at the
603d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   beginning of the global solist. Otherwise the search starts at the
604d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   specified soinfo (for RTLD_NEXT).
6056ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
6060266ae5f884d72da58f33a072e865ba131234a5eElliott HughesElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
607cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  unsigned elf_hash = elfhash(name);
6081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
609cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (start == NULL) {
610cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    start = solist;
611cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6121698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer
6130266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* s = NULL;
614cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
615cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    s = soinfo_elf_lookup(si, elf_hash, name);
616cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (s != NULL) {
617cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      *found = si;
618cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      break;
6191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
620cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
622cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (s != NULL) {
623c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
624c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes               name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
625cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
627cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  return s;
6281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
630fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Chengsoinfo* find_containing_library(const void* p) {
6310266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
632fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (soinfo* si = solist; si != NULL; si = si->next) {
633fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (address >= si->base && address - si->base < si->size) {
634fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return si;
635e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
636fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
637fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return NULL;
638e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
639e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
6400266ae5f884d72da58f33a072e865ba131234a5eElliott HughesElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr) {
6410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - si->base;
642fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
643fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // Search the library's symbol table for any defined symbol which
644fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // contains this address.
645fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (size_t i = 0; i < si->nchain; ++i) {
6460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* sym = &si->symtab[i];
647fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (sym->st_shndx != SHN_UNDEF &&
648fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr >= sym->st_value &&
649fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr < sym->st_value + sym->st_size) {
650fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return sym;
651e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
652fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
653e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
654fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return NULL;
655e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
656e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
657124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library_on_path(const char* name, const char* const paths[]) {
658124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  char buf[512];
659124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  for (size_t i = 0; paths[i] != NULL; ++i) {
6601e980b6bc8315d00a07312b25486531247abd98cElliott Hughes    int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
661124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
662ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes      PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
663124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      continue;
6641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
665124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
666124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (fd != -1) {
667124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      return fd;
668124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
669124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
670124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return -1;
6711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
673124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library(const char* name) {
674ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ opening %s ]", name);
6751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
676124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // If the name contains a slash, we should attempt to open it directly and not search the paths.
677124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (strchr(name, '/') != NULL) {
6786971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
6796971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    if (fd != -1) {
6806971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes      return fd;
6816971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    }
6826971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
683124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
6841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
685124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
686124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  int fd = open_library_on_path(name, gLdPaths);
687124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (fd == -1) {
688a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes    fd = open_library_on_path(name, gDefaultLdPaths);
689124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
690124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return fd;
6911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
69312bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)static soinfo* load_library(const char* name, const android_dlextinfo* extinfo) {
6944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Open the file.
695650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    int fd = open_library(name);
696650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    if (fd == -1) {
6974688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("library \"%s\" not found", name);
6981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
6992e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    }
7001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
701650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    // Read the ELF header and load the segments.
702650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    ElfReader elf_reader(name, fd);
70312bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)    if (!elf_reader.Load(extinfo)) {
7044688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
70523363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
70623363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner
707650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    const char* bname = strrchr(name, '/');
708650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
709650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    if (si == NULL) {
7104688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
71123363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
712650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->base = elf_reader.load_start();
713650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->size = elf_reader.load_size();
714650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->load_bias = elf_reader.load_bias();
715650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->flags = 0;
716650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->entry = 0;
717650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->dynamic = NULL;
718650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phnum = elf_reader.phdr_count();
719650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phdr = elf_reader.loaded_phdr();
720650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    return si;
7211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
723faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesstatic soinfo *find_loaded_library(const char* name) {
72412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // TODO: don't use basename only for determining libraries
72512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // http://code.google.com/p/android/issues/detail?id=6670
72612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
727faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    const char* bname = strrchr(name, '/');
72812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    bname = bname ? bname + 1 : name;
72912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
730faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    for (soinfo* si = solist; si != NULL; si = si->next) {
731d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (!strcmp(bname, si->name)) {
73212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            return si;
73312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        }
73412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    }
73512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    return NULL;
73612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel}
73712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
73812bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)static soinfo* find_library_internal(const char* name, const android_dlextinfo* extinfo) {
739d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (name == NULL) {
740d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return somain;
741d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
742d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
743d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_loaded_library(name);
744d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
745d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_LINKED) {
746d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      return si;
747d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    }
748d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    DL_ERR("OOPS: recursive link to \"%s\"", si->name);
749d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return NULL;
750d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
75112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
752ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ '%s' has not been loaded yet.  Locating...]", name);
75312bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)  si = load_library(name, extinfo);
754cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (si == NULL) {
755cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return NULL;
756cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
757cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
758cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // At this point we know that whatever is loaded @ base is a valid ELF
759cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // shared library whose segments are properly mapped in.
7605ceb889ac9a36e74e4e00c34c57cbb58d73e228cWeiwu Chen  TRACE("[ find_library_internal base=%p size=%zu name='%s' ]",
761c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        reinterpret_cast<void*>(si->base), si->size, si->name);
762cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
763cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (!soinfo_link_image(si)) {
764cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    munmap(reinterpret_cast<void*>(si->base), si->size);
765cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    soinfo_free(si);
766cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return NULL;
767d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
7686774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
769d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
770d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
771d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
77212bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)static soinfo* find_library(const char* name, const android_dlextinfo* extinfo) {
77312bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)  soinfo* si = find_library_internal(name, extinfo);
774d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
775ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count++;
776d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
777d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
778d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
779d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
780d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic int soinfo_unload(soinfo* si) {
781ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  if (si->ref_count == 1) {
782ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("unloading '%s'", si->name);
783d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallDestructors();
784d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
7850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
786d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom      if (d->d_tag == DT_NEEDED) {
787d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        const char* library_name = si->strtab + d->d_un.d_val;
7888147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes        TRACE("%s needs to unload %s", si->name, library_name);
7898147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes        soinfo_unload(find_loaded_library(library_name));
790d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
7911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
793d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    munmap(reinterpret_cast<void*>(si->base), si->size);
794d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    notify_gdb_of_unload(si);
795d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    soinfo_free(si);
796ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 0;
797d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  } else {
798ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count--;
799c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
800d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
801d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return 0;
8021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
804a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughesvoid do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
805a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes  snprintf(buffer, buffer_size, "%s:%s", gDefaultLdPaths[0], gDefaultLdPaths[1]);
806a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes}
807a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes
808cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesvoid do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
809cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (!get_AT_SECURE()) {
810cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    parse_LD_LIBRARY_PATH(ld_library_path);
811cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
812cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
813cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
814012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
815e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes  if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
816e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    DL_ERR("invalid flags to dlopen: %x", flags);
817e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    return NULL;
818e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes  }
819012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)  if (extinfo != NULL && ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0)) {
820012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)    DL_ERR("invalid extended flags to android_dlopen_ext: %x", extinfo->flags);
821012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)    return NULL;
822012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)  }
823d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
82412bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)  soinfo* si = find_library(name, extinfo);
825d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
826d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
827d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
828d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ);
829d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
830d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
8311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
832d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesint do_dlclose(soinfo* si) {
833d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
834d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  int result = soinfo_unload(si);
835d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ);
836d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return result;
8371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8394eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
8409918665a45095ad135576f005c0e5307feb366a1Chris Dearmanstatic int soinfo_relocate(soinfo* si, ElfW(Rela)* rela, unsigned count, soinfo* needed[]) {
8410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* s;
842c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  soinfo* lsi;
843c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
844c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  for (size_t idx = 0; idx < count; ++idx, ++rela) {
8450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    unsigned type = ELFW(R_TYPE)(rela->r_info);
8460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    unsigned sym = ELFW(R_SYM)(rela->r_info);
8470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + si->load_bias);
8480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Addr) sym_addr = 0;
849faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    const char* sym_name = NULL;
850c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
851c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
852c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (type == 0) { // R_*_NONE
853c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      continue;
854c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
855c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (sym != 0) {
856c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes      sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
857c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      s = soinfo_do_lookup(si, sym_name, &lsi, needed);
858c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      if (s == NULL) {
859c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        // We only allow an undefined symbol if this is a weak reference...
860c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes        s = &si->symtab[sym];
861c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
862c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
863c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          return -1;
864c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
865c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
866c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        /* IHI0044C AAELF 4.5.1.1:
867c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
868c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           Libraries are not searched to resolve weak references.
869c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           It is not an error for a weak reference to remain unsatisfied.
870c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
871c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           During linking, the value of an undefined weak reference is:
872c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - Zero if the relocation type is absolute
873c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - The address of the place if the relocation is pc-relative
874c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - The address of nominal base address if the relocation
875c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes             type is base-relative.
876c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         */
877c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
878c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (type) {
879e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__)
880e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_JUMP_SLOT:
881e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_GLOB_DAT:
882e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS64:
883e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS32:
884e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS16:
885e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_RELATIVE:
886e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland          /*
887e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * The sym_addr was initialized to be zero above, or the relocation
888e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * code below does not care about value of sym_addr.
889e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * No need to do anything.
890e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           */
891e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland          break;
892e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__)
893c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_JUMP_SLOT:
894c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_GLOB_DAT:
895c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_32:
896c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_RELATIVE:
897c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          // No need to do anything.
898c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          break;
899c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_PC32:
900c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          sym_addr = reloc;
901c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          break;
9024eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
903c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        default:
904faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes          DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx);
905c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          return -1;
906c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
907c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      } else {
908c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        // We got a definition.
9090266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
910c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      }
911c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocSymbol);
912c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    } else {
913c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      s = NULL;
914c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
915c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
916c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    switch (type) {
917e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__)
918e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_JUMP_SLOT:
919e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
920e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9210266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n",
9220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
924e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
925e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_GLOB_DAT:
926e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
927e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n",
9290266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9300266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
931e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
932e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS64:
933e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
934e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9350266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
9360266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9370266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
938e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
939e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS32:
940e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
941e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9420266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
9430266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9440266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
9450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
9460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
947e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
9480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
9490266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
9500266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT32_MIN),
9510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT32_MAX));
952e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
953e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
954e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
955e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS16:
956e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
957e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9580266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
9590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
9610266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
9620266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
963e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
9640266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
9650266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
9660266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT16_MIN),
9670266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT16_MAX));
968e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
969e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
970e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
971e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL64:
972e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
973e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9740266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
9750266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
9760266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
977e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
978e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL32:
979e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
980e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9810266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
9820266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
9830266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
9840266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
9850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
986e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
9870266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
9880266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
9890266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT32_MIN),
9900266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT32_MAX));
991e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
992e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
993e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
994e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL16:
995e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
996e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9970266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
9980266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
9990266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
10000266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
10010266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
1002e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
10030266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
10040266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
10050266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT16_MIN),
10060266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT16_MAX));
1007e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1008e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
1009e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1010e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1011e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_RELATIVE:
1012e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
1013e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
1014e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        if (sym) {
1015e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            DL_ERR("odd RELATIVE form...");
1016e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1017e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
10180266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n",
10190266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (si->base + rela->r_addend));
10200266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (si->base + rela->r_addend);
1021e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1022e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1023e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_COPY:
1024e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        if ((si->flags & FLAG_EXE) == 0) {
1025e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            /*
1026e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland              * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1027e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland              *
1028e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland              * Section 4.7.1.10 "Dynamic relocations"
1029e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland              * R_AARCH64_COPY may only appear in executable objects where e_type is
1030e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland              * set to ET_EXEC.
1031e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland              *
1032e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland              * FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1033e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland              * We should explicitly disallow ET_DYN executables from having
1034e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland              * R_AARCH64_COPY relocations.
1035e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland              */
1036e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            DL_ERR("%s R_AARCH64_COPY relocations only supported for ET_EXEC", si->name);
1037e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1038e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
1039e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocCopy);
1040e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
10410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO COPY %16llx <- %lld @ %16llx %s\n",
10420266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc,
10430266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   s->st_size,
10440266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (sym_addr + rela->r_addend),
10450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   sym_name);
1046e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        if (reloc == (sym_addr + rela->r_addend)) {
10470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ElfW(Sym)* src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
1048e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1049e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            if (src == NULL) {
1050e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland                DL_ERR("%s R_AARCH64_COPY relocation source cannot be resolved", si->name);
1051e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland                return -1;
1052e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            }
1053e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            if (lsi->has_DT_SYMBOLIC) {
1054e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland                DL_ERR("%s invalid R_AARCH64_COPY relocation against DT_SYMBOLIC shared "
10550266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                       "library %s (built with -Bsymbolic?)", si->name, lsi->name);
1056e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland                return -1;
1057e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            }
1058e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            if (s->st_size < src->st_size) {
10590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                DL_ERR("%s R_AARCH64_COPY relocation size mismatch (%lld < %lld)",
10600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                       si->name, s->st_size, src->st_size);
1061e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland                return -1;
1062e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            }
1063faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            memcpy(reinterpret_cast<void*>(reloc),
1064faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                   reinterpret_cast<void*>(src->st_value + lsi->load_bias), src->st_size);
1065e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
1066e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            DL_ERR("%s R_AARCH64_COPY relocation target cannot be resolved", si->name);
1067e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1068e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
1069e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1070e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_TLS_TPREL64:
10710266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
10720266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset);
1073e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1074e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_TLS_DTPREL32:
10750266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
10760266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset);
1077e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1078e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__)
1079c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_JUMP_SLOT:
1080c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocAbsolute);
1081c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1082c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1083c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
10840266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1085c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1086c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_GLOB_DAT:
1087c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocAbsolute);
1088c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1089c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1090c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
10910266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1092c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1093c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_RELATIVE:
1094c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1095c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1096c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      if (sym) {
1097c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DL_ERR("odd RELATIVE form...");
1098c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        return -1;
1099c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      }
1100c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
1101c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(si->base));
11020266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = si->base + rela->r_addend;
1103c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1104c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_32:
1105c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1106c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1107c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1108c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr), sym_name);
11090266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1110c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1111c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin    case R_X86_64_64:
1112c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      count_relocation(kRelocRelative);
1113c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      MARK(rela->r_offset);
1114c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1115c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin                 static_cast<size_t>(sym_addr), sym_name);
11160266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1117c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      break;
1118c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_PC32:
1119c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1120c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1121c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1122c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1123c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
11240266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc;
1125c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
11264eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1127e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1128c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    default:
1129faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes      DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx);
1130c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      return -1;
1131c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1132c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  }
1133c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  return 0;
1134c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes}
11359918665a45095ad135576f005c0e5307feb366a1Chris Dearman
11369918665a45095ad135576f005c0e5307feb366a1Chris Dearman#else // REL, not RELA.
11379918665a45095ad135576f005c0e5307feb366a1Chris Dearman
11380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic int soinfo_relocate(soinfo* si, ElfW(Rel)* rel, unsigned count, soinfo* needed[]) {
11390266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s;
1140d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo* lsi;
11411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    for (size_t idx = 0; idx < count; ++idx, ++rel) {
11430266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        unsigned type = ELFW(R_TYPE)(rel->r_info);
11440266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead.
11450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        unsigned sym = ELFW(R_SYM)(rel->r_info);
11460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + si->load_bias);
11470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        ElfW(Addr) sym_addr = 0;
1148faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        const char* sym_name = NULL;
11491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1150c620059479c47a78d57086d73726c9adc2f337adElliott Hughes        DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
1151d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (type == 0) { // R_*_NONE
1152d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            continue;
1153d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1154d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (sym != 0) {
1155c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes            sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
11565ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1157d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (s == NULL) {
11584eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                // We only allow an undefined symbol if this is a weak reference...
1159c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes                s = &si->symtab[sym];
1160c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1161e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes                    DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
1162e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1163e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1164e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1165e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* IHI0044C AAELF 4.5.1.1:
1166e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1167e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   Libraries are not searched to resolve weak references.
1168e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   It is not an error for a weak reference to remain
1169e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   unsatisfied.
1170e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1171e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   During linking, the value of an undefined weak reference is:
1172e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - Zero if the relocation type is absolute
1173e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - The address of the place if the relocation is pc-relative
1174bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes                   - The address of nominal base address if the relocation
1175e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                     type is base-relative.
1176e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                  */
1177e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1178e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                switch (type) {
11794eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
1180e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_JUMP_SLOT:
1181e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_GLOB_DAT:
1182e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_ABS32:
1183e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_RELATIVE:    /* Don't care. */
11844eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // sym_addr was initialized to be zero above or relocation
11854eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // code below does not care about value of sym_addr.
11864eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // No need to do anything.
11874eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    break;
11884eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
1189d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                case R_386_JMP_SLOT:
1190e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_GLOB_DAT:
1191e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_32:
1192c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                case R_386_RELATIVE:    /* Don't care. */
11934eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // sym_addr was initialized to be zero above or relocation
11944eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // code below does not care about value of sym_addr.
11954eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // No need to do anything.
1196e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
1197e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_PC32:
1198e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    sym_addr = reloc;
1199e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
12004eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1201e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
12024eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
1203e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_COPY:
12044eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // Fall through. Can't really copy if weak symbol is not found at run-time.
12054eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1206e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                default:
1207faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                    DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
1208e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1209e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1210e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            } else {
12114eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                // We got a definition.
12120266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
1213d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1214bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocSymbol);
12151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        } else {
1216e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            s = NULL;
12171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
12181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1219c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (type) {
12204eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
12211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_JUMP_SLOT:
1222bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
12231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1224ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
12250266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
12261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
12271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_GLOB_DAT:
1228bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
12291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1230ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
12310266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
12321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
12331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_ABS32:
1234bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
12351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1236ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
12370266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
12381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
123934ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner        case R_ARM_REL32:
1240bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
124134ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            MARK(rel->r_offset);
1242ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
124334ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner                       reloc, sym_addr, rel->r_offset, sym_name);
12440266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
124534ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            break;
12461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_COPY:
1247d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            if ((si->flags & FLAG_EXE) == 0) {
1248d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                /*
1249d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1250d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 *
1251d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * Section 4.7.1.10 "Dynamic relocations"
1252d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * R_ARM_COPY may only appear in executable objects where e_type is
1253d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * set to ET_EXEC.
1254d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 *
1255d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1256d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * We should explicitly disallow ET_DYN executables from having
1257d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * R_ARM_COPY relocations.
1258d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 */
1259d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1260d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                return -1;
1261d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            }
1262bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocCopy);
12631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1264ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s", reloc, s->st_size, sym_addr, sym_name);
1265d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            if (reloc == sym_addr) {
12660266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                ElfW(Sym)* src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
12675ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
12685ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (src == NULL) {
12695ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
12705ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
12715ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
12725ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (lsi->has_DT_SYMBOLIC) {
12735ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
12745ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                           "library %s (built with -Bsymbolic?)", si->name, lsi->name);
12755ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
12765ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
12775ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (s->st_size < src->st_size) {
12785ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
12795ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                           si->name, s->st_size, src->st_size);
12805ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
12815ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
1282faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                memcpy(reinterpret_cast<void*>(reloc),
1283faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                       reinterpret_cast<void*>(src->st_value + lsi->load_bias), src->st_size);
12845ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            } else {
12855ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
1286d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                return -1;
1287d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            }
12881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
12894eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
12904eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_JMP_SLOT:
12914eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
12924eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
12934eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
12940266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
12954eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
12964eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_GLOB_DAT:
12974eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
12984eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
12994eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
13000266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
13014eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
13024eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_32:
13034eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
13044eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
13054eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
13060266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
13074eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
13084eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_PC32:
13094eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
13104eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
13114eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
13124eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                       reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
13130266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
13144eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
13154eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__mips__)
13164eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_MIPS_REL32:
13179918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__LP64__)
13189918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // MIPS Elf64_Rel entries contain compound relocations
13199918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
13209918665a45095ad135576f005c0e5307feb366a1Chris Dearman            if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
13219918665a45095ad135576f005c0e5307feb366a1Chris Dearman                ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
1322faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
13239918665a45095ad135576f005c0e5307feb366a1Chris Dearman                       type, (unsigned)ELF64_R_TYPE2(rel->r_info),
1324faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                       (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx);
13259918665a45095ad135576f005c0e5307feb366a1Chris Dearman                return -1;
13269918665a45095ad135576f005c0e5307feb366a1Chris Dearman            }
13279918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif
13284eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
13294eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
13309918665a45095ad135576f005c0e5307feb366a1Chris Dearman            TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
13319918665a45095ad135576f005c0e5307feb366a1Chris Dearman                       static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
13324eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            if (s) {
13330266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
13344eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            } else {
13350266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base;
13364eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            }
13374eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
13384eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
13394eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes
13404eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
13414eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_ARM_RELATIVE:
13424eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
13434eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_RELATIVE:
13444eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
13454eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
13464eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
13474eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            if (sym) {
13484eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                DL_ERR("odd RELATIVE form...");
13494eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                return -1;
13504eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            }
13514eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
13524eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                       reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base));
13530266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base;
13544eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
13551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        default:
1357faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
13581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return -1;
13591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
13601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
13621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
1363c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
13641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13654eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
136687c358524e479235aa6241736d2ce325f89daafcBrian Carlstromstatic bool mips_relocate_got(soinfo* si, soinfo* needed[]) {
13679918665a45095ad135576f005c0e5307feb366a1Chris Dearman    ElfW(Addr)** got = si->plt_got;
136887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    if (got == NULL) {
136987c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom        return true;
137087c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    }
137187c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned local_gotno = si->mips_local_gotno;
137287c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned gotsym = si->mips_gotsym;
137387c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned symtabno = si->mips_symtabno;
13740266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* symtab = si->symtab;
1375d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
13769918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // got[0] is the address of the lazy resolver function.
13779918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // got[1] may be used for a GNU extension.
13789918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
13799918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // FIXME: maybe this should be in a separate routine?
1380d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if ((si->flags & FLAG_LINKER) == 0) {
138187c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom        size_t g = 0;
13829918665a45095ad135576f005c0e5307feb366a1Chris Dearman        got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
13839918665a45095ad135576f005c0e5307feb366a1Chris Dearman        if (reinterpret_cast<intptr_t>(got[g]) < 0) {
13849918665a45095ad135576f005c0e5307feb366a1Chris Dearman            got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
1385d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
13869918665a45095ad135576f005c0e5307feb366a1Chris Dearman        // Relocate the local GOT entries.
1387d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        for (; g < local_gotno; g++) {
13889918665a45095ad135576f005c0e5307feb366a1Chris Dearman            got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + si->load_bias);
1389d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1390d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1391d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
13929918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // Now for the global GOT entries...
13930266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* sym = symtab + gotsym;
1394d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got + local_gotno;
139587c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
13969918665a45095ad135576f005c0e5307feb366a1Chris Dearman        // This is an undefined reference... try to locate it.
13979918665a45095ad135576f005c0e5307feb366a1Chris Dearman        const char* sym_name = si->strtab + sym->st_name;
1398d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        soinfo* lsi;
13999918665a45095ad135576f005c0e5307feb366a1Chris Dearman        ElfW(Sym)* s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1400d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (s == NULL) {
14019918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // We only allow an undefined symbol if this is a weak reference.
1402d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            s = &symtab[g];
1403c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
14044688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("cannot locate \"%s\"...", sym_name);
140587c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom                return false;
1406d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1407d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *got = 0;
14089918665a45095ad135576f005c0e5307feb366a1Chris Dearman        } else {
14099918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // FIXME: is this sufficient?
14109918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // For reference see NetBSD link loader
14119918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // 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
14129918665a45095ad135576f005c0e5307feb366a1Chris Dearman            *got = reinterpret_cast<ElfW(Addr)*>(lsi->load_bias + s->st_value);
1413d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1414d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
141587c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    return true;
1416d7daacb46372132ae3f0121647074936c304b572Raghu Gandham}
1417d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1418d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1419812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallArray(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
1420ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  if (functions == NULL) {
1421d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1422d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
14238215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1424c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
14258215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1426ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int begin = reverse ? (count - 1) : 0;
1427ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int end = reverse ? -1 : count;
1428ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int step = reverse ? -1 : 1;
14298215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1430ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  for (int i = begin; i != end; i += step) {
1431ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1432ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    CallFunction("function", functions[i]);
1433d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1434d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1435ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s for '%s' ]", array_name, name);
14361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1438812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallFunction(const char* function_name __unused, linker_function_t function) {
1439db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
1440d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1441d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1442d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1443ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
1444d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  function();
1445ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
1446db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes
1447db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1448db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // are still writable. This happens with our debug malloc (see http://b/7941716).
1449db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
14509181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov}
14519181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
1452d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallPreInitConstructors() {
14538147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
14548147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // but ignored in a shared library.
1455d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1456d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
1457e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1458d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallConstructors() {
1459d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (constructors_called) {
1460d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1461d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1462e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1463d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // We set constructors_called before actually calling the constructors, otherwise it doesn't
1464d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // protect against recursive constructor calls. One simple example of constructor recursion
1465d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1466d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 1. The program depends on libc, so libc's constructor is called here.
1467d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1468d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 3. dlopen() calls the constructors on the newly created
1469d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    soinfo for libc_malloc_debug_leak.so.
1470d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 4. The debug .so depends on libc, so CallConstructors is
1471d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    called again with the libc soinfo. If it doesn't trigger the early-
1472d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    out above, the libc constructor will be called again (recursively!).
1473d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  constructors_called = true;
1474d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
14758147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) {
14768147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes    // The GNU dynamic linker silently ignores these, but we warn the developer.
1477c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
14788147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes          name, preinit_array_count);
1479d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
14801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1481d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom  if (dynamic != NULL) {
14820266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
1483d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom      if (d->d_tag == DT_NEEDED) {
1484d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        const char* library_name = strtab + d->d_un.d_val;
14858147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes        TRACE("\"%s\": calling constructors in DT_NEEDED \"%s\"", name, library_name);
14868147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes        find_loaded_library(library_name)->CallConstructors();
1487d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
14881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1489d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
14901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14918147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  TRACE("\"%s\": calling constructors", name);
14928147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
14938147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_INIT should be called before DT_INIT_ARRAY if both are present.
1494d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_INIT", init_func);
1495d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
1496e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov}
14978215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1498d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallDestructors() {
14998147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  TRACE("\"%s\": calling destructors", name);
15008147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
15018147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_FINI_ARRAY must be parsed in reverse order.
1502d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
15038147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
15048147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_FINI should be called after DT_FINI_ARRAY if both are present.
1505d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_FINI", fini_func);
15061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
15071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with
15091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   /dev/null. */
15105419b9474753d25dff947c7740532f86d130c0beElliott Hughesstatic int nullify_closed_stdio() {
15111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int dev_null, i, status;
15121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int return_value = 0;
15131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
151416084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
15151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null < 0) {
15164688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("cannot open /dev/null: %s", strerror(errno));
15171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return -1;
15181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1519ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
15201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If any of the stdio file descriptors is valid and not associated
15221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       with /dev/null, dup /dev/null to it.  */
15231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (i = 0; i < 3; i++) {
15241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* If it is /dev/null already, we are done. */
15254688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (i == dev_null) {
15261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
15274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
15281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1529ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Nullifying stdio file descriptor %d]", i);
15304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
15311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        /* If file is opened, we are good. */
15334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status != -1) {
15344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            continue;
15354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
15361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* The only error we allow is that the file descriptor does not
15381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           exist, in which case we dup /dev/null to it. */
15391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (errno != EBADF) {
15404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("fcntl failed: %s", strerror(errno));
15411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
15421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
15431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
15441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* Try dupping /dev/null to this stdio file descriptor and
15461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           repeat if there is a signal.  Note that any errors in closing
15471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           the stdio descriptor are lost.  */
15484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
15491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (status < 0) {
15504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("dup2 failed: %s", strerror(errno));
15511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
15521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
15531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
15541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
15551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If /dev/null is not one of the stdio file descriptors, close it. */
15571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null > 2) {
1558ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
15594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(close(dev_null));
15604688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status == -1) {
15614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("close failed: %s", strerror(errno));
15621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
15631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
15641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
15651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return return_value;
15671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
15681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1569124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic bool soinfo_link_image(soinfo* si) {
1570f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee    /* "base" might wrap around UINT32_MAX. */
15710266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Addr) base = si->load_bias;
15720266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    const ElfW(Phdr)* phdr = si->phdr;
15731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int phnum = si->phnum;
1574d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
15751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1576b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can't debug anything until the linker is relocated */
1577b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (!relocating_linker) {
1578ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        INFO("[ linking %s ]", si->name);
1579c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags);
1580b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
15811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
158263f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    /* Extract dynamic section */
1583124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    size_t dynamic_count;
15840266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Word) dynamic_flags;
158512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
1586cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman                                   &dynamic_count, &dynamic_flags);
158763f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    if (si->dynamic == NULL) {
1588b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1589124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
1590b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
1591124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
159263f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    } else {
1593b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1594ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("dynamic = %p", si->dynamic);
1595b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
159663f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    }
159763f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
15984eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
159963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    (void) phdr_table_get_arm_exidx(phdr, phnum, base,
160063f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner                                    &si->ARM_exidx, &si->ARM_exidx_count);
160163f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif
160263f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
16038147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes    // Extract useful information from dynamic section.
1604d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    uint32_t needed_count = 0;
16050266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1606c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1607c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes              d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1608c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (d->d_tag) {
16091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_HASH:
1610faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->nbucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[0];
1611faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->nchain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[1];
1612faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->bucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8);
1613faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->chain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8 + si->nbucket * 4);
16141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_STRTAB:
1616faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->strtab = reinterpret_cast<const char*>(base + d->d_un.d_ptr);
16171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_SYMTAB:
1619faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->symtab = reinterpret_cast<ElfW(Sym)*>(base + d->d_un.d_ptr);
16201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16214eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if !defined(__LP64__)
16221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTREL:
1623d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val != DT_REL) {
1624124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1625124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
16261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
16271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1628c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
16291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_JMPREL:
16304eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1631faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->plt_rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr);
1632c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1633faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->plt_rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr);
1634c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
16351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTRELSZ:
16374eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
16380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->plt_rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
1639c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
16400266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->plt_rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
1641c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
16421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16439918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__mips__)
16441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTGOT:
16459918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // Used by mips and mips64.
16469918665a45095ad135576f005c0e5307feb366a1Chris Dearman            si->plt_got = reinterpret_cast<ElfW(Addr)**>(base + d->d_un.d_ptr);
16471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1648c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
16491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_DEBUG:
1650bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_DEBUG entry to the address of _r_debug for GDB
1651cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman            // if the dynamic table is writable
16529918665a45095ad135576f005c0e5307feb366a1Chris Dearman// FIXME: not working currently for N64
16539918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The flags for the LOAD and DYNAMIC program headers do not agree.
16549918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The LOAD section containng the dynamic table has been mapped as
16559918665a45095ad135576f005c0e5307feb366a1Chris Dearman// read-only, but the DYNAMIC header claims it is writable.
16569918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if !(defined(__mips__) && defined(__LP64__))
165799c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            if ((dynamic_flags & PF_W) != 0) {
1658c620059479c47a78d57086d73726c9adc2f337adElliott Hughes                d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
165999c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            }
16601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16619918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif
16624eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1663c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         case DT_RELA:
1664faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr);
1665c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1666c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         case DT_RELASZ:
16670266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
1668c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1669c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_REL:
1670c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            DL_ERR("unsupported DT_REL in \"%s\"", si->name);
1671c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1672c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_RELSZ:
1673c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name);
1674c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1675c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1676c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_REL:
1677faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr);
1678c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1679c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_RELSZ:
16800266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
1681c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1682ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI         case DT_RELA:
1683124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1684124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1685c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
16861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT:
1687ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
16888147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func);
16891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI:
1691ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
16928147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func);
16931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAY:
1695ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
16968147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array);
16971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAYSZ:
16990266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
17001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAY:
1702ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
17038147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array);
17041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAYSZ:
17060266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
17071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAY:
1709ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
17108147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array);
17111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAYSZ:
17130266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
17141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_TEXTREL:
1716e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__)
1717e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes            DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1718e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes            return false;
1719e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else
17205135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            si->has_text_relocations = true;
17211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1722e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
17235ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_SYMBOLIC:
17245ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            si->has_DT_SYMBOLIC = true;
17255ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
1726d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        case DT_NEEDED:
1727d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            ++needed_count;
1728d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            break;
17295ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_FLAGS:
1730d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_TEXTREL) {
1731e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__)
1732e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes                DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1733e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes                return false;
1734e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else
17355ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_text_relocations = true;
1736e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
17375ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
1738d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_SYMBOLIC) {
17395ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_DT_SYMBOLIC = true;
17405ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
17415ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
17424eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
1743d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_STRSZ:
1744d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_SYMENT:
1745d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_RELENT:
1746d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             break;
1747d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_MAP:
1748bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
1749d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            {
1750006f9ad8910c945cd50e54f6c34d8c27b11c1193Benjamin Adolphi              r_debug** dp = reinterpret_cast<r_debug**>(base + d->d_un.d_ptr);
1751d7daacb46372132ae3f0121647074936c304b572Raghu Gandham              *dp = &_r_debug;
1752d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1753d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1754d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_VERSION:
1755d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_FLAGS:
1756d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_BASE_ADDRESS:
1757d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_UNREFEXTNO:
1758d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1759d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1760d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_SYMTABNO:
1761d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_symtabno = d->d_un.d_val;
1762d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1763d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1764d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_LOCAL_GOTNO:
1765d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_local_gotno = d->d_un.d_val;
1766d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1767d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1768d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_GOTSYM:
1769d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_gotsym = d->d_un.d_val;
1770d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
17714eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1772d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1773d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        default:
17744eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            DEBUG("Unused DT entry: type %p arg %p",
17754eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                  reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1776d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
17771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
17781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
17791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1780c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
1781c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          reinterpret_cast<void*>(si->base), si->strtab, si->symtab);
17821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1783124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    // Sanity checks.
1784d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (relocating_linker && needed_count != 0) {
1785d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1786d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        return false;
1787d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
1788124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->nbucket == 0) {
1789124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1790124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1791124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1792124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->strtab == 0) {
1793124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1794124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1795124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1796124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->symtab == 0) {
1797124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1798124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
17991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18017e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes    // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
1802d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_EXE) {
1803d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        memset(gLdPreloads, 0, sizeof(gLdPreloads));
18047e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes        size_t preload_count = 0;
1805d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
180612bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)            soinfo* lsi = find_library(gLdPreloadNames[i], NULL);
18077e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes            if (lsi != NULL) {
18087e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                gLdPreloads[preload_count++] = lsi;
18097e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes            } else {
18107e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                // As with glibc, failure to load an LD_PRELOAD library is just a warning.
18117e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
18127e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                        gLdPreloadNames[i], si->name, linker_get_error_buffer());
18134fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            }
18144fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        }
18154fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
18164fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1817faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    soinfo** needed = reinterpret_cast<soinfo**>(alloca((1 + needed_count) * sizeof(soinfo*)));
1818d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo** pneeded = needed;
181912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
18200266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1821d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (d->d_tag == DT_NEEDED) {
1822d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            const char* library_name = si->strtab + d->d_un.d_val;
1823ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("%s needs %s", si->name, library_name);
182412bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)            soinfo* lsi = find_library(library_name, NULL);
1825d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            if (lsi == NULL) {
1826650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes                strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
18274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1828d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                       library_name, si->name, tmp_err_buf);
1829124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
18301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
183112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            *pneeded++ = lsi;
18321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
18331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
183412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    *pneeded = NULL;
18351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1836e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__)
18375135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
1838e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // Make segments writable to allow text relocations to work properly. We will later call
1839e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // phdr_table_protect_segments() after all of them are applied and all constructors are run.
18403d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich        DL_WARN("%s has text relocations. This is wasting memory and prevents "
18413d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich                "security hardening. Please fix.", si->name);
18425135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
18435135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't unprotect loadable segments for \"%s\": %s",
18445135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1845124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
18465135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
18475135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    }
1848e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
18495135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich
18504eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1851c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (si->plt_rela != NULL) {
1852faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s plt ]\n", si->name);
18539918665a45095ad135576f005c0e5307feb366a1Chris Dearman        if (soinfo_relocate(si, si->plt_rela, si->plt_rela_count, needed)) {
1854c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1855c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
1856c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1857c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (si->rela != NULL) {
1858faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s ]\n", si->name);
18599918665a45095ad135576f005c0e5307feb366a1Chris Dearman        if (soinfo_relocate(si, si->rela, si->rela_count, needed)) {
1860c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1861c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
1862c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1863c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1864d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si->plt_rel != NULL) {
1865faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s plt ]", si->name);
1866d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1867124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1868124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
18691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1870d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si->rel != NULL) {
1871faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s ]", si->name);
1872d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1873124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1874124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
18751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1876c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
18771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18784eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
187987c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    if (!mips_relocate_got(si, needed)) {
1880124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1881d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1882d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1883d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
18841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_LINKED;
1885ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    DEBUG("[ finished linking %s ]", si->name);
18861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1887e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__)
18885135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
1889e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // All relocations are done, we can protect our segments back to read-only.
18905135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
18915135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't protect segments for \"%s\": %s",
18925135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1893124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
18945135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
18951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1896e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
18971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1898b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can also turn on GNU RELRO protection */
1899b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
19004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
19014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               si->name, strerror(errno));
1902124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
19039ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich    }
19049ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
19051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    notify_gdb_of_load(si);
1906124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    return true;
19071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
19081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1909468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1910c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * This function add vdso to internal dso list.
1911c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * It helps to stack unwinding through signal handlers.
1912c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * Also, it makes bionic more like glibc.
1913c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov */
1914812fd4263a005b88f3b4222baa910114f938d594Kito Chengstatic void add_vdso(KernelArgumentBlock& args __unused) {
19154eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(AT_SYSINFO_EHDR)
19160266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
19170266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  if (ehdr_vdso == NULL) {
19180266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    return;
19190266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  }
1920c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
19210266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  soinfo* si = soinfo_alloc("[vdso]");
1922ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov
19230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
19240266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->phnum = ehdr_vdso->e_phnum;
19250266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
19260266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->size = phdr_table_get_load_size(si->phdr, si->phnum);
19270266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->flags = 0;
19280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
1929ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov
19300266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  soinfo_link_image(si);
1931c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov#endif
1932c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov}
1933c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
1934c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov/*
1935468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and
1936468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs
1937468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point.
1938468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
19390266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
194042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    /* NOTE: we store the args pointer on a special location
1941ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       of the temporary TLS area in order to pass it to
1942ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       the C Library's runtime initializer.
1943ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *
1944ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       The initializer must clear the slot and reset the TLS
1945ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       to point to a different location to ensure that no other
1946ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       shared library constructor can access it.
1947ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     */
1948d3920b3a996b358e48232f417aa0a1e44a60f155Elliott Hughes  __libc_init_tls(args);
19491a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov
19501a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING
19511a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    struct timeval t0, t1;
19521a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    gettimeofday(&t0, 0);
19531a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif
19541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
195518a206c81d9743481e364384affd43306911283dElliott Hughes    // Initialize environment functions, and get to the ELF aux vectors table.
195642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    linker_env_init(args);
1957be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
19588d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // If this is a setuid/setgid program, close the security hole described in
19598d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
19608d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    if (get_AT_SECURE()) {
19618d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich        nullify_closed_stdio();
19628d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    }
19638d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich
1964d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    debuggerd_init();
19651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
196618a206c81d9743481e364384affd43306911283dElliott Hughes    // Get a few environment variables.
196761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    const char* LD_DEBUG = linker_env_get("LD_DEBUG");
196861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    if (LD_DEBUG != NULL) {
1969650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes      gLdDebugVerbosity = atoi(LD_DEBUG);
197018a206c81d9743481e364384affd43306911283dElliott Hughes    }
1971be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
197218a206c81d9743481e364384affd43306911283dElliott Hughes    // Normally, these are cleaned by linker_env_init, but the test
197318a206c81d9743481e364384affd43306911283dElliott Hughes    // doesn't cost us anything.
197418a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpath_env = NULL;
197518a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpreload_env = NULL;
197618a206c81d9743481e364384affd43306911283dElliott Hughes    if (!get_AT_SECURE()) {
197718a206c81d9743481e364384affd43306911283dElliott Hughes      ldpath_env = linker_env_get("LD_LIBRARY_PATH");
197818a206c81d9743481e364384affd43306911283dElliott Hughes      ldpreload_env = linker_env_get("LD_PRELOAD");
19791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
19801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1981ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    INFO("[ android linker & debugger ]");
19821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
198342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    soinfo* si = soinfo_alloc(args.argv[0]);
198418a206c81d9743481e364384affd43306911283dElliott Hughes    if (si == NULL) {
198518a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
19861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
19871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1988d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    /* bootstrap the link map, the main exe always needs to be first */
19891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_EXE;
19903a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(si->link_map_head);
19911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = 0;
199342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    map->l_name = args.argv[0];
19941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_prev = NULL;
19951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_next = NULL;
19961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_map = map;
19981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
19991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2000d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    /* gdb expects the linker to be in the debug shared object list.
2001d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * Without this, gdb has trouble locating the linker's ".text"
2002d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * and ".plt" sections. Gdb could also potentially use this to
2003d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2004d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * Don't use soinfo_alloc(), because the linker shouldn't
2005d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * be on the soinfo list.
200606f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     */
2007d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    {
2008d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        static soinfo linker_soinfo;
20094eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__LP64__)
20101a57f9f75c5752d990a2749b000ec4ceace94b54Pavel Chupin        strlcpy(linker_soinfo.name, "/system/bin/linker64", sizeof(linker_soinfo.name));
20111a57f9f75c5752d990a2749b000ec4ceace94b54Pavel Chupin#else
2012d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
20131a57f9f75c5752d990a2749b000ec4ceace94b54Pavel Chupin#endif
2014d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        linker_soinfo.flags = 0;
2015d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        linker_soinfo.base = linker_base;
2016d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom
2017d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        /*
2018d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         * Set the dynamic field in the link map otherwise gdb will complain with
2019d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         * the following:
2020d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         *   warning: .dynamic section for "/system/bin/linker" is not at the
2021d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         *   expected address (wrong library or version mismatch?)
2022d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         */
20230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2024faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2025d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
2026d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                                       &linker_soinfo.dynamic, NULL, NULL);
2027d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        insert_soinfo_into_debug_map(&linker_soinfo);
2028d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
20291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
203042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // Extract information passed from the kernel.
20310266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
203242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->phnum = args.getauxval(AT_PHNUM);
203342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->entry = args.getauxval(AT_ENTRY);
20341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
20358180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    /* Compute the value of si->base. We can't rely on the fact that
20368180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * the first entry is the PHDR because this will not be true
20378180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * for certain executables (e.g. some in the NDK unit test suite)
20388180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     */
20398180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    si->base = 0;
2040b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2041bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    si->load_bias = 0;
2042ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    for (size_t i = 0; i < si->phnum; ++i) {
204342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      if (si->phdr[i].p_type == PT_PHDR) {
20440266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
20450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
204642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes        break;
204742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      }
20488180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    }
2049d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    si->dynamic = NULL;
2050ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 1;
20511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
20524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
20534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_LIBRARY_PATH(ldpath_env);
20544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_PRELOAD(ldpreload_env);
20554fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
20565ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    somain = si;
20575ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
2058124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (!soinfo_link_image(si)) {
2059650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes        __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
206018a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
20611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
20621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2063c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov    add_vdso(args);
2064c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
2065d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallPreInitConstructors();
20669181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
2067d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
2068d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        gLdPreloads[i]->CallConstructors();
2069326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng    }
2070326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng
2071d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    /* After the link_image, the si->load_bias is initialized.
2072d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2073d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * We need to update this value for so exe here. So Unwind_Backtrace
2074d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * for some arch like x86 could work correctly within so exe.
20759c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     */
2076c5db969aa4a24a76fc0964f2ff89533e832b47a6Chao-Ying Fu    map->l_addr = si->load_bias;
2077d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
2078e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
20791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING
2080faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    gettimeofday(&t1, NULL);
2081ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
20821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2083faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes               (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
20841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
20851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
2086ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2087bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocAbsolute],
2088bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocRelative],
2089bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocCopy],
2090bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocSymbol]);
20911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
20921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
20931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
20941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned n;
20951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned i;
20961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned count = 0;
2097d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        for (n = 0; n < 4096; n++) {
2098d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (bitmask[n]) {
20991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                unsigned x = bitmask[n];
2100e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__LP64__)
2101e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland                for (i = 0; i < 32; i++) {
2102e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#else
2103d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                for (i = 0; i < 8; i++) {
2104e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#endif
2105d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    if (x & 1) {
2106d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                        count++;
2107d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    }
21081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    x >>= 1;
21091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                }
21101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
21111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
2112ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
21131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
21141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
21151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
21161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES
21171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    fflush(stdout);
21181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
21191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2120c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
21211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si->entry;
21221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2123468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2124bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only
2125bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library
2126bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself.
2127bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *
2128bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input:
2129bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    elf    -> address of ELF header, assumed to be at the start of the file.
2130bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return:
2131bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    load bias, i.e. add the value of any p_vaddr in the file to get
2132bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    the corresponding address in memory.
2133bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */
21340266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
21350266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) offset = elf->e_phoff;
2136faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes  const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
21370266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
2138fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
21390266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
2140fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (phdr->p_type == PT_LOAD) {
21410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
2142bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    }
2143fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
2144fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return 0;
2145bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner}
2146bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
2147468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
2148468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This
2149468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and
2150468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation().
2151468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich *
2152468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own
2153468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern
2154468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault.
2155468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
21560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesextern "C" ElfW(Addr) __linker_init(void* raw_args) {
215742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  KernelArgumentBlock args(raw_args);
215842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
21590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
21600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
2161faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes  ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
216242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
216342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  soinfo linker_so;
216442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  memset(&linker_so, 0, sizeof(soinfo));
216542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
2166b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes  strcpy(linker_so.name, "[dynamic linker]");
216742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.base = linker_addr;
216842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
216942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
2170d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom  linker_so.dynamic = NULL;
217142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phdr = phdr;
217242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phnum = elf_hdr->e_phnum;
217342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.flags |= FLAG_LINKER;
217442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
217542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  if (!soinfo_link_image(&linker_so)) {
217642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // It would be nice to print an error message, but if the linker
217742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // can't link itself, there's no guarantee that we'll be able to
2178b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    // call write() (because it involves a GOT reference). We may as
2179b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    // well try though...
2180b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    const char* msg = "CANNOT LINK EXECUTABLE: ";
2181b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, msg, strlen(msg));
2182b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2183b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, "\n", 1);
2184b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    _exit(EXIT_FAILURE);
218542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  }
2186468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
218742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // We have successfully fixed our own relocations. It's safe to run
218842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // the main part of the linker now.
21890d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes  args.abort_message_ptr = &gAbortMessage;
21900266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
21915419b9474753d25dff947c7740532f86d130c0beElliott Hughes
219242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  set_soinfo_pool_protection(PROT_READ);
2193d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
219442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // Return the address that the calling assembly stub should jump to.
219542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  return start_address;
2196468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
2197