linker.cpp revision 1728b2396591853345507a063ed6075dfd251706
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"
51d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov#include "linker_allocator.h"
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Do NOT use malloc() and friends or pthread_*() code here.
561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Don't use printf() either; it's caused mysterious memory
571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * corruption in the past.
581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The linker runs before we bring up libc and it's easiest
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to make sure it does not depend on any complex libc features
601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * open issues / todo:
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
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
68183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo);
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
73d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov// objects as will fit.
741728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic LinkerAllocator<soinfo> g_soinfo_allocator;
75ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
76d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* solist = &libdl_info;
77d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* sonext = &libdl_info;
78d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* somain; /* main process, always the one after libdl_info */
791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
801728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic const char* const kDefaultLdPaths[] = {
814eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__LP64__)
82011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes  "/vendor/lib64",
83011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes  "/system/lib64",
84011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes#else
85124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  "/vendor/lib",
86124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  "/system/lib",
87011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes#endif
88124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  NULL
89124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes};
90124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes
91a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPATH_BUFSIZE (LDPATH_MAX*64)
92a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPATH_MAX 8
93a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes
94a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
95a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPRELOAD_MAX 8
96a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes
971728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic char g_ld_library_paths_buffer[LDPATH_BUFSIZE];
981728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic const char* g_ld_library_paths[LDPATH_MAX + 1];
99bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
1001728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic char g_ld_preloads_buffer[LDPRELOAD_BUFSIZE];
1011728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic const char* g_ld_preload_names[LDPRELOAD_MAX + 1];
1024fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1031728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic soinfo* g_ld_preloads[LDPRELOAD_MAX + 1];
1044fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1051728b2396591853345507a063ed6075dfd251706Elliott Hughes__LIBC_HIDDEN__ int g_ld_debug_verbosity;
1061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1071728b2396591853345507a063ed6075dfd251706Elliott Hughes__LIBC_HIDDEN__ abort_msg_t* g_abort_message = NULL; // For debuggerd.
1080d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes
109bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesenum RelocationKind {
110bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocAbsolute = 0,
111bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocRelative,
112bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocCopy,
113bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocSymbol,
114bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocMax
115bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
116be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
1171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
118bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstruct linker_stats_t {
119bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    int count[kRelocMax];
120bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
121bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
122bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic linker_stats_t linker_stats;
123bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
124bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind kind) {
125bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    ++linker_stats.count[kind];
126bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
127bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
128bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind) {
129bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
1301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
133bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic unsigned bitmask[4096];
134e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__LP64__)
135e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#define MARK(offset) \
136e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    do { \
137e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        if ((((offset) >> 12) >> 5) < 4096) \
138e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            bitmask[((offset) >> 12) >> 5] |= (1 << (((offset) >> 12) & 31)); \
139faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    } while (0)
140e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#else
141bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(offset) \
142bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    do { \
143bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
144faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    } while (0)
145e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#endif
146bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
147bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(x) do {} while (0)
1481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// You shouldn't try to call memory-allocating functions in the dynamic linker.
1514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Guard against the most obvious ones.
1528f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#define DISALLOW_ALLOCATION(return_type, name, ...) \
1538f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    return_type name __VA_ARGS__ \
1548f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    { \
1554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
1568f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        __libc_format_log(ANDROID_LOG_FATAL, "linker", "%s", msg); \
1578f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        write(2, msg, strlen(msg)); \
1588f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        abort(); \
1594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
160812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, malloc, (size_t u __unused));
161812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void, free, (void* u __unused));
162812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
163812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
1642e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1650353195f344666256dba474a15c9ba22cf0cccc9Dima Zavinstatic char tmp_err_buf[768];
1662e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavinstatic char __linker_dl_err_buf[768];
1672e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
168650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hugheschar* linker_get_error_buffer() {
1695419b9474753d25dff947c7740532f86d130c0beElliott Hughes  return &__linker_dl_err_buf[0];
1702e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin}
1712e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
172650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughessize_t linker_get_error_buffer_size() {
173650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  return sizeof(__linker_dl_err_buf);
174650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes}
175650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
1761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
1771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This function is an empty stub where GDB locates a breakpoint to get notified
1781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about linker activity.
1791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1805419b9474753d25dff947c7740532f86d130c0beElliott Hughesextern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
1811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1821728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
1833a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic r_debug _r_debug = {1, NULL, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
1843a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic link_map* r_debug_tail = 0;
1851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1863a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic void insert_soinfo_into_debug_map(soinfo* info) {
187bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    // Copy the necessary fields into the debug structure.
1883a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(info->link_map_head);
189ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov    map->l_addr = info->load_bias;
190faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    map->l_name = reinterpret_cast<char*>(info->name);
1913a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    map->l_ld = info->dynamic;
1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Stick the new library at the end of the list.
1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * gdb tends to care more about libc than it does
1951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * about leaf libraries, and ordering it this way
1961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * reduces the back-and-forth over the wire.
1971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
1981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (r_debug_tail) {
1991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        r_debug_tail->l_next = map;
2001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = r_debug_tail;
2011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    } else {
2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        _r_debug.r_map = map;
2041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = 0;
2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
2081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
210bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void remove_soinfo_from_debug_map(soinfo* info) {
2113a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(info->link_map_head);
2125e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
213bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (r_debug_tail == map) {
2145e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        r_debug_tail = map->l_prev;
215bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2165e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
217bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_prev) {
218bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_prev->l_next = map->l_next;
219bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
220bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_next) {
221bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_next->l_prev = map->l_prev;
222bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2235e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2245e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
225bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_load(soinfo* info) {
2261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (info->flags & FLAG_EXE) {
2271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        // GDB already knows about the main executable
2281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
2291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2311728b2396591853345507a063ed6075dfd251706Elliott Hughes    ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
2321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2333a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_ADD;
2341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(info);
2371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2383a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_CONSISTENT;
2391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2405e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2415e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
242bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_unload(soinfo* info) {
2435e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    if (info->flags & FLAG_EXE) {
2445e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        // GDB already knows about the main executable
2455e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        return;
2465e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    }
2475e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2481728b2396591853345507a063ed6075dfd251706Elliott Hughes    ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
2495e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2503a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_DELETE;
2515e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2525e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2535e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    remove_soinfo_from_debug_map(info);
2545e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2553a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_CONSISTENT;
2565e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
25918a206c81d9743481e364384affd43306911283dElliott Hughesvoid notify_gdb_of_libraries() {
2603a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  _r_debug.r_state = r_debug::RT_ADD;
2613a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  rtld_db_dlactivity();
2623a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  _r_debug.r_state = r_debug::RT_CONSISTENT;
2633a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  rtld_db_dlactivity();
2641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
266ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic soinfo* soinfo_alloc(const char* name) {
267ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (strlen(name) >= SOINFO_NAME_LEN) {
268ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    DL_ERR("library name \"%s\" too long", name);
269ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return NULL;
270ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
271ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
2721728b2396591853345507a063ed6075dfd251706Elliott Hughes  soinfo* si = g_soinfo_allocator.alloc();
2731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
274ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Initialize the new element.
275ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  memset(si, 0, sizeof(soinfo));
276ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  strlcpy(si->name, name, sizeof(si->name));
277ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext->next = si;
278ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext = si;
2791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
280ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("name %s: allocated soinfo @ %p", name, si);
281ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  return si;
2821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
284faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesstatic void soinfo_free(soinfo* si) {
2854688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si == NULL) {
2864688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return;
2874688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
2884688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
2891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *prev = NULL, *trav;
2901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
291ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("name %s: freeing soinfo @ %p", si->name, si);
2921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
293d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (trav = solist; trav != NULL; trav = trav->next) {
2941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (trav == si)
2951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
2961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        prev = trav;
2971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (trav == NULL) {
299d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        /* si was not in solist */
3004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("name \"%s\" is not in solist!", si->name);
3011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
3021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
304be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    /* prev will never be NULL, because the first entry in solist is
3051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       always the static libdl_info.
3061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    */
3071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    prev->next = si->next;
308d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si == sonext) {
309d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        sonext = prev;
310d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
311d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov
3121728b2396591853345507a063ed6075dfd251706Elliott Hughes    g_soinfo_allocator.free(si);
3131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
315cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
316cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_path(const char* path, const char* delimiters,
317cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes                       const char** array, char* buf, size_t buf_size, size_t max_count) {
318cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (path == NULL) {
319cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return;
320cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
321cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
322cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t len = strlcpy(buf, path, buf_size);
323cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
324cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t i = 0;
325cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  char* buf_p = buf;
326cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
327cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (*array[i] != '\0') {
328cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      ++i;
329cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    }
330cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
331cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
332cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // Forget the last path if we had to truncate; this occurs if the 2nd to
333cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // last char isn't '\0' (i.e. wasn't originally a delimiter).
334cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
335cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    array[i - 1] = NULL;
336cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  } else {
337cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    array[i] = NULL;
338cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
339cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
340cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
341cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_LIBRARY_PATH(const char* path) {
3421728b2396591853345507a063ed6075dfd251706Elliott Hughes  parse_path(path, ":", g_ld_library_paths,
3431728b2396591853345507a063ed6075dfd251706Elliott Hughes             g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
344cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
345cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
346cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_PRELOAD(const char* path) {
347cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // We have historically supported ':' as well as ' ' in LD_PRELOAD.
3481728b2396591853345507a063ed6075dfd251706Elliott Hughes  parse_path(path, " :", g_ld_preload_names,
3491728b2396591853345507a063ed6075dfd251706Elliott Hughes             g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
350cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
351cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
3524eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
3534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* For a given PC, find the .so that it belongs to.
3551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the base address of the .ARM.exidx section
3561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for that .so, and the number of 8-byte entries
3571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in that section (via *pcount).
3581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Intended to be called by libc's __gnu_Unwind_Find_exidx().
3601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3613b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes * This function is exposed via dlfcn.cpp and libdl.so.
3621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
363faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
3641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned addr = (unsigned)pc;
3651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
366faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    for (soinfo* si = solist; si != 0; si = si->next) {
367468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if ((addr >= si->base) && (addr < (si->base + si->size))) {
368468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            *pcount = si->ARM_exidx_count;
369f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            return (_Unwind_Ptr)si->ARM_exidx;
3701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
3711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
372faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    *pcount = 0;
3731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return NULL;
3741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3754688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
37624053a461e7a20f34002262c1bb122023134989dChristopher Ferris#endif
3774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Here, we only have to provide a callback to iterate across all the
3791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * loaded libraries. gcc_eh does the rest. */
380faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesint dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
3811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int rv = 0;
382bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    for (soinfo* si = solist; si != NULL; si = si->next) {
383bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        dl_phdr_info dl_info;
3843a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes        dl_info.dlpi_addr = si->link_map_head.l_addr;
3853a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes        dl_info.dlpi_name = si->link_map_head.l_name;
3861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phdr = si->phdr;
3871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phnum = si->phnum;
388bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        rv = cb(&dl_info, sizeof(dl_phdr_info), data);
389bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        if (rv != 0) {
3901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
391bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        }
3921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return rv;
3941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3954688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3960266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Sym)* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
3970266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* symtab = si->symtab;
3980266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  const char* strtab = si->strtab;
3991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4000266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd",
4010266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes             name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
4021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4030266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
4040266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s = symtab + n;
4050266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    if (strcmp(strtab + s->st_name, name)) continue;
4061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4070266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    /* only concern ourselves with global and weak symbol definitions */
4080266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    switch (ELF_ST_BIND(s->st_info)) {
4090266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      case STB_GLOBAL:
4100266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      case STB_WEAK:
4110266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if (s->st_shndx == SHN_UNDEF) {
4120266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        continue;
4130266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      }
4141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4150266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
4160266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                 name, si->name, reinterpret_cast<void*>(s->st_value),
4170266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                 static_cast<size_t>(s->st_size));
4180266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      return s;
4191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4200266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  }
4211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  return NULL;
4231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
425d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic unsigned elfhash(const char* _name) {
426faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    const unsigned char* name = reinterpret_cast<const unsigned char*>(_name);
4271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned h = 0, g;
4281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
429faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    while (*name) {
4301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h = (h << 4) + *name++;
4311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        g = h & 0xf0000000;
4321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g;
4331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g >> 24;
4341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return h;
4361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Sym)* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
439943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
4400266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s = NULL;
4416ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
442c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin    if (si != NULL && somain != NULL) {
4435ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        /*
444c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * Local scope is executable scope. Just start looking into it right away
445c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * for the shortcut.
4465ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel         */
4475ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
448c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        if (si == somain) {
449c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
4505ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (s != NULL) {
451c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
4525ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                goto done;
4535ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
454c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        } else {
455c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
456c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
457c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
458c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with symbolic relocations disabled, the
459c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * first place to look to resolve external references is the main
460c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * executable.
461c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
462c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
463c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (!si->has_DT_SYMBOLIC) {
464ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s",
46561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
466c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
467c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
468c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
469c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
470c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
471c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
4725ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
473c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Look for symbols in the local scope (the object who is
4744eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes             * searching). This happens with C++ templates on x86 for some
475c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * reason.
476c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             *
477c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Notes on weak symbols:
478c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * The ELF specs are ambiguous about treatment of weak definitions in
479c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * dynamic linking.  Some systems return the first definition found
480c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * and some the first non-weak definition.   This is system dependent.
481c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Here we return the first definition found for simplicity.  */
482c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
483c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
484c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (s != NULL) {
485c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
486c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                goto done;
487c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
488c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
489c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
490c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with -Bsymbolic and symbol is not found
491c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * in the local scope, try to find the symbol in the main executable.
492c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
493c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
494c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (si->has_DT_SYMBOLIC) {
495ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s after local scope",
49661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
497c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
498c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
499c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
500c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
501c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
502c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5035ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
504d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    }
5056ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5064fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* Next, look for it in the preloads list */
5071728b2396591853345507a063ed6075dfd251706Elliott Hughes    for (int i = 0; g_ld_preloads[i] != NULL; i++) {
5081728b2396591853345507a063ed6075dfd251706Elliott Hughes        s = soinfo_elf_lookup(g_ld_preloads[i], elf_hash, name);
509d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (s != NULL) {
5101728b2396591853345507a063ed6075dfd251706Elliott Hughes            *lsi = g_ld_preloads[i];
5114fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            goto done;
5125ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5134fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
5144fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
515d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (int i = 0; needed[i] != NULL; i++) {
516ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        DEBUG("%s: looking up %s in %s",
51761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes              si->name, name, needed[i]->name);
5185ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_elf_lookup(needed[i], elf_hash, name);
5195ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if (s != NULL) {
5205ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            *lsi = needed[i];
52112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            goto done;
5225ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5236ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5246ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5256ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevdone:
526d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (s != NULL) {
527c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
528c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   "found in %s, base = %p, load bias = %p",
529c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   si->name, name, reinterpret_cast<void*>(s->st_value),
530c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   (*lsi)->name, reinterpret_cast<void*>((*lsi)->base),
531c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   reinterpret_cast<void*>((*lsi)->load_bias));
5326ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        return s;
5336ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5346ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
535943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
5366ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev}
5376ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
538d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3).  It performs symbol lookup only within the
5396ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev   specified soinfo object and not in any of its dependencies.
540d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom
541d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
542d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   that it should do a breadth first search through the dependency
543d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   tree. This agrees with the ELF spec (aka System V Application
544d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   Binary Interface) where in Chapter 5 it discuss resolving "Shared
545d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   Object Dependencies" in breadth first search order.
5466ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
5470266ae5f884d72da58f33a072e865ba131234a5eElliott HughesElfW(Sym)* dlsym_handle_lookup(soinfo* si, const char* name) {
54816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    return soinfo_elf_lookup(si, elfhash(name), name);
5491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
551d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3) to performs a global symbol lookup. If the
552d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   start value is null (for RTLD_DEFAULT), the search starts at the
553d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   beginning of the global solist. Otherwise the search starts at the
554d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   specified soinfo (for RTLD_NEXT).
5556ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
5560266ae5f884d72da58f33a072e865ba131234a5eElliott HughesElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
557cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  unsigned elf_hash = elfhash(name);
5581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
559cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (start == NULL) {
560cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    start = solist;
561cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
5621698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer
5630266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* s = NULL;
564cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
565cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    s = soinfo_elf_lookup(si, elf_hash, name);
566cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (s != NULL) {
567cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      *found = si;
568cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      break;
5691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
570cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
5711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
572cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (s != NULL) {
573c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
574c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes               name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
575cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
5761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
577cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  return s;
5781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
580fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Chengsoinfo* find_containing_library(const void* p) {
5810266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
582fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (soinfo* si = solist; si != NULL; si = si->next) {
583fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (address >= si->base && address - si->base < si->size) {
584fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return si;
585e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
586fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
587fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return NULL;
588e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
589e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
5900266ae5f884d72da58f33a072e865ba131234a5eElliott HughesElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr) {
5910266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - si->base;
592fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
593fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // Search the library's symbol table for any defined symbol which
594fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // contains this address.
595fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (size_t i = 0; i < si->nchain; ++i) {
5960266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* sym = &si->symtab[i];
597fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (sym->st_shndx != SHN_UNDEF &&
598fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr >= sym->st_value &&
599fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr < sym->st_value + sym->st_size) {
600fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return sym;
601e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
602fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
603e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
604fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return NULL;
605e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
606e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
607124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library_on_path(const char* name, const char* const paths[]) {
608124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  char buf[512];
609124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  for (size_t i = 0; paths[i] != NULL; ++i) {
6101e980b6bc8315d00a07312b25486531247abd98cElliott Hughes    int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
611124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
612ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes      PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
613124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      continue;
6141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
615124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
616124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (fd != -1) {
617124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      return fd;
618124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
619124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
620124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return -1;
6211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
623124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library(const char* name) {
624ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ opening %s ]", name);
6251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
626124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // If the name contains a slash, we should attempt to open it directly and not search the paths.
627124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (strchr(name, '/') != NULL) {
6286971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
6296971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    if (fd != -1) {
6306971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes      return fd;
6316971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    }
6326971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
6335ca7ed9005ea16733d7c87d7154473b7a500be0cDmitriy Ivanov#if defined(__LP64__)
6345ca7ed9005ea16733d7c87d7154473b7a500be0cDmitriy Ivanov    return -1;
6355ca7ed9005ea16733d7c87d7154473b7a500be0cDmitriy Ivanov#endif
636124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
6371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
638124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
6391728b2396591853345507a063ed6075dfd251706Elliott Hughes  int fd = open_library_on_path(name, g_ld_library_paths);
640124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (fd == -1) {
6411728b2396591853345507a063ed6075dfd251706Elliott Hughes    fd = open_library_on_path(name, kDefaultLdPaths);
642124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
643124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return fd;
6441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
64612bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)static soinfo* load_library(const char* name, const android_dlextinfo* extinfo) {
6474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Open the file.
648650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    int fd = open_library(name);
649650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    if (fd == -1) {
6504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("library \"%s\" not found", name);
6511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
6522e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    }
6531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
654650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    // Read the ELF header and load the segments.
655650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    ElfReader elf_reader(name, fd);
65612bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)    if (!elf_reader.Load(extinfo)) {
6574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
65823363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
65923363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner
660650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    const char* bname = strrchr(name, '/');
661650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
662650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    if (si == NULL) {
6634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
66423363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
665650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->base = elf_reader.load_start();
666650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->size = elf_reader.load_size();
667650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->load_bias = elf_reader.load_bias();
668650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->flags = 0;
669650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->entry = 0;
670650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->dynamic = NULL;
671650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phnum = elf_reader.phdr_count();
672650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phdr = elf_reader.loaded_phdr();
673650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    return si;
6741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
676faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesstatic soinfo *find_loaded_library(const char* name) {
67712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // TODO: don't use basename only for determining libraries
67812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // http://code.google.com/p/android/issues/detail?id=6670
67912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
680faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    const char* bname = strrchr(name, '/');
68112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    bname = bname ? bname + 1 : name;
68212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
683faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    for (soinfo* si = solist; si != NULL; si = si->next) {
684d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (!strcmp(bname, si->name)) {
68512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            return si;
68612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        }
68712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    }
68812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    return NULL;
68912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel}
69012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
69112bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)static soinfo* find_library_internal(const char* name, const android_dlextinfo* extinfo) {
692d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (name == NULL) {
693d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return somain;
694d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
695d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
696d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_loaded_library(name);
697d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
698d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_LINKED) {
699d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      return si;
700d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    }
701d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    DL_ERR("OOPS: recursive link to \"%s\"", si->name);
702d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return NULL;
703d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
70412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
705ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ '%s' has not been loaded yet.  Locating...]", name);
70612bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)  si = load_library(name, extinfo);
707cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (si == NULL) {
708cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return NULL;
709cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
710cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
711cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // At this point we know that whatever is loaded @ base is a valid ELF
712cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // shared library whose segments are properly mapped in.
7135ceb889ac9a36e74e4e00c34c57cbb58d73e228cWeiwu Chen  TRACE("[ find_library_internal base=%p size=%zu name='%s' ]",
714c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        reinterpret_cast<void*>(si->base), si->size, si->name);
715cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
716183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)  if (!soinfo_link_image(si, extinfo)) {
717cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    munmap(reinterpret_cast<void*>(si->base), si->size);
718cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    soinfo_free(si);
719cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return NULL;
720d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
7216774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
722d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
723d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
724d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
72512bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)static soinfo* find_library(const char* name, const android_dlextinfo* extinfo) {
72612bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)  soinfo* si = find_library_internal(name, extinfo);
727d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
728ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count++;
729d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
730d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
731d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
732d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
733d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic int soinfo_unload(soinfo* si) {
734ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  if (si->ref_count == 1) {
735ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("unloading '%s'", si->name);
736d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallDestructors();
737d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
7380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
739d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom      if (d->d_tag == DT_NEEDED) {
740d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        const char* library_name = si->strtab + d->d_un.d_val;
7418147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes        TRACE("%s needs to unload %s", si->name, library_name);
7428147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes        soinfo_unload(find_loaded_library(library_name));
743d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
7441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
7451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
746d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    munmap(reinterpret_cast<void*>(si->base), si->size);
747d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    notify_gdb_of_unload(si);
748ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 0;
749d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov    soinfo_free(si);
750d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  } else {
751ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count--;
752c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
753d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
754d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return 0;
7551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
757a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughesvoid do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
7581728b2396591853345507a063ed6075dfd251706Elliott Hughes  snprintf(buffer, buffer_size, "%s:%s", kDefaultLdPaths[0], kDefaultLdPaths[1]);
759a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes}
760a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes
761cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesvoid do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
762cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (!get_AT_SECURE()) {
763cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    parse_LD_LIBRARY_PATH(ld_library_path);
764cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
765cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
766cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
767012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
768e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes  if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
769e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    DL_ERR("invalid flags to dlopen: %x", flags);
770e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    return NULL;
771e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes  }
772012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)  if (extinfo != NULL && ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0)) {
773012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)    DL_ERR("invalid extended flags to android_dlopen_ext: %x", extinfo->flags);
774012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)    return NULL;
775012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)  }
7761728b2396591853345507a063ed6075dfd251706Elliott Hughes  g_soinfo_allocator.protect_all(PROT_READ | PROT_WRITE);
77712bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)  soinfo* si = find_library(name, extinfo);
778d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
779d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
780d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
7811728b2396591853345507a063ed6075dfd251706Elliott Hughes  g_soinfo_allocator.protect_all(PROT_READ);
782d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
783d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
7841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
785d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesint do_dlclose(soinfo* si) {
7861728b2396591853345507a063ed6075dfd251706Elliott Hughes  g_soinfo_allocator.protect_all(PROT_READ | PROT_WRITE);
787d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  int result = soinfo_unload(si);
7881728b2396591853345507a063ed6075dfd251706Elliott Hughes  g_soinfo_allocator.protect_all(PROT_READ);
789d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return result;
7901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
7924eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
7939918665a45095ad135576f005c0e5307feb366a1Chris Dearmanstatic int soinfo_relocate(soinfo* si, ElfW(Rela)* rela, unsigned count, soinfo* needed[]) {
7940266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* s;
795c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  soinfo* lsi;
796c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
797c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  for (size_t idx = 0; idx < count; ++idx, ++rela) {
7980266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    unsigned type = ELFW(R_TYPE)(rela->r_info);
7990266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    unsigned sym = ELFW(R_SYM)(rela->r_info);
8000266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + si->load_bias);
8010266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Addr) sym_addr = 0;
802faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    const char* sym_name = NULL;
803c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
804c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
805c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (type == 0) { // R_*_NONE
806c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      continue;
807c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
808c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (sym != 0) {
809c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes      sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
810c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      s = soinfo_do_lookup(si, sym_name, &lsi, needed);
811c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      if (s == NULL) {
812c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        // We only allow an undefined symbol if this is a weak reference...
813c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes        s = &si->symtab[sym];
814c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
815c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
816c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          return -1;
817c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
818c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
819c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        /* IHI0044C AAELF 4.5.1.1:
820c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
821c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           Libraries are not searched to resolve weak references.
822c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           It is not an error for a weak reference to remain unsatisfied.
823c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
824c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           During linking, the value of an undefined weak reference is:
825c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - Zero if the relocation type is absolute
826c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - The address of the place if the relocation is pc-relative
827c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - The address of nominal base address if the relocation
828c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes             type is base-relative.
829c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         */
830c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
831c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (type) {
832e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__)
833e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_JUMP_SLOT:
834e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_GLOB_DAT:
835e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS64:
836e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS32:
837e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS16:
838e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_RELATIVE:
839e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland          /*
840e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * The sym_addr was initialized to be zero above, or the relocation
841e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * code below does not care about value of sym_addr.
842e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * No need to do anything.
843e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           */
844e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland          break;
845e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__)
846c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_JUMP_SLOT:
847c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_GLOB_DAT:
848c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_32:
849c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_RELATIVE:
850c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          // No need to do anything.
851c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          break;
852c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_PC32:
853c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          sym_addr = reloc;
854c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          break;
8554eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
856c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        default:
857faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes          DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx);
858c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          return -1;
859c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
860c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      } else {
861c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        // We got a definition.
8620266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
863c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      }
864c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocSymbol);
865c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    } else {
866c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      s = NULL;
867c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
868c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
869c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    switch (type) {
870e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__)
871e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_JUMP_SLOT:
872e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
873e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
8740266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n",
8750266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
8760266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
877e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
878e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_GLOB_DAT:
879e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
880e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
8810266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n",
8820266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
8830266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
884e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
885e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS64:
886e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
887e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
8880266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
8890266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
8900266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
891e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
892e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS32:
893e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
894e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
8950266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
8960266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
8970266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
8980266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
8990266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
900e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
9010266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
9020266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
9030266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT32_MIN),
9040266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT32_MAX));
905e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
906e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
907e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
908e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS16:
909e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
910e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9110266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
9120266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9130266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
9140266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
9150266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
916e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
9170266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
9180266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
9190266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT16_MIN),
9200266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT16_MAX));
921e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
922e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
923e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
924e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL64:
925e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
926e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9270266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
9280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
9290266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
930e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
931e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL32:
932e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
933e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9340266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
9350266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
9360266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
9370266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
9380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
939e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
9400266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
9410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
9420266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT32_MIN),
9430266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT32_MAX));
944e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
945e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
946e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
947e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL16:
948e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
949e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9500266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
9510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
9520266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
9530266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
9540266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
955e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
9560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
9570266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
9580266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT16_MIN),
9590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT16_MAX));
960e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
961e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
962e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
963e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
964e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_RELATIVE:
965e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
966e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
967e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        if (sym) {
968e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            DL_ERR("odd RELATIVE form...");
969e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
970e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
9710266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n",
9720266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (si->base + rela->r_addend));
9730266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (si->base + rela->r_addend);
974e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
975e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
976e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_COPY:
977b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov        /*
978b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         * ET_EXEC is not supported so this should not happen.
979b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         *
980b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
981b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         *
982b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         * Section 4.7.1.10 "Dynamic relocations"
983b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         * R_AARCH64_COPY may only appear in executable objects where e_type is
984b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         * set to ET_EXEC.
985b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         */
986b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov        DL_ERR("%s R_AARCH64_COPY relocations are not supported", si->name);
987b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov        return -1;
988e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_TLS_TPREL64:
9890266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
9900266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset);
991e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
992e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_TLS_DTPREL32:
9930266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
9940266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset);
995e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
996e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__)
997c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_JUMP_SLOT:
998c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocAbsolute);
999c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1000c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1001c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
10020266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1003c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1004c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_GLOB_DAT:
1005c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocAbsolute);
1006c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1007c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1008c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
10090266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1010c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1011c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_RELATIVE:
1012c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1013c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1014c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      if (sym) {
1015c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DL_ERR("odd RELATIVE form...");
1016c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        return -1;
1017c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      }
1018c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
1019c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(si->base));
10200266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = si->base + rela->r_addend;
1021c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1022c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_32:
1023c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1024c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1025c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1026c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr), sym_name);
10270266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1028c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1029c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin    case R_X86_64_64:
1030c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      count_relocation(kRelocRelative);
1031c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      MARK(rela->r_offset);
1032c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1033c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin                 static_cast<size_t>(sym_addr), sym_name);
10340266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1035c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      break;
1036c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_PC32:
1037c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1038c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1039c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1040c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1041c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
10420266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc;
1043c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
10444eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1045e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1046c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    default:
1047faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes      DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx);
1048c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      return -1;
1049c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1050c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  }
1051c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  return 0;
1052c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes}
10539918665a45095ad135576f005c0e5307feb366a1Chris Dearman
10549918665a45095ad135576f005c0e5307feb366a1Chris Dearman#else // REL, not RELA.
10559918665a45095ad135576f005c0e5307feb366a1Chris Dearman
10560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic int soinfo_relocate(soinfo* si, ElfW(Rel)* rel, unsigned count, soinfo* needed[]) {
10570266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s;
1058d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo* lsi;
10591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
10604688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    for (size_t idx = 0; idx < count; ++idx, ++rel) {
10610266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        unsigned type = ELFW(R_TYPE)(rel->r_info);
10620266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead.
10630266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        unsigned sym = ELFW(R_SYM)(rel->r_info);
10640266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + si->load_bias);
10650266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        ElfW(Addr) sym_addr = 0;
1066faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        const char* sym_name = NULL;
10671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1068c620059479c47a78d57086d73726c9adc2f337adElliott Hughes        DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
1069d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (type == 0) { // R_*_NONE
1070d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            continue;
1071d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1072d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (sym != 0) {
1073c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes            sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
10745ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1075d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (s == NULL) {
10764eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                // We only allow an undefined symbol if this is a weak reference...
1077c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes                s = &si->symtab[sym];
1078c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1079e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes                    DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
1080e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1081e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1082e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1083e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* IHI0044C AAELF 4.5.1.1:
1084e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1085e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   Libraries are not searched to resolve weak references.
1086e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   It is not an error for a weak reference to remain
1087e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   unsatisfied.
1088e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1089e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   During linking, the value of an undefined weak reference is:
1090e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - Zero if the relocation type is absolute
1091e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - The address of the place if the relocation is pc-relative
1092bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes                   - The address of nominal base address if the relocation
1093e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                     type is base-relative.
1094e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                  */
1095e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1096e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                switch (type) {
10974eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
1098e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_JUMP_SLOT:
1099e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_GLOB_DAT:
1100e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_ABS32:
1101e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_RELATIVE:    /* Don't care. */
11024eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // sym_addr was initialized to be zero above or relocation
11034eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // code below does not care about value of sym_addr.
11044eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // No need to do anything.
11054eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    break;
11064eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
1107d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                case R_386_JMP_SLOT:
1108e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_GLOB_DAT:
1109e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_32:
1110c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                case R_386_RELATIVE:    /* Don't care. */
11114eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // sym_addr was initialized to be zero above or relocation
11124eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // code below does not care about value of sym_addr.
11134eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // No need to do anything.
1114e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
1115e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_PC32:
1116e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    sym_addr = reloc;
1117e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
11184eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1119e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
11204eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
1121e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_COPY:
11224eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // Fall through. Can't really copy if weak symbol is not found at run-time.
11234eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1124e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                default:
1125faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                    DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
1126e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1127e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1128e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            } else {
11294eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                // We got a definition.
11300266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
1131d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1132bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocSymbol);
11331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        } else {
1134e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            s = NULL;
11351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
11361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1137c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (type) {
11384eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
11391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_JUMP_SLOT:
1140bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
11411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1142ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
11430266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
11441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_GLOB_DAT:
1146bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
11471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1148ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
11490266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
11501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_ABS32:
1152bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
11531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1154ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
11550266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
11561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
115734ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner        case R_ARM_REL32:
1158bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
115934ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            MARK(rel->r_offset);
1160ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
116134ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner                       reloc, sym_addr, rel->r_offset, sym_name);
11620266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
116334ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            break;
11641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_COPY:
1165b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov            /*
11666275f2083415d22a6ce0de55645079cd47e0cc80Dmitriy Ivanov             * ET_EXEC is not supported so this should not happen.
11676275f2083415d22a6ce0de55645079cd47e0cc80Dmitriy Ivanov             *
1168b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov             * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1169b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov             *
1170b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov             * Section 4.7.1.10 "Dynamic relocations"
1171b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov             * R_ARM_COPY may only appear in executable objects where e_type is
1172b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov             * set to ET_EXEC.
1173b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov             */
1174b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov            DL_ERR("%s R_ARM_COPY relocations are not supported", si->name);
1175b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov            return -1;
11764eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
11774eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_JMP_SLOT:
11784eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
11794eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
11804eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
11810266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
11824eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
11834eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_GLOB_DAT:
11844eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
11854eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
11864eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
11870266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
11884eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
11894eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_32:
11904eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
11914eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
11924eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
11930266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
11944eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
11954eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_PC32:
11964eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
11974eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
11984eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
11994eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                       reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
12000266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
12014eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
12024eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__mips__)
12034eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_MIPS_REL32:
12049918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__LP64__)
12059918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // MIPS Elf64_Rel entries contain compound relocations
12069918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
12079918665a45095ad135576f005c0e5307feb366a1Chris Dearman            if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
12089918665a45095ad135576f005c0e5307feb366a1Chris Dearman                ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
1209faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
12109918665a45095ad135576f005c0e5307feb366a1Chris Dearman                       type, (unsigned)ELF64_R_TYPE2(rel->r_info),
1211faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                       (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx);
12129918665a45095ad135576f005c0e5307feb366a1Chris Dearman                return -1;
12139918665a45095ad135576f005c0e5307feb366a1Chris Dearman            }
12149918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif
12154eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
12164eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
12179918665a45095ad135576f005c0e5307feb366a1Chris Dearman            TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
12189918665a45095ad135576f005c0e5307feb366a1Chris Dearman                       static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
12194eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            if (s) {
12200266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
12214eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            } else {
12220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base;
12234eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            }
12244eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
12254eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
12264eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes
12274eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
12284eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_ARM_RELATIVE:
12294eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
12304eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_RELATIVE:
12314eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
12324eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
12334eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
12344eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            if (sym) {
12354eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                DL_ERR("odd RELATIVE form...");
12364eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                return -1;
12374eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            }
12384eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
12394eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                       reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base));
12400266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base;
12414eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
12421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        default:
1244faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
12451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return -1;
12461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
12471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
12481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
12491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
1250c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
12511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
12524eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
125387c358524e479235aa6241736d2ce325f89daafcBrian Carlstromstatic bool mips_relocate_got(soinfo* si, soinfo* needed[]) {
12549918665a45095ad135576f005c0e5307feb366a1Chris Dearman    ElfW(Addr)** got = si->plt_got;
125587c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    if (got == NULL) {
125687c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom        return true;
125787c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    }
125887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned local_gotno = si->mips_local_gotno;
125987c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned gotsym = si->mips_gotsym;
126087c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned symtabno = si->mips_symtabno;
12610266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* symtab = si->symtab;
1262d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
12639918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // got[0] is the address of the lazy resolver function.
12649918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // got[1] may be used for a GNU extension.
12659918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
12669918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // FIXME: maybe this should be in a separate routine?
1267d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if ((si->flags & FLAG_LINKER) == 0) {
126887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom        size_t g = 0;
12699918665a45095ad135576f005c0e5307feb366a1Chris Dearman        got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
12709918665a45095ad135576f005c0e5307feb366a1Chris Dearman        if (reinterpret_cast<intptr_t>(got[g]) < 0) {
12719918665a45095ad135576f005c0e5307feb366a1Chris Dearman            got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
1272d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
12739918665a45095ad135576f005c0e5307feb366a1Chris Dearman        // Relocate the local GOT entries.
1274d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        for (; g < local_gotno; g++) {
12759918665a45095ad135576f005c0e5307feb366a1Chris Dearman            got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + si->load_bias);
1276d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1277d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1278d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
12799918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // Now for the global GOT entries...
12800266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* sym = symtab + gotsym;
1281d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got + local_gotno;
128287c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
12839918665a45095ad135576f005c0e5307feb366a1Chris Dearman        // This is an undefined reference... try to locate it.
12849918665a45095ad135576f005c0e5307feb366a1Chris Dearman        const char* sym_name = si->strtab + sym->st_name;
1285d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        soinfo* lsi;
12869918665a45095ad135576f005c0e5307feb366a1Chris Dearman        ElfW(Sym)* s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1287d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (s == NULL) {
12889918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // We only allow an undefined symbol if this is a weak reference.
1289d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            s = &symtab[g];
1290c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
12914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("cannot locate \"%s\"...", sym_name);
129287c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom                return false;
1293d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1294d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *got = 0;
12959918665a45095ad135576f005c0e5307feb366a1Chris Dearman        } else {
12969918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // FIXME: is this sufficient?
12979918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // For reference see NetBSD link loader
12989918665a45095ad135576f005c0e5307feb366a1Chris 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
12999918665a45095ad135576f005c0e5307feb366a1Chris Dearman            *got = reinterpret_cast<ElfW(Addr)*>(lsi->load_bias + s->st_value);
1300d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1301d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
130287c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    return true;
1303d7daacb46372132ae3f0121647074936c304b572Raghu Gandham}
1304d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1305d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1306812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallArray(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
1307ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  if (functions == NULL) {
1308d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1309d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
13108215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1311c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
13128215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1313ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int begin = reverse ? (count - 1) : 0;
1314ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int end = reverse ? -1 : count;
1315ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int step = reverse ? -1 : 1;
13168215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1317ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  for (int i = begin; i != end; i += step) {
1318ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1319ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    CallFunction("function", functions[i]);
1320d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1321d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1322ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s for '%s' ]", array_name, name);
13231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
13241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1325812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallFunction(const char* function_name __unused, linker_function_t function) {
1326db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
1327d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1328d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1329d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1330ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
1331d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  function();
1332ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
1333db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes
1334db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1335db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // are still writable. This happens with our debug malloc (see http://b/7941716).
13361728b2396591853345507a063ed6075dfd251706Elliott Hughes  g_soinfo_allocator.protect_all(PROT_READ | PROT_WRITE);
13379181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov}
13389181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
1339d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallPreInitConstructors() {
13408147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
13418147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // but ignored in a shared library.
1342d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1343d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
1344e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1345d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallConstructors() {
1346d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (constructors_called) {
1347d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1348d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1349e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1350d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // We set constructors_called before actually calling the constructors, otherwise it doesn't
1351d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // protect against recursive constructor calls. One simple example of constructor recursion
1352d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1353d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 1. The program depends on libc, so libc's constructor is called here.
1354d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1355d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 3. dlopen() calls the constructors on the newly created
1356d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    soinfo for libc_malloc_debug_leak.so.
1357d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 4. The debug .so depends on libc, so CallConstructors is
1358d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    called again with the libc soinfo. If it doesn't trigger the early-
1359d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    out above, the libc constructor will be called again (recursively!).
1360d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  constructors_called = true;
1361d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
13628147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) {
13638147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes    // The GNU dynamic linker silently ignores these, but we warn the developer.
1364c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
13658147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes          name, preinit_array_count);
1366d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
13671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1368d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom  if (dynamic != NULL) {
13690266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
1370d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom      if (d->d_tag == DT_NEEDED) {
1371d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        const char* library_name = strtab + d->d_un.d_val;
13728147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes        TRACE("\"%s\": calling constructors in DT_NEEDED \"%s\"", name, library_name);
13738147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes        find_loaded_library(library_name)->CallConstructors();
1374d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
13751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1376d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
13771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13788147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  TRACE("\"%s\": calling constructors", name);
13798147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
13808147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_INIT should be called before DT_INIT_ARRAY if both are present.
1381d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_INIT", init_func);
1382d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
1383e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov}
13848215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1385d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallDestructors() {
13868147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  TRACE("\"%s\": calling destructors", name);
13878147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
13888147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_FINI_ARRAY must be parsed in reverse order.
1389d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
13908147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
13918147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_FINI should be called after DT_FINI_ARRAY if both are present.
1392d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_FINI", fini_func);
13931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
13941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with
13961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   /dev/null. */
13975419b9474753d25dff947c7740532f86d130c0beElliott Hughesstatic int nullify_closed_stdio() {
13981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int dev_null, i, status;
13991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int return_value = 0;
14001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
140116084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
14021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null < 0) {
14034688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("cannot open /dev/null: %s", strerror(errno));
14041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return -1;
14051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1406ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
14071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If any of the stdio file descriptors is valid and not associated
14091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       with /dev/null, dup /dev/null to it.  */
14101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (i = 0; i < 3; i++) {
14111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* If it is /dev/null already, we are done. */
14124688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (i == dev_null) {
14131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
14144688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
14151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1416ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Nullifying stdio file descriptor %d]", i);
14174688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
14181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        /* If file is opened, we are good. */
14204688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status != -1) {
14214688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            continue;
14224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
14231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* The only error we allow is that the file descriptor does not
14251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           exist, in which case we dup /dev/null to it. */
14261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (errno != EBADF) {
14274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("fcntl failed: %s", strerror(errno));
14281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
14301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* Try dupping /dev/null to this stdio file descriptor and
14331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           repeat if there is a signal.  Note that any errors in closing
14341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           the stdio descriptor are lost.  */
14354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
14361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (status < 0) {
14374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("dup2 failed: %s", strerror(errno));
14381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
14401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If /dev/null is not one of the stdio file descriptors, close it. */
14441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null > 2) {
1445ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
14464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(close(dev_null));
14474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status == -1) {
14484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("close failed: %s", strerror(errno));
14491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return return_value;
14541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1456183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo) {
1457f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee    /* "base" might wrap around UINT32_MAX. */
14580266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Addr) base = si->load_bias;
14590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    const ElfW(Phdr)* phdr = si->phdr;
14601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int phnum = si->phnum;
1461d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
14621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1463b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can't debug anything until the linker is relocated */
1464b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (!relocating_linker) {
1465ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        INFO("[ linking %s ]", si->name);
1466c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags);
1467b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
14681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
146963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    /* Extract dynamic section */
1470124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    size_t dynamic_count;
14710266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Word) dynamic_flags;
147212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
1473cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman                                   &dynamic_count, &dynamic_flags);
147463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    if (si->dynamic == NULL) {
1475b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1476124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
1477b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
1478124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
147963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    } else {
1480b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1481ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("dynamic = %p", si->dynamic);
1482b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
148363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    }
148463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
14854eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
148663f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    (void) phdr_table_get_arm_exidx(phdr, phnum, base,
148763f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner                                    &si->ARM_exidx, &si->ARM_exidx_count);
148863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif
148963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
14908147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes    // Extract useful information from dynamic section.
1491d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    uint32_t needed_count = 0;
14920266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1493c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1494c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes              d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1495c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (d->d_tag) {
14961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_HASH:
1497faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->nbucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[0];
1498faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->nchain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[1];
1499faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->bucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8);
1500faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->chain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8 + si->nbucket * 4);
15011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_STRTAB:
1503faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->strtab = reinterpret_cast<const char*>(base + d->d_un.d_ptr);
15041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_SYMTAB:
1506faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->symtab = reinterpret_cast<ElfW(Sym)*>(base + d->d_un.d_ptr);
15071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15084eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if !defined(__LP64__)
15091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTREL:
1510d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val != DT_REL) {
1511124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1512124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
15131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
15141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1515c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
15161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_JMPREL:
15174eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1518faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->plt_rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr);
1519c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1520faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->plt_rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr);
1521c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
15221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTRELSZ:
15244eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
15250266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->plt_rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
1526c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
15270266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->plt_rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
1528c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
15291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15309918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__mips__)
15311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTGOT:
15329918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // Used by mips and mips64.
15339918665a45095ad135576f005c0e5307feb366a1Chris Dearman            si->plt_got = reinterpret_cast<ElfW(Addr)**>(base + d->d_un.d_ptr);
15341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1535c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
15361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_DEBUG:
1537bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_DEBUG entry to the address of _r_debug for GDB
1538cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman            // if the dynamic table is writable
15399918665a45095ad135576f005c0e5307feb366a1Chris Dearman// FIXME: not working currently for N64
15409918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The flags for the LOAD and DYNAMIC program headers do not agree.
15419918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The LOAD section containng the dynamic table has been mapped as
15429918665a45095ad135576f005c0e5307feb366a1Chris Dearman// read-only, but the DYNAMIC header claims it is writable.
15439918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if !(defined(__mips__) && defined(__LP64__))
154499c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            if ((dynamic_flags & PF_W) != 0) {
1545c620059479c47a78d57086d73726c9adc2f337adElliott Hughes                d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
154699c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            }
15471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15489918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif
15494eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1550c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         case DT_RELA:
1551faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr);
1552c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1553c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         case DT_RELASZ:
15540266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
1555c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1556c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_REL:
1557c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            DL_ERR("unsupported DT_REL in \"%s\"", si->name);
1558c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1559c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_RELSZ:
1560c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name);
1561c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1562c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1563c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_REL:
1564faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr);
1565c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1566c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_RELSZ:
15670266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
1568c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1569ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI         case DT_RELA:
1570124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1571124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1572c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
15731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT:
1574ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
15758147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func);
15761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI:
1578ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
15798147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func);
15801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAY:
1582ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
15838147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array);
15841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAYSZ:
15860266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
15871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAY:
1589ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
15908147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array);
15911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAYSZ:
15930266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
15941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAY:
1596ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
15978147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array);
15981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAYSZ:
16000266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
16011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_TEXTREL:
1603e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__)
1604e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes            DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1605e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes            return false;
1606e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else
16075135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            si->has_text_relocations = true;
16081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1609e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
16105ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_SYMBOLIC:
16115ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            si->has_DT_SYMBOLIC = true;
16125ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
1613d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        case DT_NEEDED:
1614d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            ++needed_count;
1615d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            break;
16165ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_FLAGS:
1617d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_TEXTREL) {
1618e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__)
1619e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes                DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1620e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes                return false;
1621e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else
16225ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_text_relocations = true;
1623e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
16245ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
1625d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_SYMBOLIC) {
16265ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_DT_SYMBOLIC = true;
16275ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
16285ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
16294eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
1630d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_STRSZ:
1631d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_SYMENT:
1632d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_RELENT:
1633d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             break;
1634d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_MAP:
1635bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
1636d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            {
1637006f9ad8910c945cd50e54f6c34d8c27b11c1193Benjamin Adolphi              r_debug** dp = reinterpret_cast<r_debug**>(base + d->d_un.d_ptr);
1638d7daacb46372132ae3f0121647074936c304b572Raghu Gandham              *dp = &_r_debug;
1639d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1640d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1641d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_VERSION:
1642d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_FLAGS:
1643d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_BASE_ADDRESS:
1644d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_UNREFEXTNO:
1645d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1646d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1647d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_SYMTABNO:
1648d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_symtabno = d->d_un.d_val;
1649d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1650d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1651d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_LOCAL_GOTNO:
1652d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_local_gotno = d->d_un.d_val;
1653d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1654d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1655d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_GOTSYM:
1656d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_gotsym = d->d_un.d_val;
1657d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
16584eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1659d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1660d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        default:
16614eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            DEBUG("Unused DT entry: type %p arg %p",
16624eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                  reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1663d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
16641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
16651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1667c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
1668c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          reinterpret_cast<void*>(si->base), si->strtab, si->symtab);
16691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1670124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    // Sanity checks.
1671d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (relocating_linker && needed_count != 0) {
1672d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1673d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        return false;
1674d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
1675124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->nbucket == 0) {
1676124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1677124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1678124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1679124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->strtab == 0) {
1680124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1681124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1682124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1683124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->symtab == 0) {
1684124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1685124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
16861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16887e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes    // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
1689d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_EXE) {
16901728b2396591853345507a063ed6075dfd251706Elliott Hughes        memset(g_ld_preloads, 0, sizeof(g_ld_preloads));
16917e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes        size_t preload_count = 0;
16921728b2396591853345507a063ed6075dfd251706Elliott Hughes        for (size_t i = 0; g_ld_preload_names[i] != NULL; i++) {
16931728b2396591853345507a063ed6075dfd251706Elliott Hughes            soinfo* lsi = find_library(g_ld_preload_names[i], NULL);
16947e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes            if (lsi != NULL) {
16951728b2396591853345507a063ed6075dfd251706Elliott Hughes                g_ld_preloads[preload_count++] = lsi;
16967e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes            } else {
16977e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                // As with glibc, failure to load an LD_PRELOAD library is just a warning.
16987e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
16991728b2396591853345507a063ed6075dfd251706Elliott Hughes                        g_ld_preload_names[i], si->name, linker_get_error_buffer());
17004fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            }
17014fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        }
17024fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
17034fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1704faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    soinfo** needed = reinterpret_cast<soinfo**>(alloca((1 + needed_count) * sizeof(soinfo*)));
1705d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo** pneeded = needed;
170612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
17070266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1708d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (d->d_tag == DT_NEEDED) {
1709d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            const char* library_name = si->strtab + d->d_un.d_val;
1710ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("%s needs %s", si->name, library_name);
171112bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)            soinfo* lsi = find_library(library_name, NULL);
1712d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            if (lsi == NULL) {
1713650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes                strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
17144688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1715d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                       library_name, si->name, tmp_err_buf);
1716124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
17171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
171812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            *pneeded++ = lsi;
17191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
17201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
172112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    *pneeded = NULL;
17221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1723e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__)
17245135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
1725e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // Make segments writable to allow text relocations to work properly. We will later call
1726e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // phdr_table_protect_segments() after all of them are applied and all constructors are run.
1727865119efd47ae3cae5a0c09c6a9cc184a10614c1Du Chenyang#if !defined(__i386__) // The platform itself has too many text relocations on x86.
17283d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich        DL_WARN("%s has text relocations. This is wasting memory and prevents "
17293d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich                "security hardening. Please fix.", si->name);
1730865119efd47ae3cae5a0c09c6a9cc184a10614c1Du Chenyang#endif
17315135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
17325135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't unprotect loadable segments for \"%s\": %s",
17335135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1734124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
17355135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
17365135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    }
1737e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
17385135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich
17394eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1740c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (si->plt_rela != NULL) {
1741faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s plt ]\n", si->name);
17429918665a45095ad135576f005c0e5307feb366a1Chris Dearman        if (soinfo_relocate(si, si->plt_rela, si->plt_rela_count, needed)) {
1743c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1744c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
1745c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1746c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (si->rela != NULL) {
1747faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s ]\n", si->name);
17489918665a45095ad135576f005c0e5307feb366a1Chris Dearman        if (soinfo_relocate(si, si->rela, si->rela_count, needed)) {
1749c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1750c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
1751c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1752c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1753d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si->plt_rel != NULL) {
1754faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s plt ]", si->name);
1755d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1756124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1757124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
17581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1759d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si->rel != NULL) {
1760faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s ]", si->name);
1761d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1762124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1763124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
17641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1765c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
17661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17674eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
176887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    if (!mips_relocate_got(si, needed)) {
1769124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1770d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1771d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1772d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
17731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_LINKED;
1774ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    DEBUG("[ finished linking %s ]", si->name);
17751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1776e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__)
17775135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
1778e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // All relocations are done, we can protect our segments back to read-only.
17795135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
17805135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't protect segments for \"%s\": %s",
17815135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1782124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
17835135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
17841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1785e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
17861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1787b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can also turn on GNU RELRO protection */
1788b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
17894688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
17904688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               si->name, strerror(errno));
1791124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
17929ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich    }
17939ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
1794183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    /* Handle serializing/sharing the RELRO segment */
1795183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
1796183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      if (phdr_table_serialize_gnu_relro(si->phdr, si->phnum, si->load_bias,
1797183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)                                         extinfo->relro_fd) < 0) {
1798183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
1799183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)               si->name, strerror(errno));
1800183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        return false;
1801183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      }
1802183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
1803183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      if (phdr_table_map_gnu_relro(si->phdr, si->phnum, si->load_bias,
1804183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)                                   extinfo->relro_fd) < 0) {
1805183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
1806183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)               si->name, strerror(errno));
1807183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        return false;
1808183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      }
1809183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    }
1810183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)
18111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    notify_gdb_of_load(si);
1812124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    return true;
18131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
18141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1815468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1816c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * This function add vdso to internal dso list.
1817c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * It helps to stack unwinding through signal handlers.
1818c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * Also, it makes bionic more like glibc.
1819c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov */
1820812fd4263a005b88f3b4222baa910114f938d594Kito Chengstatic void add_vdso(KernelArgumentBlock& args __unused) {
18214eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(AT_SYSINFO_EHDR)
18220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
18230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  if (ehdr_vdso == NULL) {
18240266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    return;
18250266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  }
1826c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
18270266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  soinfo* si = soinfo_alloc("[vdso]");
1828ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov
18290266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
18300266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->phnum = ehdr_vdso->e_phnum;
18310266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
18320266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->size = phdr_table_get_load_size(si->phdr, si->phnum);
18330266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->flags = 0;
18340266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
1835ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov
18360dcf06f976670b5d05286b184d150f61b71c1184Torne (Richard Coles)  soinfo_link_image(si, NULL);
1837c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov#endif
1838c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov}
1839c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
1840c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov/*
1841468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and
1842468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs
1843468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point.
1844468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
18450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
184642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    /* NOTE: we store the args pointer on a special location
1847ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       of the temporary TLS area in order to pass it to
1848ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       the C Library's runtime initializer.
1849ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *
1850ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       The initializer must clear the slot and reset the TLS
1851ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       to point to a different location to ensure that no other
1852ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       shared library constructor can access it.
1853ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     */
1854d3920b3a996b358e48232f417aa0a1e44a60f155Elliott Hughes  __libc_init_tls(args);
18551a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov
18561a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING
18571a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    struct timeval t0, t1;
18581a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    gettimeofday(&t0, 0);
18591a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif
18601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
186118a206c81d9743481e364384affd43306911283dElliott Hughes    // Initialize environment functions, and get to the ELF aux vectors table.
186242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    linker_env_init(args);
1863be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
18648d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // If this is a setuid/setgid program, close the security hole described in
18658d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
18668d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    if (get_AT_SECURE()) {
18678d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich        nullify_closed_stdio();
18688d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    }
18698d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich
1870d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    debuggerd_init();
18711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
187218a206c81d9743481e364384affd43306911283dElliott Hughes    // Get a few environment variables.
187361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    const char* LD_DEBUG = linker_env_get("LD_DEBUG");
187461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    if (LD_DEBUG != NULL) {
18751728b2396591853345507a063ed6075dfd251706Elliott Hughes      g_ld_debug_verbosity = atoi(LD_DEBUG);
187618a206c81d9743481e364384affd43306911283dElliott Hughes    }
1877be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
187818a206c81d9743481e364384affd43306911283dElliott Hughes    // Normally, these are cleaned by linker_env_init, but the test
187918a206c81d9743481e364384affd43306911283dElliott Hughes    // doesn't cost us anything.
188018a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpath_env = NULL;
188118a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpreload_env = NULL;
188218a206c81d9743481e364384affd43306911283dElliott Hughes    if (!get_AT_SECURE()) {
188318a206c81d9743481e364384affd43306911283dElliott Hughes      ldpath_env = linker_env_get("LD_LIBRARY_PATH");
188418a206c81d9743481e364384affd43306911283dElliott Hughes      ldpreload_env = linker_env_get("LD_PRELOAD");
18851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1887d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov    // Linker does not call constructors for its own
1888d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov    // global variables so we need to initialize
1889d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov    // the allocator explicitly.
18901728b2396591853345507a063ed6075dfd251706Elliott Hughes    g_soinfo_allocator.init();
1891d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov
1892ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    INFO("[ android linker & debugger ]");
18931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
189442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    soinfo* si = soinfo_alloc(args.argv[0]);
189518a206c81d9743481e364384affd43306911283dElliott Hughes    if (si == NULL) {
189618a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
18971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1899d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    /* bootstrap the link map, the main exe always needs to be first */
19001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_EXE;
19013a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(si->link_map_head);
19021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = 0;
190442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    map->l_name = args.argv[0];
19051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_prev = NULL;
19061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_next = NULL;
19071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_map = map;
19091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
19101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1911d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    /* gdb expects the linker to be in the debug shared object list.
1912d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * Without this, gdb has trouble locating the linker's ".text"
1913d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * and ".plt" sections. Gdb could also potentially use this to
1914d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1915d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * Don't use soinfo_alloc(), because the linker shouldn't
1916d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * be on the soinfo list.
191706f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     */
1918d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    {
1919d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        static soinfo linker_soinfo;
19204eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__LP64__)
19211a57f9f75c5752d990a2749b000ec4ceace94b54Pavel Chupin        strlcpy(linker_soinfo.name, "/system/bin/linker64", sizeof(linker_soinfo.name));
19221a57f9f75c5752d990a2749b000ec4ceace94b54Pavel Chupin#else
1923d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
19241a57f9f75c5752d990a2749b000ec4ceace94b54Pavel Chupin#endif
1925d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        linker_soinfo.flags = 0;
1926d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        linker_soinfo.base = linker_base;
1927d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom
1928d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        /*
1929d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         * Set the dynamic field in the link map otherwise gdb will complain with
1930d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         * the following:
1931d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         *   warning: .dynamic section for "/system/bin/linker" is not at the
1932d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         *   expected address (wrong library or version mismatch?)
1933d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         */
19340266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
1935faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
1936d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1937d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                                       &linker_soinfo.dynamic, NULL, NULL);
1938d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        insert_soinfo_into_debug_map(&linker_soinfo);
1939d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
19401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
194142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // Extract information passed from the kernel.
19420266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
194342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->phnum = args.getauxval(AT_PHNUM);
194442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->entry = args.getauxval(AT_ENTRY);
19451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19468180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    /* Compute the value of si->base. We can't rely on the fact that
19478180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * the first entry is the PHDR because this will not be true
19488180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * for certain executables (e.g. some in the NDK unit test suite)
19498180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     */
19508180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    si->base = 0;
1951b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    si->size = phdr_table_get_load_size(si->phdr, si->phnum);
1952bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    si->load_bias = 0;
1953ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    for (size_t i = 0; i < si->phnum; ++i) {
195442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      if (si->phdr[i].p_type == PT_PHDR) {
19550266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
19560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
195742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes        break;
195842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      }
19598180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    }
1960d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    si->dynamic = NULL;
1961ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 1;
19621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19632aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich    ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
19642aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich    if (elf_hdr->e_type != ET_DYN) {
19652aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich        __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
19662aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich        exit(EXIT_FAILURE);
19672aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich    }
19682aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich
19694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
19704688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_LIBRARY_PATH(ldpath_env);
19714688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_PRELOAD(ldpreload_env);
19724fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
19735ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    somain = si;
19745ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
1975183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    if (!soinfo_link_image(si, NULL)) {
1976650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes        __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
197718a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
19781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
19791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1980c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov    add_vdso(args);
1981c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
1982d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallPreInitConstructors();
19839181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
19841728b2396591853345507a063ed6075dfd251706Elliott Hughes    for (size_t i = 0; g_ld_preloads[i] != NULL; ++i) {
19851728b2396591853345507a063ed6075dfd251706Elliott Hughes        g_ld_preloads[i]->CallConstructors();
1986326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng    }
1987326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng
1988d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    /* After the link_image, the si->load_bias is initialized.
1989d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1990d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * We need to update this value for so exe here. So Unwind_Backtrace
1991d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * for some arch like x86 could work correctly within so exe.
19929c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     */
1993c5db969aa4a24a76fc0964f2ff89533e832b47a6Chao-Ying Fu    map->l_addr = si->load_bias;
1994d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
1995e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
19961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING
1997faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    gettimeofday(&t1, NULL);
1998ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
19991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2000faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes               (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
20011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
20021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
2003ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2004bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocAbsolute],
2005bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocRelative],
2006bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocCopy],
2007bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocSymbol]);
20081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
20091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
20101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
20111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned n;
20121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned i;
20131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned count = 0;
2014d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        for (n = 0; n < 4096; n++) {
2015d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (bitmask[n]) {
20161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                unsigned x = bitmask[n];
2017e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__LP64__)
2018e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland                for (i = 0; i < 32; i++) {
2019e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#else
2020d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                for (i = 0; i < 8; i++) {
2021e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#endif
2022d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    if (x & 1) {
2023d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                        count++;
2024d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    }
20251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    x >>= 1;
20261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                }
20271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
20281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
2029ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
20301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
20311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
20321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
20331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES
20341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    fflush(stdout);
20351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
20361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2037c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
20381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si->entry;
20391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2040468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2041bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only
2042bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library
2043bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself.
2044bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *
2045bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input:
2046bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    elf    -> address of ELF header, assumed to be at the start of the file.
2047bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return:
2048bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    load bias, i.e. add the value of any p_vaddr in the file to get
2049bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    the corresponding address in memory.
2050bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */
20510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
20520266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) offset = elf->e_phoff;
2053faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes  const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
20540266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
2055fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
20560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
2057fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (phdr->p_type == PT_LOAD) {
20580266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
2059bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    }
2060fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
2061fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return 0;
2062bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner}
2063bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
2064468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
2065468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This
2066468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and
2067468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation().
2068468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich *
2069468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own
2070468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern
2071468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault.
2072468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
20730266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesextern "C" ElfW(Addr) __linker_init(void* raw_args) {
207442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  KernelArgumentBlock args(raw_args);
207542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
20760266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
20770266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
2078faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes  ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
207942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
208042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  soinfo linker_so;
208142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  memset(&linker_so, 0, sizeof(soinfo));
208242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
2083b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes  strcpy(linker_so.name, "[dynamic linker]");
208442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.base = linker_addr;
208542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
208642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
2087d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom  linker_so.dynamic = NULL;
208842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phdr = phdr;
208942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phnum = elf_hdr->e_phnum;
209042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.flags |= FLAG_LINKER;
209142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
2092183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)  if (!soinfo_link_image(&linker_so, NULL)) {
209342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // It would be nice to print an error message, but if the linker
209442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // can't link itself, there's no guarantee that we'll be able to
2095b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    // call write() (because it involves a GOT reference). We may as
2096b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    // well try though...
2097b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    const char* msg = "CANNOT LINK EXECUTABLE: ";
2098b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, msg, strlen(msg));
2099b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2100b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, "\n", 1);
2101b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    _exit(EXIT_FAILURE);
210242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  }
2103468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
210442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // We have successfully fixed our own relocations. It's safe to run
210542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // the main part of the linker now.
21061728b2396591853345507a063ed6075dfd251706Elliott Hughes  args.abort_message_ptr = &g_abort_message;
21070266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
21085419b9474753d25dff947c7740532f86d130c0beElliott Hughes
21091728b2396591853345507a063ed6075dfd251706Elliott Hughes  g_soinfo_allocator.protect_all(PROT_READ);
2110d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
211142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // Return the address that the calling assembly stub should jump to.
211242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  return start_address;
2113468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
2114