linker.cpp revision 29bbc9dd4c606de9187e46d8899a2a744715c967
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>
374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <sys/mman.h>
384688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <unistd.h>
391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
400d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#include <new>
410d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov
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"
4604dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov#include "private/ScopedFd.h"
471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker.h"
491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker_debug.h"
50be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner#include "linker_environ.h"
5123363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner#include "linker_phdr.h"
52d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov#include "linker_allocator.h"
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Do NOT use malloc() and friends or pthread_*() code here.
571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Don't use printf() either; it's caused mysterious memory
581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * corruption in the past.
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The linker runs before we bring up libc and it's easiest
601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to make sure it does not depend on any complex libc features
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * open issues / todo:
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - cleaner error reporting
651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - after linking, set as much stuff as possible to READONLY
661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   and NOEXEC
674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes */
681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
69489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#if defined(__LP64__)
70489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#define SEARCH_NAME(x) x
71489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#else
72489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov// Nvidia drivers are relying on the bug:
73489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov// http://code.google.com/p/android/issues/detail?id=6670
74489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov// so we continue to use base-name lookup for lp32
75489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanovstatic const char* get_base_name(const char* name) {
76489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  const char* bname = strrchr(name, '/');
77489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  return bname ? bname + 1 : name;
78489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov}
79489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#define SEARCH_NAME(x) get_base_name(x)
80489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#endif
81489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov
820266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
841728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic LinkerAllocator<soinfo> g_soinfo_allocator;
85d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
86ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
87d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo* solist;
88d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo* sonext;
89d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* somain; /* main process, always the one after libdl_info */
901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
911728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic const char* const kDefaultLdPaths[] = {
924eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__LP64__)
93011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes  "/vendor/lib64",
94011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes  "/system/lib64",
95011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes#else
96124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  "/vendor/lib",
97124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  "/system/lib",
98011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes#endif
99851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  nullptr
100124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes};
101124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes
102a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPATH_BUFSIZE (LDPATH_MAX*64)
103a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPATH_MAX 8
104a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes
105a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64)
106a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPRELOAD_MAX 8
107a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes
1081728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic char g_ld_library_paths_buffer[LDPATH_BUFSIZE];
1091728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic const char* g_ld_library_paths[LDPATH_MAX + 1];
110bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
1111728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic char g_ld_preloads_buffer[LDPRELOAD_BUFSIZE];
1121728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic const char* g_ld_preload_names[LDPRELOAD_MAX + 1];
1134fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1141728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic soinfo* g_ld_preloads[LDPRELOAD_MAX + 1];
1154fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1161728b2396591853345507a063ed6075dfd251706Elliott Hughes__LIBC_HIDDEN__ int g_ld_debug_verbosity;
1171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
118851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
1190d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes
120bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesenum RelocationKind {
121bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocAbsolute = 0,
122bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocRelative,
123bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocCopy,
124bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocSymbol,
125bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocMax
126bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
127be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
1281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
129bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstruct linker_stats_t {
130bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    int count[kRelocMax];
131bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
132bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
133bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic linker_stats_t linker_stats;
134bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
135bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind kind) {
136bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    ++linker_stats.count[kind];
137bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
138bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
139bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind) {
140bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
1411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
144bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic unsigned bitmask[4096];
145e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__LP64__)
146e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#define MARK(offset) \
147e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    do { \
148e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        if ((((offset) >> 12) >> 5) < 4096) \
149e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            bitmask[((offset) >> 12) >> 5] |= (1 << (((offset) >> 12) & 31)); \
150faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    } while (0)
151e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#else
152bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(offset) \
153bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    do { \
154bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
155faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    } while (0)
156e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#endif
157bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
158bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(x) do {} while (0)
1591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// You shouldn't try to call memory-allocating functions in the dynamic linker.
1624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Guard against the most obvious ones.
1638f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#define DISALLOW_ALLOCATION(return_type, name, ...) \
1648f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    return_type name __VA_ARGS__ \
1658f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    { \
16612bf3bcdeeaa0dcdc7a4f4e4d11bc2fc3bf6bd7aDmitriy Ivanov        __libc_fatal("ERROR: " #name " called from the dynamic linker!\n"); \
1674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
168812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, malloc, (size_t u __unused));
169812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void, free, (void* u __unused));
170812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
171812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
1722e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1730353195f344666256dba474a15c9ba22cf0cccc9Dima Zavinstatic char tmp_err_buf[768];
1742e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavinstatic char __linker_dl_err_buf[768];
1752e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
176650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hugheschar* linker_get_error_buffer() {
1775419b9474753d25dff947c7740532f86d130c0beElliott Hughes  return &__linker_dl_err_buf[0];
1782e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin}
1792e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
180650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughessize_t linker_get_error_buffer_size() {
181650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  return sizeof(__linker_dl_err_buf);
182650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes}
183650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
1841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
1851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This function is an empty stub where GDB locates a breakpoint to get notified
1861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about linker activity.
1871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1885419b9474753d25dff947c7740532f86d130c0beElliott Hughesextern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
1891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1901728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
191851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanovstatic r_debug _r_debug = {1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
1923a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic link_map* r_debug_tail = 0;
1931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1943a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic void insert_soinfo_into_debug_map(soinfo* info) {
195bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    // Copy the necessary fields into the debug structure.
1963a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(info->link_map_head);
197ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov    map->l_addr = info->load_bias;
198faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    map->l_name = reinterpret_cast<char*>(info->name);
1993a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    map->l_ld = info->dynamic;
2001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Stick the new library at the end of the list.
2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * gdb tends to care more about libc than it does
2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * about leaf libraries, and ordering it this way
2041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * reduces the back-and-forth over the wire.
2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
2061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (r_debug_tail) {
2071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        r_debug_tail->l_next = map;
2081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = r_debug_tail;
2091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    } else {
2111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        _r_debug.r_map = map;
2121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = 0;
2131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
2161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
218bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void remove_soinfo_from_debug_map(soinfo* info) {
2193a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(info->link_map_head);
2205e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
221bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (r_debug_tail == map) {
2225e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        r_debug_tail = map->l_prev;
223bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2245e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
225bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_prev) {
226bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_prev->l_next = map->l_next;
227bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
228bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_next) {
229bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_next->l_prev = map->l_prev;
230bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2315e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2325e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
233bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_load(soinfo* info) {
2341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (info->flags & FLAG_EXE) {
2351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        // GDB already knows about the main executable
2361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
2371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2391728b2396591853345507a063ed6075dfd251706Elliott Hughes    ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
2401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2413a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_ADD;
2421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(info);
2451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2463a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_CONSISTENT;
2471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2485e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2495e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
250bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_unload(soinfo* info) {
2515e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    if (info->flags & FLAG_EXE) {
2525e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        // GDB already knows about the main executable
2535e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        return;
2545e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    }
2555e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2561728b2396591853345507a063ed6075dfd251706Elliott Hughes    ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
2575e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2583a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_DELETE;
2595e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2605e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2615e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    remove_soinfo_from_debug_map(info);
2625e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2633a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_CONSISTENT;
2645e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
26718a206c81d9743481e364384affd43306911283dElliott Hughesvoid notify_gdb_of_libraries() {
2683a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  _r_debug.r_state = r_debug::RT_ADD;
2693a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  rtld_db_dlactivity();
2703a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  _r_debug.r_state = r_debug::RT_CONSISTENT;
2713a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  rtld_db_dlactivity();
2721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
274d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy IvanovLinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
275d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  return g_soinfo_links_allocator.alloc();
276d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
277d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
278d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
279d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  g_soinfo_links_allocator.free(entry);
280d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
281d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
282d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic void protect_data(int protection) {
283d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  g_soinfo_allocator.protect_all(protection);
284d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  g_soinfo_links_allocator.protect_all(protection);
285d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
286d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
287d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo* soinfo_alloc(const char* name, struct stat* file_stat) {
288ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (strlen(name) >= SOINFO_NAME_LEN) {
289ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    DL_ERR("library name \"%s\" too long", name);
290851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    return nullptr;
291ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
292ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
2930d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat);
294d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
295ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext->next = si;
296ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext = si;
2971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
298ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("name %s: allocated soinfo @ %p", name, si);
299ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  return si;
3001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
302faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesstatic void soinfo_free(soinfo* si) {
303851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    if (si == nullptr) {
3044688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return;
3054688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
3064688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
307d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    if (si->base != 0 && si->size != 0) {
308d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      munmap(reinterpret_cast<void*>(si->base), si->size);
309d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    }
310d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
311851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    soinfo *prev = nullptr, *trav;
3121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
313ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("name %s: freeing soinfo @ %p", si->name, si);
3141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
315851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    for (trav = solist; trav != nullptr; trav = trav->next) {
3161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (trav == si)
3171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
3181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        prev = trav;
3191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
320851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    if (trav == nullptr) {
321d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        /* si was not in solist */
3224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("name \"%s\" is not in solist!", si->name);
3231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
3241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
326d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // clear links to/from si
327d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    si->remove_all_links();
328d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
329851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    /* prev will never be null, because the first entry in solist is
3301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       always the static libdl_info.
3311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    */
3321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    prev->next = si->next;
333d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si == sonext) {
334d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        sonext = prev;
335d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
336d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov
3371728b2396591853345507a063ed6075dfd251706Elliott Hughes    g_soinfo_allocator.free(si);
3381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
340cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
341cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_path(const char* path, const char* delimiters,
342cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes                       const char** array, char* buf, size_t buf_size, size_t max_count) {
343851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (path == nullptr) {
344cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return;
345cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
346cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
347cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t len = strlcpy(buf, path, buf_size);
348cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
349cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t i = 0;
350cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  char* buf_p = buf;
351cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
352cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (*array[i] != '\0') {
353cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      ++i;
354cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    }
355cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
356cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
357cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // Forget the last path if we had to truncate; this occurs if the 2nd to
358cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // last char isn't '\0' (i.e. wasn't originally a delimiter).
359cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
360851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    array[i - 1] = nullptr;
361cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  } else {
362851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    array[i] = nullptr;
363cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
364cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
365cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
366cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_LIBRARY_PATH(const char* path) {
3671728b2396591853345507a063ed6075dfd251706Elliott Hughes  parse_path(path, ":", g_ld_library_paths,
3681728b2396591853345507a063ed6075dfd251706Elliott Hughes             g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
369cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
370cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
371cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_PRELOAD(const char* path) {
372cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // We have historically supported ':' as well as ' ' in LD_PRELOAD.
3731728b2396591853345507a063ed6075dfd251706Elliott Hughes  parse_path(path, " :", g_ld_preload_names,
3741728b2396591853345507a063ed6075dfd251706Elliott Hughes             g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
375cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
376cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
3774eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
3784688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* For a given PC, find the .so that it belongs to.
3801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the base address of the .ARM.exidx section
3811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for that .so, and the number of 8-byte entries
3821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in that section (via *pcount).
3831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Intended to be called by libc's __gnu_Unwind_Find_exidx().
3851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3863b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes * This function is exposed via dlfcn.cpp and libdl.so.
3871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
388faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
3891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned addr = (unsigned)pc;
3901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
391faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    for (soinfo* si = solist; si != 0; si = si->next) {
392468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if ((addr >= si->base) && (addr < (si->base + si->size))) {
393468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            *pcount = si->ARM_exidx_count;
394f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            return (_Unwind_Ptr)si->ARM_exidx;
3951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
3961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
397faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    *pcount = 0;
398851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    return nullptr;
3991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
40124053a461e7a20f34002262c1bb122023134989dChristopher Ferris#endif
4024688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
4031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Here, we only have to provide a callback to iterate across all the
4041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * loaded libraries. gcc_eh does the rest. */
405faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesint dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
4061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int rv = 0;
407851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    for (soinfo* si = solist; si != nullptr; si = si->next) {
408bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        dl_phdr_info dl_info;
4093a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes        dl_info.dlpi_addr = si->link_map_head.l_addr;
4103a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes        dl_info.dlpi_name = si->link_map_head.l_name;
4111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phdr = si->phdr;
4121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phnum = si->phnum;
413bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        rv = cb(&dl_info, sizeof(dl_phdr_info), data);
414bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        if (rv != 0) {
4151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
416bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        }
4171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return rv;
4191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4204688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
42102aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanovstatic ElfW(Sym)* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
4220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* symtab = si->symtab;
4230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  const char* strtab = si->strtab;
4241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4250266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd",
4260266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes             name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
4271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
4290266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s = symtab + n;
4300266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    if (strcmp(strtab + s->st_name, name)) continue;
4311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
43202aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov    /* only concern ourselves with global and weak symbol definitions */
4330266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    switch (ELF_ST_BIND(s->st_info)) {
4340266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      case STB_GLOBAL:
4350266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      case STB_WEAK:
4360266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if (s->st_shndx == SHN_UNDEF) {
437d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov          continue;
438d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        }
4391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
440d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
4410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                 name, si->name, reinterpret_cast<void*>(s->st_value),
4420266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                 static_cast<size_t>(s->st_size));
443d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        return s;
444d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov      case STB_LOCAL:
44502aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov        continue;
446d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov      default:
44712bf3bcdeeaa0dcdc7a4f4e4d11bc2fc3bf6bd7aDmitriy Ivanov        __libc_fatal("ERROR: Unexpected ST_BIND value: %d for '%s' in '%s'",
44812bf3bcdeeaa0dcdc7a4f4e4d11bc2fc3bf6bd7aDmitriy Ivanov            ELF_ST_BIND(s->st_info), name, si->name);
4491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4500266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  }
4511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
452aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov  TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
453aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov             name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
454aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov
455aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov
456851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  return nullptr;
4571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4590d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanovsoinfo::soinfo(const char* name, const struct stat* file_stat) {
4600d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  memset(this, 0, sizeof(*this));
4610d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov
4620d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  strlcpy(this->name, name, sizeof(this->name));
4630d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  flags = FLAG_NEW_SOINFO;
4640d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  version = SOINFO_VERSION;
4650d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov
466851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (file_stat != nullptr) {
4670d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov    set_st_dev(file_stat->st_dev);
4680d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov    set_st_ino(file_stat->st_ino);
4690d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  }
4700d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov}
4710d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov
4729598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanovvoid soinfo::resolve_ifunc_symbols() {
4739598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov  if (!get_has_ifuncs()) {
4749598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov    return;
4759598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov  }
476c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
4779598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov  phdr_table_unprotect_segments(phdr, phnum, load_bias);
478c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
479c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith  TRACE_TYPE(IFUNC, "CHECKING FOR IFUNCS AND PERFORMING SYMBOL UPDATES");
480c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
4819598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov  for (size_t i = 0; i < nchain; ++i) {
4829598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov    ElfW(Sym)* s = &symtab[i];
483c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
484c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      // The address of the ifunc in the symbol table is the address of the
485c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      // function that chooses the function to which the ifunc will refer.
486c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      // In order to return the proper value, we run the choosing function
487c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      // in the linker and then return its result (minus the base offset).
488c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      TRACE_TYPE(IFUNC, "FOUND IFUNC");
489c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      ElfW(Addr) (*ifunc_ptr)();
4909598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov      ifunc_ptr = reinterpret_cast<ElfW(Addr)(*)()>(s->st_value + base);
4919598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov      s->st_value = (ifunc_ptr() - base);
492c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      TRACE_TYPE(IFUNC, "NEW VALUE IS %p", (void*)s->st_value);
493c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    }
494c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith  }
4959598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov  phdr_table_protect_segments(phdr, phnum, load_bias);
496c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith}
497c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
498d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic unsigned elfhash(const char* _name) {
499faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    const unsigned char* name = reinterpret_cast<const unsigned char*>(_name);
5001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned h = 0, g;
5011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
502faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    while (*name) {
5031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h = (h << 4) + *name++;
5041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        g = h & 0xf0000000;
5051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g;
5061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g >> 24;
5071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
5081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return h;
5091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
51129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanovstatic ElfW(Sym)* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi) {
512943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
513851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    ElfW(Sym)* s = nullptr;
5146ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
515851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    if (si != nullptr && somain != nullptr) {
5165ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        /*
517c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * Local scope is executable scope. Just start looking into it right away
518c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * for the shortcut.
5195ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel         */
5205ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
521c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        if (si == somain) {
52202aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov            s = soinfo_elf_lookup(si, elf_hash, name);
523851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov            if (s != nullptr) {
524c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
5255ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                goto done;
5265ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
527c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov
528c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov            /* Next, look for it in the preloads list */
529c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov            for (int i = 0; g_ld_preloads[i] != NULL; i++) {
530c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                s = soinfo_elf_lookup(g_ld_preloads[i], elf_hash, name);
531c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                if (s != NULL) {
532c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                    *lsi = g_ld_preloads[i];
533c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                    goto done;
534c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                }
535c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov            }
536c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        } else {
537c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
538c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
539c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
540c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with symbolic relocations disabled, the
541c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * first place to look to resolve external references is the main
542c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * executable.
543c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
544c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
545c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (!si->has_DT_SYMBOLIC) {
546ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s",
54761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
54802aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov                s = soinfo_elf_lookup(somain, elf_hash, name);
549851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov                if (s != nullptr) {
550c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
551c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
552c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
553c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov
554c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                /* Next, look for it in the preloads list */
555c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                for (int i = 0; g_ld_preloads[i] != NULL; i++) {
556c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                    s = soinfo_elf_lookup(g_ld_preloads[i], elf_hash, name);
557c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                    if (s != NULL) {
558c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                        *lsi = g_ld_preloads[i];
559c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                        goto done;
560c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                    }
561c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                }
562c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5635ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
564c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Look for symbols in the local scope (the object who is
5654eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes             * searching). This happens with C++ templates on x86 for some
566c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * reason.
567c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             *
568c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Notes on weak symbols:
569c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * The ELF specs are ambiguous about treatment of weak definitions in
570c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * dynamic linking.  Some systems return the first definition found
571c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * and some the first non-weak definition.   This is system dependent.
572c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Here we return the first definition found for simplicity.  */
573c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
57402aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov            s = soinfo_elf_lookup(si, elf_hash, name);
575851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov            if (s != nullptr) {
576c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
577c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                goto done;
578c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
579c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
580c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
581c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with -Bsymbolic and symbol is not found
582c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * in the local scope, try to find the symbol in the main executable.
583c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
584c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
585c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (si->has_DT_SYMBOLIC) {
586ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s after local scope",
58761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
58802aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov                s = soinfo_elf_lookup(somain, elf_hash, name);
589851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov                if (s != nullptr) {
590c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
591c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
592c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
5936ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
594c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                /* Next, look for it in the preloads list */
595c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                for (int i = 0; g_ld_preloads[i] != NULL; i++) {
596c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                    s = soinfo_elf_lookup(g_ld_preloads[i], elf_hash, name);
597c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                    if (s != NULL) {
598c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                        *lsi = g_ld_preloads[i];
599c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                        goto done;
600c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                    }
601c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov                }
602c2048944ff8d9f2993264b45dfabf18d9036e379Dmitriy Ivanov            }
6035ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
6044fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
6054fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
60629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    si->get_children().visit([&](soinfo* child) {
60729bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        DEBUG("%s: looking up %s in %s", si->name, name, child->name);
60829bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        s = soinfo_elf_lookup(child, elf_hash, name);
609851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov        if (s != nullptr) {
61029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            *lsi = child;
61129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            return false;
6125ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
61329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        return true;
61429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    });
6156ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
6166ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevdone:
617851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    if (s != nullptr) {
618c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
619c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   "found in %s, base = %p, load bias = %p",
620c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   si->name, name, reinterpret_cast<void*>(s->st_value),
621c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   (*lsi)->name, reinterpret_cast<void*>((*lsi)->base),
622c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   reinterpret_cast<void*>((*lsi)->load_bias));
6236ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        return s;
6246ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
6256ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
626851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    return nullptr;
6276ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev}
6286ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
6290cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov// Each size has it's own allocator.
6300cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanovtemplate<size_t size>
6310cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanovclass SizeBasedAllocator {
6320cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov public:
6330cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov  static void* alloc() {
6340cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov    return allocator_.alloc();
6350cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov  }
6360cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov
6370cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov  static void free(void* ptr) {
6380cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov    allocator_.free(ptr);
6390cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov  }
6404bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov
6410cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov private:
6420cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov  static LinkerBlockAllocator allocator_;
6430cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov};
6440cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov
6450cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanovtemplate<size_t size>
6460cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy IvanovLinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
6470cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov
6480cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanovtemplate<typename T>
6490cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanovclass TypeBasedAllocator {
6500cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov public:
6510cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov  static T* alloc() {
6520cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov    return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
6530cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov  }
6540cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov
6550cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov  static void free(T* ptr) {
6560cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov    SizeBasedAllocator<sizeof(T)>::free(ptr);
6570cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov  }
6580cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov};
6590cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov
6600cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanovtemplate <typename T>
6610cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanovusing linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
6620cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov
6630cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanovtypedef linked_list_t<soinfo> SoinfoLinkedList;
6644bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov
665aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanovstatic LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_list_allocator_rw;
666aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanovclass SoinfoListAllocatorRW {
667aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov public:
668aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov  static LinkedListEntry<soinfo>* alloc() {
669aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov    return g_soinfo_list_allocator_rw.alloc();
670aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov  }
671d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom
672aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov  static void free(LinkedListEntry<soinfo>* ptr) {
673aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov    g_soinfo_list_allocator_rw.free(ptr);
674aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov  }
675aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov};
676aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov
677aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov// This is used by dlsym(3).  It performs symbol lookup only within the
678aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov// specified soinfo object and its dependencies in breadth first order.
67902aa70589d22fa9b65da43de705d6de2715870c6Dmitriy IvanovElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
6800cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov  SoinfoLinkedList visit_list;
6810cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov  SoinfoLinkedList visited;
6820cd83ebb0e9784827d9ec0a8028a710e73a28b2bDmitriy Ivanov
683aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov  visit_list.push_back(si);
684aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov  soinfo* current_soinfo;
685aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov  while ((current_soinfo = visit_list.pop_front()) != nullptr) {
686042426ba6375f5c145379e598486ec6d675533c9Dmitriy Ivanov    if (visited.contains(current_soinfo)) {
687042426ba6375f5c145379e598486ec6d675533c9Dmitriy Ivanov      continue;
688042426ba6375f5c145379e598486ec6d675533c9Dmitriy Ivanov    }
689042426ba6375f5c145379e598486ec6d675533c9Dmitriy Ivanov
69002aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov    ElfW(Sym)* result = soinfo_elf_lookup(current_soinfo, elfhash(name), name);
691aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov
692aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov    if (result != nullptr) {
693aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov      *found = current_soinfo;
694aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov      return result;
695aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov    }
696042426ba6375f5c145379e598486ec6d675533c9Dmitriy Ivanov    visited.push_back(current_soinfo);
697aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov
698aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov    current_soinfo->get_children().for_each([&](soinfo* child) {
699aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov      visit_list.push_back(child);
700aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov    });
701aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov  }
702aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov
703aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov  return nullptr;
7041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
706d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3) to performs a global symbol lookup. If the
707d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   start value is null (for RTLD_DEFAULT), the search starts at the
708d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   beginning of the global solist. Otherwise the search starts at the
709d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   specified soinfo (for RTLD_NEXT).
7106ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
71102aa70589d22fa9b65da43de705d6de2715870c6Dmitriy IvanovElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
712cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  unsigned elf_hash = elfhash(name);
7131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
714851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (start == nullptr) {
715cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    start = solist;
716cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
7171698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer
718851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  ElfW(Sym)* s = nullptr;
719851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  for (soinfo* si = start; (s == nullptr) && (si != nullptr); si = si->next) {
72002aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov    s = soinfo_elf_lookup(si, elf_hash, name);
721851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    if (s != nullptr) {
722cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      *found = si;
723cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      break;
7241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
725cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
7261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
727851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (s != nullptr) {
728c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
729c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes               name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
730cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
7311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
732cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  return s;
7331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
735fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Chengsoinfo* find_containing_library(const void* p) {
7360266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
737851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  for (soinfo* si = solist; si != nullptr; si = si->next) {
738fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (address >= si->base && address - si->base < si->size) {
739fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return si;
740e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
741fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
742851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  return nullptr;
743e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
744e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
7450266ae5f884d72da58f33a072e865ba131234a5eElliott HughesElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr) {
7460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - si->base;
747fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
748fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // Search the library's symbol table for any defined symbol which
749fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // contains this address.
750fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (size_t i = 0; i < si->nchain; ++i) {
7510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* sym = &si->symtab[i];
752fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (sym->st_shndx != SHN_UNDEF &&
753fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr >= sym->st_value &&
754fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr < sym->st_value + sym->st_size) {
755fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return sym;
756e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
757fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
758e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
759851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  return nullptr;
760e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
761e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
762124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library_on_path(const char* name, const char* const paths[]) {
763124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  char buf[512];
764851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  for (size_t i = 0; paths[i] != nullptr; ++i) {
7651e980b6bc8315d00a07312b25486531247abd98cElliott Hughes    int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
766124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
767ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes      PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
768124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      continue;
7691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
770124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
771124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (fd != -1) {
772124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      return fd;
773124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
774124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
775124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return -1;
7761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
778124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library(const char* name) {
779ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ opening %s ]", name);
7801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
781124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // If the name contains a slash, we should attempt to open it directly and not search the paths.
782851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (strchr(name, '/') != nullptr) {
7836971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
7846971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    if (fd != -1) {
7856971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes      return fd;
7866971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    }
7876971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
7885ca7ed9005ea16733d7c87d7154473b7a500be0cDmitriy Ivanov#if defined(__LP64__)
789e43c4a7a665032a29cb5ec15d4adbf81ea199220Dmitriy Ivanov    return -1;
7905ca7ed9005ea16733d7c87d7154473b7a500be0cDmitriy Ivanov#endif
791124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
7921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
793124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
7941728b2396591853345507a063ed6075dfd251706Elliott Hughes  int fd = open_library_on_path(name, g_ld_library_paths);
795124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (fd == -1) {
7961728b2396591853345507a063ed6075dfd251706Elliott Hughes    fd = open_library_on_path(name, kDefaultLdPaths);
797124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
798124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return fd;
7991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
801b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic soinfo* load_library(const char* name, int dlflags, const android_dlextinfo* extinfo) {
80204dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov    int fd = -1;
80304dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov    ScopedFd file_guard(-1);
80404dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov
805851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
80604dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov      fd = extinfo->library_fd;
80704dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov    } else {
80804dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov      // Open the file.
80904dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov      fd = open_library(name);
81004dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov      if (fd == -1) {
8114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("library \"%s\" not found", name);
812851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov        return nullptr;
81304dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov      }
81404dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov
81504dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov      file_guard.reset(fd);
8162e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    }
8171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
818650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    ElfReader elf_reader(name, fd);
819d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
820d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    struct stat file_stat;
821d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
822d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      DL_ERR("unable to stat file for the library %s: %s", name, strerror(errno));
823851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov      return nullptr;
824d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    }
825d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
826d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // Check for symlink and other situations where
827d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // file can have different names.
828851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    for (soinfo* si = solist; si != nullptr; si = si->next) {
829d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      if (si->get_st_dev() != 0 &&
830d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          si->get_st_ino() != 0 &&
831d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          si->get_st_dev() == file_stat.st_dev &&
832d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          si->get_st_ino() == file_stat.st_ino) {
833d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
834d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        return si;
835d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      }
836d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    }
837d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
838b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    if ((dlflags & RTLD_NOLOAD) != 0) {
839851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov      return nullptr;
840b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    }
841b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov
842d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // Read the ELF header and load the segments.
84312bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)    if (!elf_reader.Load(extinfo)) {
844851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov        return nullptr;
84523363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
84623363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner
847489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov    soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat);
848851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    if (si == nullptr) {
849851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov        return nullptr;
85023363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
851650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->base = elf_reader.load_start();
852650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->size = elf_reader.load_size();
853650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->load_bias = elf_reader.load_bias();
854650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phnum = elf_reader.phdr_count();
855650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phdr = elf_reader.loaded_phdr();
856d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
857d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // At this point we know that whatever is loaded @ base is a valid ELF
858d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // shared library whose segments are properly mapped in.
85904dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov    TRACE("[ load_library base=%p size=%zu name='%s' ]",
860d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          reinterpret_cast<void*>(si->base), si->size, si->name);
861d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
86229bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (!si->LinkImage(extinfo)) {
863d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      soinfo_free(si);
864851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov      return nullptr;
865d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    }
866d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
867650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    return si;
8681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
870489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanovstatic soinfo *find_loaded_library_by_name(const char* name) {
871489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  const char* search_name = SEARCH_NAME(name);
872851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  for (soinfo* si = solist; si != nullptr; si = si->next) {
873489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov    if (!strcmp(search_name, si->name)) {
874489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov      return si;
87512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    }
876489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  }
877851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  return nullptr;
87812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel}
87912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
880b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic soinfo* find_library_internal(const char* name, int dlflags, const android_dlextinfo* extinfo) {
881851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (name == nullptr) {
882d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return somain;
883d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
884d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
885489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  soinfo* si = find_loaded_library_by_name(name);
886b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov
887b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  // Library might still be loaded, the accurate detection
888b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  // of this fact is done by load_library
889851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (si == nullptr) {
890b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    TRACE("[ '%s' has not been found by name.  Trying harder...]", name);
891b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    si = load_library(name, dlflags, extinfo);
892b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  }
893b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov
894851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (si != nullptr && (si->flags & FLAG_LINKED) == 0) {
895b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    DL_ERR("recursive link to \"%s\"", si->name);
896851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    return nullptr;
897d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
89812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
899b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  return si;
900d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
901d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
902b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic soinfo* find_library(const char* name, int dlflags, const android_dlextinfo* extinfo) {
903b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  soinfo* si = find_library_internal(name, dlflags, extinfo);
904851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (si != nullptr) {
905ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count++;
906d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
907d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
908d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
909d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
910b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic void soinfo_unload(soinfo* si) {
911ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  if (si->ref_count == 1) {
912ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("unloading '%s'", si->name);
913d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallDestructors();
914d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
9150d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov    if (si->has_min_version(0)) {
9164bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov      // It is not safe to do si->get_children().for_each, because
9174bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov      // during soinfo_free the child will concurrently modify the si->children
9184bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov      // list, therefore we create a copy and use it to unload children.
9194bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov      size_t children_count = si->get_children().size();
9204bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov      soinfo* children[children_count];
9214bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov      si->get_children().copy_to_array(children, children_count);
9224bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov
9234bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov      for (size_t i = 0; i < children_count; ++i) {
9244bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov        TRACE("%s needs to unload %s", si->name, children[i]->name);
9254bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov        soinfo_unload(children[i]);
9264bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov      }
927d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    } else {
928d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
929d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        if (d->d_tag == DT_NEEDED) {
930d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          const char* library_name = si->strtab + d->d_un.d_val;
931d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          TRACE("%s needs to unload %s", si->name, library_name);
932851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov          soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr);
933851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov          if (needed != nullptr) {
934b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            soinfo_unload(needed);
935b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov          } else {
936b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            // Not found: for example if symlink was deleted between dlopen and dlclose
937b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            // Since we cannot really handle errors at this point - print and continue.
938b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
939b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov          }
940d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        }
941d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
9421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
9431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
944d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    notify_gdb_of_unload(si);
945ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 0;
946d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov    soinfo_free(si);
947d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  } else {
948ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count--;
949c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
950d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
9511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
9521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
953a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughesvoid do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
954052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris  // Use basic string manipulation calls to avoid snprintf.
955052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris  // snprintf indirectly calls pthread_getspecific to get the size of a buffer.
956052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris  // When debug malloc is enabled, this call returns 0. This in turn causes
957052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris  // snprintf to do nothing, which causes libraries to fail to load.
958052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris  // See b/17302493 for further details.
959052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris  // Once the above bug is fixed, this code can be modified to use
960052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris  // snprintf again.
961052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris  size_t required_len = strlen(kDefaultLdPaths[0]) + strlen(kDefaultLdPaths[1]) + 2;
962052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris  if (buffer_size < required_len) {
963052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris    __libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: buffer len %zu, required len %zu",
964052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris                 buffer_size, required_len);
965052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris  }
966052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris  char* end = stpcpy(buffer, kDefaultLdPaths[0]);
967052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris  *end = ':';
968052fa3a34c07b9b361c30565f029dd543da14e30Christopher Ferris  strcpy(end + 1, kDefaultLdPaths[1]);
969a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes}
970a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes
971cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesvoid do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
972cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (!get_AT_SECURE()) {
973cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    parse_LD_LIBRARY_PATH(ld_library_path);
974cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
975cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
976cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
9771a586293400d0e1d73e6eb82f7dfe9d2d9ed3c4bElliott Hughessoinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
978b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NOLOAD)) != 0) {
979e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    DL_ERR("invalid flags to dlopen: %x", flags);
980851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    return nullptr;
981e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes  }
982851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (extinfo != nullptr && ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0)) {
9833a8646fc5d9f1a9304251a1d1c8ff9e523d81b40Dmitriy Ivanov    DL_ERR("invalid extended flags to android_dlopen_ext: %" PRIx64, extinfo->flags);
984851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    return nullptr;
985012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)  }
986d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ | PROT_WRITE);
987b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  soinfo* si = find_library(name, flags, extinfo);
988851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (si != nullptr) {
989d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
990d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
991d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ);
992d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
993d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
9941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
995b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovvoid do_dlclose(soinfo* si) {
996d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ | PROT_WRITE);
997b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  soinfo_unload(si);
998d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ);
9991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
10001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1001c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith// ifuncs are only defined for x86
1002c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#if defined(__i386__)
100329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanovstatic void soinfo_ifunc_relocate(soinfo* si, ElfW(Rel)* rel, unsigned count) {
1004c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith  for (size_t idx = 0; idx < count; ++idx, ++rel) {
1005c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    ElfW(Sym)* s;
1006c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    soinfo* lsi;
1007c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    unsigned type = ELFW(R_TYPE)(rel->r_info);
1008c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    unsigned sym = ELFW(R_SYM)(rel->r_info);
1009c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + si->load_bias);
1010c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    ElfW(Addr) sym_addr = 0;
1011851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    const char* sym_name = nullptr;
1012c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
101329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    s = soinfo_do_lookup(si, sym_name, &lsi);
1014c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
1015c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC && type == R_386_JMP_SLOT) {
1016c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      TRACE("IFUNC RELOCATION, PASS 2: %p",  (void*)(sym_addr));
1017c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      ElfW(Addr) (*ifunc_ptr)();
1018c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      ifunc_ptr = reinterpret_cast<ElfW(Addr)(*)()>(s->st_value + si->base);
1019c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_ptr();
1020c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    }
1021c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith  }
1022c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith}
1023c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#endif
1024c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
1025c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#if defined(__x86_64__)
102629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanovstatic void soinfo_ifunc_relocate(soinfo* si, ElfW(Rela)* rela, unsigned count) {
1027c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith  for (size_t idx = 0; idx < count; ++idx, ++rela) {
1028c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    ElfW(Sym)* s;
1029c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    soinfo* lsi;
1030c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    unsigned type = ELFW(R_TYPE)(rela->r_info);
1031c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    unsigned sym = ELFW(R_SYM)(rela->r_info);
1032c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + si->load_bias);
1033c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    ElfW(Addr) sym_addr = 0;
1034851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    const char* sym_name = nullptr;
1035c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
103629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    s = soinfo_do_lookup(si, sym_name, &lsi);
1037c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
1038c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC && type == R_X86_64_JUMP_SLOT) {
1039c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      TRACE("IFUNC RELOCATION, PASS 2: %p",  (void*)(sym_addr + rela->r_addend));
1040c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      ElfW(Addr) (*ifunc_ptr)();
1041c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      ifunc_ptr = reinterpret_cast<ElfW(Addr)(*)()>(s->st_value + si->base);
1042c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_ptr();
1043c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    }
1044c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith  }
1045c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith}
1046c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#endif
1047c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
10484eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
104929bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanovint soinfo::Relocate(ElfW(Rela)* rela, unsigned count) {
10500266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* s;
1051c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  soinfo* lsi;
1052c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
1053c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  for (size_t idx = 0; idx < count; ++idx, ++rela) {
10540266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    unsigned type = ELFW(R_TYPE)(rela->r_info);
10550266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    unsigned sym = ELFW(R_SYM)(rela->r_info);
105629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + load_bias);
10570266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Addr) sym_addr = 0;
1058851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    const char* sym_name = nullptr;
1059c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
106029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    DEBUG("Processing '%s' relocation at index %zd", name, idx);
1061c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (type == 0) { // R_*_NONE
1062c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      continue;
1063c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1064c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (sym != 0) {
106529bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov      sym_name = reinterpret_cast<const char*>(strtab + symtab[sym].st_name);
106629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov      s = soinfo_do_lookup(this, sym_name, &lsi);
1067851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov      if (s == nullptr) {
1068c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        // We only allow an undefined symbol if this is a weak reference...
106929bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        s = &symtab[sym];
1070c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
107129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov          DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
1072c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          return -1;
1073c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
1074c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
1075c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        /* IHI0044C AAELF 4.5.1.1:
1076c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
1077c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           Libraries are not searched to resolve weak references.
1078c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           It is not an error for a weak reference to remain unsatisfied.
1079c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
1080c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           During linking, the value of an undefined weak reference is:
1081c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - Zero if the relocation type is absolute
1082c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - The address of the place if the relocation is pc-relative
1083c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - The address of nominal base address if the relocation
1084c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes             type is base-relative.
1085c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         */
1086c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
1087c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (type) {
1088e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__)
1089e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_JUMP_SLOT:
1090e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_GLOB_DAT:
1091e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS64:
1092e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS32:
1093e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS16:
1094e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_RELATIVE:
1095e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland          /*
1096e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * The sym_addr was initialized to be zero above, or the relocation
1097e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * code below does not care about value of sym_addr.
1098e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * No need to do anything.
1099e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           */
1100e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland          break;
1101e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__)
1102c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_JUMP_SLOT:
1103c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_GLOB_DAT:
1104c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_32:
1105bb5bb2046fa0759d94995cbfb4f41137cd0b2a91Pavel Chupin        case R_X86_64_64:
1106c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_RELATIVE:
1107c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          // No need to do anything.
1108c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          break;
1109c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_PC32:
1110c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          sym_addr = reloc;
1111c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          break;
11124eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1113c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        default:
1114faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes          DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx);
1115c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          return -1;
1116c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
1117c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      } else {
1118c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        // We got a definition.
11190266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
1120c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      }
1121c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocSymbol);
1122c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    } else {
1123851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov      s = nullptr;
1124c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1125c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
1126c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    switch (type) {
1127e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__)
1128e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_JUMP_SLOT:
1129e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
1130e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
11310266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n",
11320266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
11330266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
1134e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1135e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_GLOB_DAT:
1136e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
1137e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
11380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n",
11390266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
11400266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
1141e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1142e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS64:
1143e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
1144e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
11450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
11460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
11470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
1148e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1149e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS32:
1150e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
1151e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
11520266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
11530266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
11540266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
11550266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
11560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
1157e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
11580266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
11590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
11600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT32_MIN),
11610266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT32_MAX));
1162e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1163e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
1164e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1165e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS16:
1166e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
1167e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
11680266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
11690266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
11700266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
11710266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
11720266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
1173e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
11740266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
11750266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
11760266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT16_MIN),
11770266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT16_MAX));
1178e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1179e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
1180e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1181e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL64:
1182e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
1183e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
11840266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
11850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
11860266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
1187e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1188e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL32:
1189e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
1190e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
11910266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
11920266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
11930266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
11940266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
11950266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
1196e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
11970266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
11980266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
11990266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT32_MIN),
12000266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT32_MAX));
1201e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1202e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
1203e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1204e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL16:
1205e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
1206e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
12070266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
12080266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
12090266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
12100266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
12110266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
1212e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
12130266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
12140266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
12150266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT16_MIN),
12160266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT16_MAX));
1217e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1218e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
1219e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1220e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1221e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_RELATIVE:
1222e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
1223e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
1224e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        if (sym) {
1225e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            DL_ERR("odd RELATIVE form...");
1226e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1227e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
12280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n",
122929bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                   reloc, (base + rela->r_addend));
123029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        *reinterpret_cast<ElfW(Addr)*>(reloc) = (base + rela->r_addend);
1231e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1232e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1233e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_COPY:
123476e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich        /*
123576e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         * ET_EXEC is not supported so this should not happen.
123676e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         *
123776e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
123876e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         *
123976e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         * Section 4.7.1.10 "Dynamic relocations"
124076e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         * R_AARCH64_COPY may only appear in executable objects where e_type is
124176e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         * set to ET_EXEC.
124276e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         */
124329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        DL_ERR("%s R_AARCH64_COPY relocations are not supported", name);
124476e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich        return -1;
1245e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_TLS_TPREL64:
12460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
12470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset);
1248e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1249e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_TLS_DTPREL32:
12500266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
12510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset);
1252e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1253e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__)
1254c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_JUMP_SLOT:
1255c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocAbsolute);
1256c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1257c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1258c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
1259c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
126029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        set_has_ifuncs(true);
1261c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      } else {
1262c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith        *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1263c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith      }
1264c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1265c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_GLOB_DAT:
1266c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocAbsolute);
1267c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1268c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1269c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
12700266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1271c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1272c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_RELATIVE:
1273c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1274c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1275c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      if (sym) {
1276c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DL_ERR("odd RELATIVE form...");
1277c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        return -1;
1278c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      }
1279c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
128029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                 static_cast<size_t>(base));
128129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov      *reinterpret_cast<ElfW(Addr)*>(reloc) = base + rela->r_addend;
1282c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1283c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_32:
1284c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1285c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1286c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1287c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr), sym_name);
12880266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1289c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1290c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin    case R_X86_64_64:
1291c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      count_relocation(kRelocRelative);
1292c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      MARK(rela->r_offset);
1293c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1294c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin                 static_cast<size_t>(sym_addr), sym_name);
12950266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1296c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      break;
1297c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_PC32:
1298c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1299c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1300c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1301c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1302c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
13030266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc;
1304c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
13054eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1306e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1307c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    default:
1308faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes      DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx);
1309c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      return -1;
1310c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1311c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  }
1312c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  return 0;
1313c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes}
13149918665a45095ad135576f005c0e5307feb366a1Chris Dearman
13159918665a45095ad135576f005c0e5307feb366a1Chris Dearman#else // REL, not RELA.
131629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanovint soinfo::Relocate(ElfW(Rel)* rel, unsigned count) {
13170266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s;
1318d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo* lsi;
13191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13204688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    for (size_t idx = 0; idx < count; ++idx, ++rel) {
13210266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        unsigned type = ELFW(R_TYPE)(rel->r_info);
13220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead.
13230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        unsigned sym = ELFW(R_SYM)(rel->r_info);
132429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
13250266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        ElfW(Addr) sym_addr = 0;
1326851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov        const char* sym_name = nullptr;
13271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
132829bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        DEBUG("Processing '%s' relocation at index %zd", name, idx);
1329d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (type == 0) { // R_*_NONE
1330d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            continue;
1331d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1332d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (sym != 0) {
133329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            sym_name = reinterpret_cast<const char*>(strtab + symtab[sym].st_name);
133429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            s = soinfo_do_lookup(this, sym_name, &lsi);
1335851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov            if (s == nullptr) {
13364eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                // We only allow an undefined symbol if this is a weak reference...
133729bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                s = &symtab[sym];
1338c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
133929bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                    DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, name);
1340e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1341e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1342e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1343e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* IHI0044C AAELF 4.5.1.1:
1344e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1345e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   Libraries are not searched to resolve weak references.
1346e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   It is not an error for a weak reference to remain
1347e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   unsatisfied.
1348e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1349e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   During linking, the value of an undefined weak reference is:
1350e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - Zero if the relocation type is absolute
1351e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - The address of the place if the relocation is pc-relative
1352bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes                   - The address of nominal base address if the relocation
1353e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                     type is base-relative.
1354e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                  */
1355e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1356e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                switch (type) {
13574eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
1358e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_JUMP_SLOT:
1359e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_GLOB_DAT:
1360e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_ABS32:
1361e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_RELATIVE:    /* Don't care. */
13624eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // sym_addr was initialized to be zero above or relocation
13634eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // code below does not care about value of sym_addr.
13644eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // No need to do anything.
13654eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    break;
13664eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
1367d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                case R_386_JMP_SLOT:
1368e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_GLOB_DAT:
1369e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_32:
1370c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                case R_386_RELATIVE:    /* Don't care. */
13714eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // sym_addr was initialized to be zero above or relocation
13724eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // code below does not care about value of sym_addr.
13734eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // No need to do anything.
1374e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
1375e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_PC32:
1376e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    sym_addr = reloc;
1377e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
13784eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1379e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
13804eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
1381e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_COPY:
13824eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // Fall through. Can't really copy if weak symbol is not found at run-time.
13834eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1384e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                default:
1385faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                    DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
1386e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1387e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1388e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            } else {
13894eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                // We got a definition.
13900266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
1391d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1392bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocSymbol);
13931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        } else {
1394851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov            s = nullptr;
13951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
13961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1397c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (type) {
13984eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
13991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_JUMP_SLOT:
1400bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
14011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1402ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
14030266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
14041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_GLOB_DAT:
1406bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
14071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1408ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
14090266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
14101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_ABS32:
1412bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
14131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1414ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
14150266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
14161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
141734ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner        case R_ARM_REL32:
1418bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
141934ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            MARK(rel->r_offset);
1420ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
142134ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner                       reloc, sym_addr, rel->r_offset, sym_name);
14220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
142334ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            break;
14241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_COPY:
142576e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich            /*
142676e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             * ET_EXEC is not supported so this should not happen.
142776e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             *
142876e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
142976e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             *
143076e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             * Section 4.7.1.10 "Dynamic relocations"
143176e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             * R_ARM_COPY may only appear in executable objects where e_type is
143276e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             * set to ET_EXEC.
143376e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             */
143429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            DL_ERR("%s R_ARM_COPY relocations are not supported", name);
143576e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich            return -1;
14364eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
14374eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_JMP_SLOT:
14384eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
14394eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
14404eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1441c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith            if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
144229bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov              set_has_ifuncs(true);
1443c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith            } else {
1444c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith              *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
1445c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith            }
14464eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
14474eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_GLOB_DAT:
14484eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
14494eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
14504eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
14510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
14524eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
14534eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_32:
14544eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
14554eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
14564eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
14570266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
14584eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
14594eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_PC32:
14604eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
14614eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
14624eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
14634eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                       reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
14640266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
14654eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
14664eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__mips__)
14674eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_MIPS_REL32:
14689918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__LP64__)
14699918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // MIPS Elf64_Rel entries contain compound relocations
14709918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
14719918665a45095ad135576f005c0e5307feb366a1Chris Dearman            if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
14729918665a45095ad135576f005c0e5307feb366a1Chris Dearman                ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
1473faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
14749918665a45095ad135576f005c0e5307feb366a1Chris Dearman                       type, (unsigned)ELF64_R_TYPE2(rel->r_info),
1475faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                       (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx);
14769918665a45095ad135576f005c0e5307feb366a1Chris Dearman                return -1;
14779918665a45095ad135576f005c0e5307feb366a1Chris Dearman            }
14789918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif
14794eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
14804eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
14819918665a45095ad135576f005c0e5307feb366a1Chris Dearman            TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
14829918665a45095ad135576f005c0e5307feb366a1Chris Dearman                       static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
14834eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            if (s) {
14840266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
14854eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            } else {
148629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
14874eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            }
14884eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
14894eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
14904eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes
14914eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
14924eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_ARM_RELATIVE:
14934eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
14944eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_RELATIVE:
14954eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
14964eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
14974eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
14984eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            if (sym) {
14994eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                DL_ERR("odd RELATIVE form...");
15004eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                return -1;
15014eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            }
15024eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
150329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                       reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(base));
150429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            *reinterpret_cast<ElfW(Addr)*>(reloc) += base;
15054eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
15061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        default:
1508faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
15091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return -1;
15101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
15111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
15121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
15131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
1514c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
15151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15164eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
151729bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanovstatic bool mips_relocate_got(soinfo* si) {
15189918665a45095ad135576f005c0e5307feb366a1Chris Dearman    ElfW(Addr)** got = si->plt_got;
1519851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    if (got == nullptr) {
152087c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom        return true;
152187c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    }
152287c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned local_gotno = si->mips_local_gotno;
152387c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned gotsym = si->mips_gotsym;
152487c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned symtabno = si->mips_symtabno;
15250266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* symtab = si->symtab;
1526d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
15279918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // got[0] is the address of the lazy resolver function.
15289918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // got[1] may be used for a GNU extension.
15299918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
15309918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // FIXME: maybe this should be in a separate routine?
1531d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if ((si->flags & FLAG_LINKER) == 0) {
153287c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom        size_t g = 0;
15339918665a45095ad135576f005c0e5307feb366a1Chris Dearman        got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
15349918665a45095ad135576f005c0e5307feb366a1Chris Dearman        if (reinterpret_cast<intptr_t>(got[g]) < 0) {
15359918665a45095ad135576f005c0e5307feb366a1Chris Dearman            got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
1536d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
15379918665a45095ad135576f005c0e5307feb366a1Chris Dearman        // Relocate the local GOT entries.
1538d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        for (; g < local_gotno; g++) {
15399918665a45095ad135576f005c0e5307feb366a1Chris Dearman            got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + si->load_bias);
1540d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1541d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1542d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
15439918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // Now for the global GOT entries...
15440266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* sym = symtab + gotsym;
1545d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got + local_gotno;
154687c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
15479918665a45095ad135576f005c0e5307feb366a1Chris Dearman        // This is an undefined reference... try to locate it.
15489918665a45095ad135576f005c0e5307feb366a1Chris Dearman        const char* sym_name = si->strtab + sym->st_name;
1549d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        soinfo* lsi;
155029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        ElfW(Sym)* s = soinfo_do_lookup(si, sym_name, &lsi);
1551851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov        if (s == nullptr) {
15529918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // We only allow an undefined symbol if this is a weak reference.
1553d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            s = &symtab[g];
1554c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
15554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("cannot locate \"%s\"...", sym_name);
155687c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom                return false;
1557d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1558d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *got = 0;
15599918665a45095ad135576f005c0e5307feb366a1Chris Dearman        } else {
15609918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // FIXME: is this sufficient?
15619918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // For reference see NetBSD link loader
15629918665a45095ad135576f005c0e5307feb366a1Chris 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
15639918665a45095ad135576f005c0e5307feb366a1Chris Dearman            *got = reinterpret_cast<ElfW(Addr)*>(lsi->load_bias + s->st_value);
1564d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1565d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
156687c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    return true;
1567d7daacb46372132ae3f0121647074936c304b572Raghu Gandham}
1568d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1569d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1570812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallArray(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
1571851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (functions == nullptr) {
1572d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1573d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
15748215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1575c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
15768215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1577ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int begin = reverse ? (count - 1) : 0;
1578ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int end = reverse ? -1 : count;
1579ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int step = reverse ? -1 : 1;
15808215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1581ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  for (int i = begin; i != end; i += step) {
1582ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1583ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    CallFunction("function", functions[i]);
1584d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1585d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1586ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s for '%s' ]", array_name, name);
15871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
15881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1589812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallFunction(const char* function_name __unused, linker_function_t function) {
1590851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
1591d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1592d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1593d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1594ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
1595d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  function();
1596ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
1597db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes
1598db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1599db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // are still writable. This happens with our debug malloc (see http://b/7941716).
1600d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ | PROT_WRITE);
16019181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov}
16029181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
1603d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallPreInitConstructors() {
16048147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
16058147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // but ignored in a shared library.
1606d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1607d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
1608e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1609d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallConstructors() {
1610d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (constructors_called) {
1611d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1612d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1613e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1614d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // We set constructors_called before actually calling the constructors, otherwise it doesn't
1615d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // protect against recursive constructor calls. One simple example of constructor recursion
1616d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1617d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 1. The program depends on libc, so libc's constructor is called here.
1618d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1619d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 3. dlopen() calls the constructors on the newly created
1620d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    soinfo for libc_malloc_debug_leak.so.
1621d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 4. The debug .so depends on libc, so CallConstructors is
1622d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    called again with the libc soinfo. If it doesn't trigger the early-
1623d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    out above, the libc constructor will be called again (recursively!).
1624d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  constructors_called = true;
1625d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1626851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if ((flags & FLAG_EXE) == 0 && preinit_array != nullptr) {
16278147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes    // The GNU dynamic linker silently ignores these, but we warn the developer.
1628c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
16298147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes          name, preinit_array_count);
1630d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
16311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1632d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  get_children().for_each([] (soinfo* si) {
1633d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    si->CallConstructors();
1634d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  });
16351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16368147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  TRACE("\"%s\": calling constructors", name);
16378147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
16388147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_INIT should be called before DT_INIT_ARRAY if both are present.
1639d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_INIT", init_func);
1640d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
16419598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov
16429598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov  resolve_ifunc_symbols();
1643e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov}
16448215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1645d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallDestructors() {
16468147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  TRACE("\"%s\": calling destructors", name);
16478147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
16488147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_FINI_ARRAY must be parsed in reverse order.
1649d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
16508147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
16518147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_FINI should be called after DT_FINI_ARRAY if both are present.
1652d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_FINI", fini_func);
1653b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov
1654b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  // This is needed on second call to dlopen
1655b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  // after library has been unloaded with RTLD_NODELETE
1656b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  constructors_called = false;
16571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
16581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1659d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::add_child(soinfo* child) {
16600d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  if (has_min_version(0)) {
16610d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov    this->children.push_front(child);
16620d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov    child->parents.push_front(this);
1663d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1664d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1665d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1666d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::remove_all_links() {
16670d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  if (!has_min_version(0)) {
1668d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return;
1669d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1670d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1671d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  // 1. Untie connected soinfos from 'this'.
1672d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  children.for_each([&] (soinfo* child) {
1673d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    child->parents.remove_if([&] (const soinfo* parent) {
1674d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      return parent == this;
1675d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    });
1676d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  });
1677d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1678d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  parents.for_each([&] (soinfo* parent) {
16794bea498544bb1377f610520d7f58856382a6e5fcDmitriy Ivanov    parent->children.remove_if([&] (const soinfo* child) {
1680d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      return child == this;
1681d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    });
1682d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  });
1683d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1684d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  // 2. Once everything untied - clear local lists.
1685d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  parents.clear();
1686d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  children.clear();
1687d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1688d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1689d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::set_st_dev(dev_t dev) {
16900d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  if (has_min_version(0)) {
16910d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov    st_dev = dev;
1692d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1693d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1694d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1695d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::set_st_ino(ino_t ino) {
16960d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  if (has_min_version(0)) {
16970d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov    st_ino = ino;
1698d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1699d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1700d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1701c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smithvoid soinfo::set_has_ifuncs(bool ifuncs) {
17020d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  if (has_min_version(1)) {
17030d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov    has_ifuncs = ifuncs;
1704c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith  }
1705c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith}
1706c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
1707d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovdev_t soinfo::get_st_dev() {
17080d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  if (has_min_version(0)) {
17090d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov    return st_dev;
1710d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1711d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
17120d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  return 0;
1713d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov};
1714d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1715d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovino_t soinfo::get_st_ino() {
17160d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  if (has_min_version(0)) {
17170d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov    return st_ino;
1718d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1719d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
17200d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  return 0;
1721d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1722d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1723c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smithbool soinfo::get_has_ifuncs() {
17240d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  if (has_min_version(1)) {
17250d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov    return has_ifuncs;
1726c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith  }
1727c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
17280d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  return false;
1729c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith}
1730c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
1731d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov// This is a return on get_children() in case
1732d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov// 'this->flags' does not have FLAG_NEW_SOINFO set.
1733d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo::soinfo_list_t g_empty_list;
1734d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1735d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovsoinfo::soinfo_list_t& soinfo::get_children() {
17360d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  if (has_min_version(0)) {
17370d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov    return this->children;
1738d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1739d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
17400d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  return g_empty_list;
1741d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1742d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
17431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with
17441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   /dev/null. */
17455419b9474753d25dff947c7740532f86d130c0beElliott Hughesstatic int nullify_closed_stdio() {
17461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int dev_null, i, status;
17471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int return_value = 0;
17481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
174916084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
17501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null < 0) {
17514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("cannot open /dev/null: %s", strerror(errno));
17521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return -1;
17531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1754ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
17551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If any of the stdio file descriptors is valid and not associated
17571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       with /dev/null, dup /dev/null to it.  */
17581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (i = 0; i < 3; i++) {
17591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* If it is /dev/null already, we are done. */
17604688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (i == dev_null) {
17611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
17624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
17631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1764ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Nullifying stdio file descriptor %d]", i);
17654688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
17661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        /* If file is opened, we are good. */
17684688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status != -1) {
17694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            continue;
17704688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
17711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* The only error we allow is that the file descriptor does not
17731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           exist, in which case we dup /dev/null to it. */
17741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (errno != EBADF) {
17754688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("fcntl failed: %s", strerror(errno));
17761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
17771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
17781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
17791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* Try dupping /dev/null to this stdio file descriptor and
17811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           repeat if there is a signal.  Note that any errors in closing
17821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           the stdio descriptor are lost.  */
17834688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
17841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (status < 0) {
17854688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("dup2 failed: %s", strerror(errno));
17861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
17871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
17881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
17891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
17901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If /dev/null is not one of the stdio file descriptors, close it. */
17921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null > 2) {
1793ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
17944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(close(dev_null));
17954688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status == -1) {
17964688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("close failed: %s", strerror(errno));
17971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
17981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
17991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return return_value;
18021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
18031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
180429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanovbool soinfo::LinkImage(const android_dlextinfo* extinfo) {
180529bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    bool relocating_linker = (flags & FLAG_LINKER) != 0;
18061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1807b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can't debug anything until the linker is relocated */
1808b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (!relocating_linker) {
180929bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        INFO("[ linking %s ]", name);
181029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags);
1811b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
18121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
181363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    /* Extract dynamic section */
1814124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    size_t dynamic_count;
18150266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Word) dynamic_flags;
181629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic,
1817cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman                                   &dynamic_count, &dynamic_flags);
181829bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (dynamic == nullptr) {
1819b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
182029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            DL_ERR("missing PT_DYNAMIC in \"%s\"", name);
1821b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
1822124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
182363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    } else {
1824b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
182529bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            DEBUG("dynamic = %p", dynamic);
1826b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
182763f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    }
182863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
18294eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
183029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    (void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
183129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                                    &ARM_exidx, &ARM_exidx_count);
183263f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif
183363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
18348147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes    // Extract useful information from dynamic section.
1835d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    uint32_t needed_count = 0;
183629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
1837c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1838c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes              d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1839c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (d->d_tag) {
18401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_HASH:
184129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            nbucket = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
184229bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            nchain = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
184329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            bucket = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
184429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            chain = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket * 4);
18451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_STRTAB:
184729bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            strtab = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
18481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_SYMTAB:
185029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            symtab = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
18511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18524eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if !defined(__LP64__)
18531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTREL:
1854d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val != DT_REL) {
185529bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                DL_ERR("unsupported DT_RELA in \"%s\"", name);
1856124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
18571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
18581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1859c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
18601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_JMPREL:
18614eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
186229bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            plt_rela = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
1863c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
186429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            plt_rel = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
1865c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
18661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTRELSZ:
18684eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
186929bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            plt_rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
1870c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
187129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            plt_rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
1872c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
18731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18749918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__mips__)
18751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTGOT:
18769918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // Used by mips and mips64.
187729bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            plt_got = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
18781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1879c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
18801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_DEBUG:
1881bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_DEBUG entry to the address of _r_debug for GDB
1882cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman            // if the dynamic table is writable
18839918665a45095ad135576f005c0e5307feb366a1Chris Dearman// FIXME: not working currently for N64
18849918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The flags for the LOAD and DYNAMIC program headers do not agree.
18859918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The LOAD section containng the dynamic table has been mapped as
18869918665a45095ad135576f005c0e5307feb366a1Chris Dearman// read-only, but the DYNAMIC header claims it is writable.
18879918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if !(defined(__mips__) && defined(__LP64__))
188899c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            if ((dynamic_flags & PF_W) != 0) {
1889c620059479c47a78d57086d73726c9adc2f337adElliott Hughes                d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
189099c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            }
18911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
18929918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif
18934eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1894c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         case DT_RELA:
189529bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            rela = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
1896c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1897c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         case DT_RELASZ:
189829bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
1899c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1900c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_REL:
190129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            DL_ERR("unsupported DT_REL in \"%s\"", name);
1902c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1903c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_RELSZ:
190429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            DL_ERR("unsupported DT_RELSZ in \"%s\"", name);
1905c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1906c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1907c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_REL:
190829bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            rel = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
1909c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1910c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_RELSZ:
191129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
1912c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1913ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI         case DT_RELA:
191429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            DL_ERR("unsupported DT_RELA in \"%s\"", name);
1915124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1916c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
19171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT:
191829bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            init_func = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
191929bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            DEBUG("%s constructors (DT_INIT) found at %p", name, init_func);
19201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
19211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI:
192229bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            fini_func = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
192329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            DEBUG("%s destructors (DT_FINI) found at %p", name, fini_func);
19241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
19251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAY:
192629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            init_array = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
192729bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", name, init_array);
19281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
19291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAYSZ:
193029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            init_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
19311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
19321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAY:
193329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            fini_array = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
193429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", name, fini_array);
19351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
19361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAYSZ:
193729bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
19381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
19391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAY:
194029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            preinit_array = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
194129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", name, preinit_array);
19421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
19431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAYSZ:
194429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
19451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
19461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_TEXTREL:
1947e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__)
194829bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", name);
1949e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes            return false;
1950e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else
195129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            has_text_relocations = true;
19521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1953e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
19545ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_SYMBOLIC:
195529bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            has_DT_SYMBOLIC = true;
19565ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
1957d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        case DT_NEEDED:
1958d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            ++needed_count;
1959d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            break;
19605ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_FLAGS:
1961d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_TEXTREL) {
1962e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__)
196329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", name);
1964e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes                return false;
1965e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else
196629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                has_text_relocations = true;
1967e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
19685ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
1969d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_SYMBOLIC) {
197029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                has_DT_SYMBOLIC = true;
19715ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
19725ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
19734eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
1974d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_STRSZ:
1975d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_SYMENT:
1976d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_RELENT:
1977d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             break;
1978d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_MAP:
1979bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
1980d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            {
198129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov              r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
1982d7daacb46372132ae3f0121647074936c304b572Raghu Gandham              *dp = &_r_debug;
1983d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1984d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1985d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_VERSION:
1986d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_FLAGS:
1987d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_BASE_ADDRESS:
1988d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_UNREFEXTNO:
1989d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1990d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1991d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_SYMTABNO:
199229bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            mips_symtabno = d->d_un.d_val;
1993d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1994d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1995d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_LOCAL_GOTNO:
199629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            mips_local_gotno = d->d_un.d_val;
1997d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1998d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1999d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_GOTSYM:
200029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            mips_gotsym = d->d_un.d_val;
2001d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
20024eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
2003d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
2004d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        default:
20054eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            DEBUG("Unused DT entry: type %p arg %p",
20064eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                  reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
2007d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
20081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
20091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
20101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2011c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
201229bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov          reinterpret_cast<void*>(base), strtab, symtab);
20131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2014124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    // Sanity checks.
2015d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (relocating_linker && needed_count != 0) {
2016d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
2017d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        return false;
2018d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
201929bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (nbucket == 0) {
202029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", name);
2021124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
2022124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
202329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (strtab == 0) {
202429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        DL_ERR("empty/missing DT_STRTAB in \"%s\"", name);
2025124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
2026124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
202729bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (symtab == 0) {
202829bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        DL_ERR("empty/missing DT_SYMTAB in \"%s\"", name);
2029124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
20301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
20311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
20327e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes    // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
203329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (flags & FLAG_EXE) {
20341728b2396591853345507a063ed6075dfd251706Elliott Hughes        memset(g_ld_preloads, 0, sizeof(g_ld_preloads));
20357e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes        size_t preload_count = 0;
2036851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov        for (size_t i = 0; g_ld_preload_names[i] != nullptr; i++) {
2037851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov            soinfo* lsi = find_library(g_ld_preload_names[i], 0, nullptr);
2038851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov            if (lsi != nullptr) {
20391728b2396591853345507a063ed6075dfd251706Elliott Hughes                g_ld_preloads[preload_count++] = lsi;
20407e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes            } else {
20417e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                // As with glibc, failure to load an LD_PRELOAD library is just a warning.
20427e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
204329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                        g_ld_preload_names[i], name, linker_get_error_buffer());
20444fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            }
20454fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        }
20464fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
20474fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
204829bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
2049d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (d->d_tag == DT_NEEDED) {
205029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            const char* library_name = strtab + d->d_un.d_val;
205129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            DEBUG("%s needs %s", name, library_name);
2052851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov            soinfo* lsi = find_library(library_name, 0, nullptr);
2053851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov            if (lsi == nullptr) {
2054650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes                strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
20554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
205629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                       library_name, name, tmp_err_buf);
2057124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
20581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
2059d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
206029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov            add_child(lsi);
20611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
20621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
20631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2064e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__)
206529bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (has_text_relocations) {
2066e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // Make segments writable to allow text relocations to work properly. We will later call
2067e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // phdr_table_protect_segments() after all of them are applied and all constructors are run.
20683d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich        DL_WARN("%s has text relocations. This is wasting memory and prevents "
206929bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                "security hardening. Please fix.", name);
207029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
20715135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't unprotect loadable segments for \"%s\": %s",
207229bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                   name, strerror(errno));
2073124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
20745135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
20755135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    }
2076e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
20775135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich
20784eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
207929bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (plt_rela != nullptr) {
208029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        DEBUG("[ relocating %s plt ]\n", name);
208129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        if (Relocate(plt_rela, plt_rela_count)) {
2082c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
2083c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
2084c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
208529bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (rela != nullptr) {
208629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        DEBUG("[ relocating %s ]\n", name);
208729bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        if (Relocate(rela, rela_count)) {
2088c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
2089c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
2090c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
2091c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
209229bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (plt_rel != nullptr) {
209329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        DEBUG("[ relocating %s plt ]", name);
209429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        if (Relocate(plt_rel, plt_rel_count)) {
2095124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
2096124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
20971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
209829bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (rel != nullptr) {
209929bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        DEBUG("[ relocating %s ]", name);
210029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        if (Relocate(rel, rel_count)) {
2101124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
2102124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
21031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2104c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
21051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2106c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    // if there are ifuncs, we need to do an additional relocation pass.
2107c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    // they cannot be resolved until the rest of the relocations are done
2108c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    // because we need to call the resolution function which may be waiting
2109c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    // on relocations.
211029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if(get_has_ifuncs()) {
2111c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#if defined(__i386__)
211229bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov      soinfo_ifunc_relocate(this, plt_rel, plt_rel_count);
2113c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#elif defined(__x86_64__)
211429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov      soinfo_ifunc_relocate(this, plt_rela, plt_rela_count);
2115c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#endif
2116c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith    }
2117c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith
21184eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
211929bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (!mips_relocate_got(this)) {
2120124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
2121d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
2122d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
2123d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
212429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    flags |= FLAG_LINKED;
212529bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    DEBUG("[ finished linking %s ]", name);
21261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2127e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__)
212829bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (has_text_relocations) {
2129e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // All relocations are done, we can protect our segments back to read-only.
213029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov        if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
21315135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't protect segments for \"%s\": %s",
213229bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov                   name, strerror(errno));
2133124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
21345135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
21351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2136e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
21371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2138b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can also turn on GNU RELRO protection */
213929bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
21404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
214129bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov               name, strerror(errno));
2142124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
21439ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich    }
21449ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
2145183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    /* Handle serializing/sharing the RELRO segment */
2146183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
214729bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov      if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
2148183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)                                         extinfo->relro_fd) < 0) {
2149183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
215029bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov               name, strerror(errno));
2151183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        return false;
2152183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      }
2153183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
215429bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov      if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
2155183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)                                   extinfo->relro_fd) < 0) {
2156183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
215729bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov               name, strerror(errno));
2158183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        return false;
2159183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      }
2160183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    }
2161183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)
216229bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    notify_gdb_of_load(this);
2163124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    return true;
21641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
21651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2166468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
2167c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * This function add vdso to internal dso list.
2168c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * It helps to stack unwinding through signal handlers.
2169c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * Also, it makes bionic more like glibc.
2170c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov */
2171812fd4263a005b88f3b4222baa910114f938d594Kito Chengstatic void add_vdso(KernelArgumentBlock& args __unused) {
21724eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(AT_SYSINFO_EHDR)
21730266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
2174851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  if (ehdr_vdso == nullptr) {
21750266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    return;
21760266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  }
2177c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
2178851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  soinfo* si = soinfo_alloc("[vdso]", nullptr);
2179ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov
21800266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
21810266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->phnum = ehdr_vdso->e_phnum;
21820266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
21830266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->size = phdr_table_get_load_size(si->phdr, si->phnum);
21840266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
2185ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov
218629bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov  si->LinkImage(nullptr);
2187c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov#endif
2188c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov}
2189c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
2190c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov/*
2191d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * This is linker soinfo for GDB. See details below.
2192d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov */
21930d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#if defined(__LP64__)
21940d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#define LINKER_PATH "/system/bin/linker64"
21950d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#else
21960d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#define LINKER_PATH "/system/bin/linker"
21970d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#endif
21980d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanovstatic soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr);
2199d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
2200d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov/* gdb expects the linker to be in the debug shared object list.
2201d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * Without this, gdb has trouble locating the linker's ".text"
2202d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * and ".plt" sections. Gdb could also potentially use this to
2203d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2204d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * Don't use soinfo_alloc(), because the linker shouldn't
2205d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * be on the soinfo list.
2206d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov */
2207d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
2208d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  linker_soinfo_for_gdb.base = linker_base;
2209d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
2210d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  /*
2211d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   * Set the dynamic field in the link map otherwise gdb will complain with
2212d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   * the following:
2213d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   *   warning: .dynamic section for "/system/bin/linker" is not at the
2214d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   *   expected address (wrong library or version mismatch?)
2215d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   */
2216d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2217d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2218d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
2219851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov                                 &linker_soinfo_for_gdb.dynamic, nullptr, nullptr);
2220d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2221d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
2222d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
2223d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov/*
2224468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and
2225468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs
2226468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point.
2227468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
22280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
22291a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING
22301a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    struct timeval t0, t1;
22311a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    gettimeofday(&t0, 0);
22321a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif
22331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
223418a206c81d9743481e364384affd43306911283dElliott Hughes    // Initialize environment functions, and get to the ELF aux vectors table.
223542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    linker_env_init(args);
2236be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
22378d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // If this is a setuid/setgid program, close the security hole described in
22388d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
22398d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    if (get_AT_SECURE()) {
22408d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich        nullify_closed_stdio();
22418d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    }
22428d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich
2243d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    debuggerd_init();
22441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
224518a206c81d9743481e364384affd43306911283dElliott Hughes    // Get a few environment variables.
224661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    const char* LD_DEBUG = linker_env_get("LD_DEBUG");
2247851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    if (LD_DEBUG != nullptr) {
22481728b2396591853345507a063ed6075dfd251706Elliott Hughes      g_ld_debug_verbosity = atoi(LD_DEBUG);
224918a206c81d9743481e364384affd43306911283dElliott Hughes    }
2250be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
225118a206c81d9743481e364384affd43306911283dElliott Hughes    // Normally, these are cleaned by linker_env_init, but the test
225218a206c81d9743481e364384affd43306911283dElliott Hughes    // doesn't cost us anything.
2253851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    const char* ldpath_env = nullptr;
2254851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    const char* ldpreload_env = nullptr;
225518a206c81d9743481e364384affd43306911283dElliott Hughes    if (!get_AT_SECURE()) {
225618a206c81d9743481e364384affd43306911283dElliott Hughes      ldpath_env = linker_env_get("LD_LIBRARY_PATH");
225718a206c81d9743481e364384affd43306911283dElliott Hughes      ldpreload_env = linker_env_get("LD_PRELOAD");
22581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
22591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2260ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    INFO("[ android linker & debugger ]");
22611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2262851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    soinfo* si = soinfo_alloc(args.argv[0], nullptr);
2263851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    if (si == nullptr) {
226418a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
22651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
22661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2267d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    /* bootstrap the link map, the main exe always needs to be first */
22681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_EXE;
22693a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(si->link_map_head);
22701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
22711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = 0;
227242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    map->l_name = args.argv[0];
2273851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    map->l_prev = nullptr;
2274851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    map->l_next = nullptr;
22751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
22761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_map = map;
22771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
22781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2279d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    init_linker_info_for_gdb(linker_base);
22801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
228142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // Extract information passed from the kernel.
22820266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
228342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->phnum = args.getauxval(AT_PHNUM);
228442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->entry = args.getauxval(AT_ENTRY);
22851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
22868180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    /* Compute the value of si->base. We can't rely on the fact that
22878180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * the first entry is the PHDR because this will not be true
22888180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * for certain executables (e.g. some in the NDK unit test suite)
22898180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     */
22908180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    si->base = 0;
2291b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2292bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    si->load_bias = 0;
2293ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    for (size_t i = 0; i < si->phnum; ++i) {
229442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      if (si->phdr[i].p_type == PT_PHDR) {
22950266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
22960266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
229742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes        break;
229842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      }
22998180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    }
2300851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    si->dynamic = nullptr;
2301ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 1;
23021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
23032aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich    ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
23042aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich    if (elf_hdr->e_type != ET_DYN) {
23052aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich        __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
23062aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich        exit(EXIT_FAILURE);
23072aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich    }
23082aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich
23094688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
23104688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_LIBRARY_PATH(ldpath_env);
23114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_PRELOAD(ldpreload_env);
23124fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
23135ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    somain = si;
23145ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
231529bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    if (!si->LinkImage(nullptr)) {
2316650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes        __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
231718a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
23181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
23191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2320c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov    add_vdso(args);
2321c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
2322d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallPreInitConstructors();
23239181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
2324851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    for (size_t i = 0; g_ld_preloads[i] != nullptr; ++i) {
23251728b2396591853345507a063ed6075dfd251706Elliott Hughes        g_ld_preloads[i]->CallConstructors();
2326326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng    }
2327326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng
232829bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov    /* After the LinkImage, the si->load_bias is initialized.
2329d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2330d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * We need to update this value for so exe here. So Unwind_Backtrace
2331d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * for some arch like x86 could work correctly within so exe.
23329c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     */
2333c5db969aa4a24a76fc0964f2ff89533e832b47a6Chao-Ying Fu    map->l_addr = si->load_bias;
2334d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
2335e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
23361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING
2337851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov    gettimeofday(&t1, nullptr);
2338ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
23391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2340faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes               (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
23411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
23421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
2343ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2344bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocAbsolute],
2345bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocRelative],
2346bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocCopy],
2347bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocSymbol]);
23481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
23491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
23501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
23511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned n;
23521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned i;
23531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned count = 0;
2354d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        for (n = 0; n < 4096; n++) {
2355d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (bitmask[n]) {
23561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                unsigned x = bitmask[n];
2357e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__LP64__)
2358e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland                for (i = 0; i < 32; i++) {
2359e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#else
2360d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                for (i = 0; i < 8; i++) {
2361e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#endif
2362d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    if (x & 1) {
2363d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                        count++;
2364d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    }
23651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    x >>= 1;
23661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                }
23671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
23681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
2369ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
23701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
23711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
23721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
23731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES
23741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    fflush(stdout);
23751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
23761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2377c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
23781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si->entry;
23791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2380468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2381bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only
2382bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library
2383bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself.
2384bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *
2385bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input:
2386bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    elf    -> address of ELF header, assumed to be at the start of the file.
2387bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return:
2388bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    load bias, i.e. add the value of any p_vaddr in the file to get
2389bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    the corresponding address in memory.
2390bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */
23910266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
23920266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) offset = elf->e_phoff;
2393faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes  const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
23940266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
2395fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
23960266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
2397fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (phdr->p_type == PT_LOAD) {
23980266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
2399bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    }
2400fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
2401fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return 0;
2402bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner}
2403bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
2404efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanovextern "C" void _start();
2405efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov
2406468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
2407468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This
2408468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and
2409468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation().
2410468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich *
2411468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own
2412468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern
2413468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault.
2414468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
24150266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesextern "C" ElfW(Addr) __linker_init(void* raw_args) {
241642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  KernelArgumentBlock args(raw_args);
241742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
24180266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
2419efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov  ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
24200266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
2421faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes  ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
242242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
24230d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  soinfo linker_so("[dynamic linker]", nullptr);
242442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
2425efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov  // If the linker is not acting as PT_INTERP entry_point is equal to
2426efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov  // _start. Which means that the linker is running as an executable and
2427efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov  // already linked by PT_INTERP.
2428efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov  //
2429efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov  // This happens when user tries to run 'adb shell /system/bin/linker'
2430efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov  // see also https://code.google.com/p/android/issues/detail?id=63174
2431efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov  if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
2432efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov    __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
2433efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov  }
2434efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov
243542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.base = linker_addr;
243642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
243742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
2438851135bf9941b3813adb9b4f43d76e040c4ba157Dmitriy Ivanov  linker_so.dynamic = nullptr;
243942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phdr = phdr;
244042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phnum = elf_hdr->e_phnum;
244142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.flags |= FLAG_LINKER;
244242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
244329bbc9dd4c606de9187e46d8899a2a744715c967Dmitriy Ivanov  if (!linker_so.LinkImage(nullptr)) {
244442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // It would be nice to print an error message, but if the linker
244542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // can't link itself, there's no guarantee that we'll be able to
2446b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    // call write() (because it involves a GOT reference). We may as
2447b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    // well try though...
2448b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    const char* msg = "CANNOT LINK EXECUTABLE: ";
2449b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, msg, strlen(msg));
2450b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2451b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, "\n", 1);
2452b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    _exit(EXIT_FAILURE);
245342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  }
2454468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
245514241402de0faa4b244b1bd6b1f0799ce169b880Dmitriy Ivanov  __libc_init_tls(args);
245614241402de0faa4b244b1bd6b1f0799ce169b880Dmitriy Ivanov
2457efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov  // Initialize the linker's own global variables
24584151ea73b75e274d1ff80b42d9d457a783208516Dmitriy Ivanov  linker_so.CallConstructors();
24594151ea73b75e274d1ff80b42d9d457a783208516Dmitriy Ivanov
24600d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  // Initialize static variables. Note that in order to
24610d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  // get correct libdl_info we need to call constructors
24620d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  // before get_libdl_info().
24630d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  solist = get_libdl_info();
24640d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov  sonext = get_libdl_info();
24650d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov
246642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // We have successfully fixed our own relocations. It's safe to run
246742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // the main part of the linker now.
24681728b2396591853345507a063ed6075dfd251706Elliott Hughes  args.abort_message_ptr = &g_abort_message;
24690266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
24705419b9474753d25dff947c7740532f86d130c0beElliott Hughes
2471d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ);
2472d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
247342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // Return the address that the calling assembly stub should jump to.
247442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  return start_address;
2475468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
2476