linker.cpp revision 76e289c026f11126fc88841b3019fd5bb419bb67
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 <sys/stat.h>
394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <unistd.h>
401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Private C library headers.
42eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/bionic_tls.h"
43eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/KernelArgumentBlock.h"
44eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/ScopedPthreadMutexLocker.h"
4504dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov#include "private/ScopedFd.h"
461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker.h"
481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker_debug.h"
49be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner#include "linker_environ.h"
5023363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner#include "linker_phdr.h"
51d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov#include "linker_allocator.h"
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Do NOT use malloc() and friends or pthread_*() code here.
561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Don't use printf() either; it's caused mysterious memory
571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * corruption in the past.
581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The linker runs before we bring up libc and it's easiest
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to make sure it does not depend on any complex libc features
601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * open issues / todo:
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - cleaner error reporting
641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - after linking, set as much stuff as possible to READONLY
651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   and NOEXEC
664688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes */
671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
68489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#if defined(__LP64__)
69489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#define SEARCH_NAME(x) x
70489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#else
71489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov// Nvidia drivers are relying on the bug:
72489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov// http://code.google.com/p/android/issues/detail?id=6670
73489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov// so we continue to use base-name lookup for lp32
74489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanovstatic const char* get_base_name(const char* name) {
75489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  const char* bname = strrchr(name, '/');
76489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  return bname ? bname + 1 : name;
77489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov}
78489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#define SEARCH_NAME(x) get_base_name(x)
79489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#endif
80489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov
81183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo);
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
99124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  NULL
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
1181728b2396591853345507a063ed6075dfd251706Elliott Hughes__LIBC_HIDDEN__ abort_msg_t* g_abort_message = NULL; // 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
128d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanovenum class SymbolLookupScope {
129d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov  kAllowLocal,
130d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov  kExcludeLocal,
131d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov};
132d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov
1331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
134bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstruct linker_stats_t {
135bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    int count[kRelocMax];
136bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
137bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
138bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic linker_stats_t linker_stats;
139bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
140bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind kind) {
141bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    ++linker_stats.count[kind];
142bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
143bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
144bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind) {
145bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
1461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
149bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic unsigned bitmask[4096];
150e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__LP64__)
151e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#define MARK(offset) \
152e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    do { \
153e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        if ((((offset) >> 12) >> 5) < 4096) \
154e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            bitmask[((offset) >> 12) >> 5] |= (1 << (((offset) >> 12) & 31)); \
155faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    } while (0)
156e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#else
157bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(offset) \
158bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    do { \
159bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
160faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    } while (0)
161e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#endif
162bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
163bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(x) do {} while (0)
1641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1664688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// You shouldn't try to call memory-allocating functions in the dynamic linker.
1674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Guard against the most obvious ones.
1688f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#define DISALLOW_ALLOCATION(return_type, name, ...) \
1698f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    return_type name __VA_ARGS__ \
1708f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    { \
17112bf3bcdeeaa0dcdc7a4f4e4d11bc2fc3bf6bd7aDmitriy Ivanov        __libc_fatal("ERROR: " #name " called from the dynamic linker!\n"); \
1724688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
173812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, malloc, (size_t u __unused));
174812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void, free, (void* u __unused));
175812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
176812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
1772e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1780353195f344666256dba474a15c9ba22cf0cccc9Dima Zavinstatic char tmp_err_buf[768];
1792e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavinstatic char __linker_dl_err_buf[768];
1802e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
181650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hugheschar* linker_get_error_buffer() {
1825419b9474753d25dff947c7740532f86d130c0beElliott Hughes  return &__linker_dl_err_buf[0];
1832e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin}
1842e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
185650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughessize_t linker_get_error_buffer_size() {
186650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  return sizeof(__linker_dl_err_buf);
187650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes}
188650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
1891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This function is an empty stub where GDB locates a breakpoint to get notified
1911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about linker activity.
1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1935419b9474753d25dff947c7740532f86d130c0beElliott Hughesextern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1951728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
1963a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic r_debug _r_debug = {1, NULL, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
1973a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic link_map* r_debug_tail = 0;
1981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1993a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic void insert_soinfo_into_debug_map(soinfo* info) {
200bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    // Copy the necessary fields into the debug structure.
2013a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(info->link_map_head);
202ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov    map->l_addr = info->load_bias;
203faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    map->l_name = reinterpret_cast<char*>(info->name);
2043a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    map->l_ld = info->dynamic;
2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Stick the new library at the end of the list.
2071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * gdb tends to care more about libc than it does
2081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * about leaf libraries, and ordering it this way
2091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * reduces the back-and-forth over the wire.
2101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
2111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (r_debug_tail) {
2121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        r_debug_tail->l_next = map;
2131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = r_debug_tail;
2141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    } else {
2161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        _r_debug.r_map = map;
2171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = 0;
2181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
2211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
223bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void remove_soinfo_from_debug_map(soinfo* info) {
2243a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(info->link_map_head);
2255e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
226bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (r_debug_tail == map) {
2275e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        r_debug_tail = map->l_prev;
228bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2295e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
230bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_prev) {
231bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_prev->l_next = map->l_next;
232bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
233bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_next) {
234bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_next->l_prev = map->l_prev;
235bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2365e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2375e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
238bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_load(soinfo* info) {
2391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (info->flags & FLAG_EXE) {
2401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        // GDB already knows about the main executable
2411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
2421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2441728b2396591853345507a063ed6075dfd251706Elliott Hughes    ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
2451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2463a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_ADD;
2471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(info);
2501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2513a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_CONSISTENT;
2521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2535e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2545e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
255bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_unload(soinfo* info) {
2565e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    if (info->flags & FLAG_EXE) {
2575e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        // GDB already knows about the main executable
2585e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        return;
2595e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    }
2605e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2611728b2396591853345507a063ed6075dfd251706Elliott Hughes    ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
2625e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2633a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_DELETE;
2645e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2655e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2665e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    remove_soinfo_from_debug_map(info);
2675e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2683a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_CONSISTENT;
2695e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
27218a206c81d9743481e364384affd43306911283dElliott Hughesvoid notify_gdb_of_libraries() {
2733a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  _r_debug.r_state = r_debug::RT_ADD;
2743a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  rtld_db_dlactivity();
2753a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  _r_debug.r_state = r_debug::RT_CONSISTENT;
2763a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  rtld_db_dlactivity();
2771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
279d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy IvanovLinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
280d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  return g_soinfo_links_allocator.alloc();
281d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
282d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
283d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
284d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  g_soinfo_links_allocator.free(entry);
285d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
286d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
287d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic void protect_data(int protection) {
288d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  g_soinfo_allocator.protect_all(protection);
289d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  g_soinfo_links_allocator.protect_all(protection);
290d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
291d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
292d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo* soinfo_alloc(const char* name, struct stat* file_stat) {
293ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (strlen(name) >= SOINFO_NAME_LEN) {
294ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    DL_ERR("library name \"%s\" too long", name);
295ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return NULL;
296ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
297ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
2981728b2396591853345507a063ed6075dfd251706Elliott Hughes  soinfo* si = g_soinfo_allocator.alloc();
2991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
300ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Initialize the new element.
301ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  memset(si, 0, sizeof(soinfo));
302ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  strlcpy(si->name, name, sizeof(si->name));
303d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  si->flags = FLAG_NEW_SOINFO;
304d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
305d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if (file_stat != NULL) {
306d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    si->set_st_dev(file_stat->st_dev);
307d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    si->set_st_ino(file_stat->st_ino);
308d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
309d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
310ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext->next = si;
311ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext = si;
3121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
313ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("name %s: allocated soinfo @ %p", name, si);
314ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  return si;
3151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
317faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesstatic void soinfo_free(soinfo* si) {
3184688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si == NULL) {
3194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return;
3204688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
3214688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
322d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    if (si->base != 0 && si->size != 0) {
323d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      munmap(reinterpret_cast<void*>(si->base), si->size);
324d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    }
325d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
3261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *prev = NULL, *trav;
3271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
328ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("name %s: freeing soinfo @ %p", si->name, si);
3291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
330d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (trav = solist; trav != NULL; trav = trav->next) {
3311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (trav == si)
3321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
3331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        prev = trav;
3341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (trav == NULL) {
336d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        /* si was not in solist */
3374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("name \"%s\" is not in solist!", si->name);
3381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
3391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
341d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // clear links to/from si
342d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    si->remove_all_links();
343d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
344be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    /* prev will never be NULL, because the first entry in solist is
3451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       always the static libdl_info.
3461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    */
3471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    prev->next = si->next;
348d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si == sonext) {
349d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        sonext = prev;
350d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
351d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov
3521728b2396591853345507a063ed6075dfd251706Elliott Hughes    g_soinfo_allocator.free(si);
3531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
355cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
356cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_path(const char* path, const char* delimiters,
357cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes                       const char** array, char* buf, size_t buf_size, size_t max_count) {
358cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (path == NULL) {
359cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return;
360cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
361cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
362cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t len = strlcpy(buf, path, buf_size);
363cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
364cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t i = 0;
365cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  char* buf_p = buf;
366cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
367cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (*array[i] != '\0') {
368cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      ++i;
369cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    }
370cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
371cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
372cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // Forget the last path if we had to truncate; this occurs if the 2nd to
373cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // last char isn't '\0' (i.e. wasn't originally a delimiter).
374cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
375cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    array[i - 1] = NULL;
376cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  } else {
377cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    array[i] = NULL;
378cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
379cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
380cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
381cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_LIBRARY_PATH(const char* path) {
3821728b2396591853345507a063ed6075dfd251706Elliott Hughes  parse_path(path, ":", g_ld_library_paths,
3831728b2396591853345507a063ed6075dfd251706Elliott Hughes             g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
384cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
385cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
386cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_PRELOAD(const char* path) {
387cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // We have historically supported ':' as well as ' ' in LD_PRELOAD.
3881728b2396591853345507a063ed6075dfd251706Elliott Hughes  parse_path(path, " :", g_ld_preload_names,
3891728b2396591853345507a063ed6075dfd251706Elliott Hughes             g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
390cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
391cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
3924eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
3934688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* For a given PC, find the .so that it belongs to.
3951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the base address of the .ARM.exidx section
3961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for that .so, and the number of 8-byte entries
3971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in that section (via *pcount).
3981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Intended to be called by libc's __gnu_Unwind_Find_exidx().
4001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4013b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes * This function is exposed via dlfcn.cpp and libdl.so.
4021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
403faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
4041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned addr = (unsigned)pc;
4051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
406faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    for (soinfo* si = solist; si != 0; si = si->next) {
407468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if ((addr >= si->base) && (addr < (si->base + si->size))) {
408468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            *pcount = si->ARM_exidx_count;
409f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            return (_Unwind_Ptr)si->ARM_exidx;
4101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
4111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
412faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    *pcount = 0;
4131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return NULL;
4141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4154688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
41624053a461e7a20f34002262c1bb122023134989dChristopher Ferris#endif
4174688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
4181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Here, we only have to provide a callback to iterate across all the
4191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * loaded libraries. gcc_eh does the rest. */
420faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesint dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
4211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int rv = 0;
422bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    for (soinfo* si = solist; si != NULL; si = si->next) {
423bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        dl_phdr_info dl_info;
4243a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes        dl_info.dlpi_addr = si->link_map_head.l_addr;
4253a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes        dl_info.dlpi_name = si->link_map_head.l_name;
4261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phdr = si->phdr;
4271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phnum = si->phnum;
428bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        rv = cb(&dl_info, sizeof(dl_phdr_info), data);
429bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        if (rv != 0) {
4301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
431bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        }
4321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return rv;
4341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
436d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanovstatic ElfW(Sym)* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name, const SymbolLookupScope& lookup_scope) {
4370266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* symtab = si->symtab;
4380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  const char* strtab = si->strtab;
4391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4400266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd",
4410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes             name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
4421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4430266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
4440266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s = symtab + n;
4450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    if (strcmp(strtab + s->st_name, name)) continue;
4461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    switch (ELF_ST_BIND(s->st_info)) {
4480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      case STB_GLOBAL:
4490266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      case STB_WEAK:
4500266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if (s->st_shndx == SHN_UNDEF) {
451d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov          continue;
452d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        }
4531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
454d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
4550266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                 name, si->name, reinterpret_cast<void*>(s->st_value),
4560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                 static_cast<size_t>(s->st_size));
457d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        return s;
458d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov      case STB_LOCAL:
459d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        if (lookup_scope != SymbolLookupScope::kAllowLocal) {
460d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov          continue;
461d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        }
462d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        TRACE_TYPE(LOOKUP, "FOUND LOCAL %s in %s (%p) %zd",
463d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov                name, si->name, reinterpret_cast<void*>(s->st_value),
464d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov                static_cast<size_t>(s->st_size));
465d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        return s;
466d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov      default:
46712bf3bcdeeaa0dcdc7a4f4e4d11bc2fc3bf6bd7aDmitriy Ivanov        __libc_fatal("ERROR: Unexpected ST_BIND value: %d for '%s' in '%s'",
46812bf3bcdeeaa0dcdc7a4f4e4d11bc2fc3bf6bd7aDmitriy Ivanov            ELF_ST_BIND(s->st_info), name, si->name);
4691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4700266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  }
4711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4720266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  return NULL;
4731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
475d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic unsigned elfhash(const char* _name) {
476faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    const unsigned char* name = reinterpret_cast<const unsigned char*>(_name);
4771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned h = 0, g;
4781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
479faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    while (*name) {
4801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h = (h << 4) + *name++;
4811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        g = h & 0xf0000000;
4821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g;
4831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g >> 24;
4841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return h;
4861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4880266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Sym)* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
489943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
4900266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s = NULL;
4916ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
492c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin    if (si != NULL && somain != NULL) {
4935ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        /*
494c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * Local scope is executable scope. Just start looking into it right away
495c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * for the shortcut.
4965ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel         */
4975ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
498c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        if (si == somain) {
499d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov            s = soinfo_elf_lookup(si, elf_hash, name, SymbolLookupScope::kAllowLocal);
5005ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (s != NULL) {
501c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
5025ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                goto done;
5035ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
504c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        } else {
505c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
506c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
507c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
508c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with symbolic relocations disabled, the
509c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * first place to look to resolve external references is the main
510c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * executable.
511c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
512c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
513c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (!si->has_DT_SYMBOLIC) {
514ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s",
51561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
516d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov                s = soinfo_elf_lookup(somain, elf_hash, name, SymbolLookupScope::kExcludeLocal);
517c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
518c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
519c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
520c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
521c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5225ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
523c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Look for symbols in the local scope (the object who is
5244eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes             * searching). This happens with C++ templates on x86 for some
525c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * reason.
526c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             *
527c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Notes on weak symbols:
528c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * The ELF specs are ambiguous about treatment of weak definitions in
529c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * dynamic linking.  Some systems return the first definition found
530c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * and some the first non-weak definition.   This is system dependent.
531c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Here we return the first definition found for simplicity.  */
532c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
533d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov            s = soinfo_elf_lookup(si, elf_hash, name, SymbolLookupScope::kAllowLocal);
534c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (s != NULL) {
535c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
536c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                goto done;
537c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
538c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
539c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
540c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with -Bsymbolic and symbol is not found
541c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * in the local scope, try to find the symbol in the main executable.
542c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
543c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
544c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (si->has_DT_SYMBOLIC) {
545ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s after local scope",
54661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
547d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov                s = soinfo_elf_lookup(somain, elf_hash, name, SymbolLookupScope::kExcludeLocal);
548c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
549c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
550c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
551c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
552c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5535ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
554d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    }
5556ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5564fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* Next, look for it in the preloads list */
5571728b2396591853345507a063ed6075dfd251706Elliott Hughes    for (int i = 0; g_ld_preloads[i] != NULL; i++) {
558d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        s = soinfo_elf_lookup(g_ld_preloads[i], elf_hash, name, SymbolLookupScope::kExcludeLocal);
559d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (s != NULL) {
5601728b2396591853345507a063ed6075dfd251706Elliott Hughes            *lsi = g_ld_preloads[i];
5614fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            goto done;
5625ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5634fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
5644fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
565d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (int i = 0; needed[i] != NULL; i++) {
566ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        DEBUG("%s: looking up %s in %s",
56761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes              si->name, name, needed[i]->name);
568d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        s = soinfo_elf_lookup(needed[i], elf_hash, name, SymbolLookupScope::kExcludeLocal);
5695ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if (s != NULL) {
5705ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            *lsi = needed[i];
57112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            goto done;
5725ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5736ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5746ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5756ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevdone:
576d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (s != NULL) {
577c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
578c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   "found in %s, base = %p, load bias = %p",
579c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   si->name, name, reinterpret_cast<void*>(s->st_value),
580c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   (*lsi)->name, reinterpret_cast<void*>((*lsi)->base),
581c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   reinterpret_cast<void*>((*lsi)->load_bias));
5826ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        return s;
5836ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5846ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
585943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
5866ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev}
5876ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
588d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3).  It performs symbol lookup only within the
5896ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev   specified soinfo object and not in any of its dependencies.
590d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom
591d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
592d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   that it should do a breadth first search through the dependency
593d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   tree. This agrees with the ELF spec (aka System V Application
594d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   Binary Interface) where in Chapter 5 it discuss resolving "Shared
595d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   Object Dependencies" in breadth first search order.
5966ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
597d97e9f546ea195686a78e539315b273393609b9eDmitriy IvanovElfW(Sym)* dlsym_handle_lookup(soinfo* si, const char* name, soinfo* caller) {
598d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov    return soinfo_elf_lookup(si, elfhash(name), name,
599d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        caller == si ? SymbolLookupScope::kAllowLocal : SymbolLookupScope::kExcludeLocal);
6001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
602d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3) to performs a global symbol lookup. If the
603d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   start value is null (for RTLD_DEFAULT), the search starts at the
604d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   beginning of the global solist. Otherwise the search starts at the
605d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   specified soinfo (for RTLD_NEXT).
6066ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
607d97e9f546ea195686a78e539315b273393609b9eDmitriy IvanovElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start, soinfo* caller) {
608cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  unsigned elf_hash = elfhash(name);
6091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
610cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (start == NULL) {
611cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    start = solist;
612cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6131698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer
6140266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* s = NULL;
615cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
616d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov    s = soinfo_elf_lookup(si, elf_hash, name,
617d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov        caller == si ? SymbolLookupScope::kAllowLocal : SymbolLookupScope::kExcludeLocal);
618cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (s != NULL) {
619cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      *found = si;
620cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      break;
6211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
622cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
624cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (s != NULL) {
625c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
626c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes               name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
627cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
629cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  return s;
6301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
632fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Chengsoinfo* find_containing_library(const void* p) {
6330266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
634fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (soinfo* si = solist; si != NULL; si = si->next) {
635fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (address >= si->base && address - si->base < si->size) {
636fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return si;
637e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
638fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
639fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return NULL;
640e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
641e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
6420266ae5f884d72da58f33a072e865ba131234a5eElliott HughesElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr) {
6430266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - si->base;
644fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
645fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // Search the library's symbol table for any defined symbol which
646fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // contains this address.
647fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (size_t i = 0; i < si->nchain; ++i) {
6480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* sym = &si->symtab[i];
649fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (sym->st_shndx != SHN_UNDEF &&
650fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr >= sym->st_value &&
651fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr < sym->st_value + sym->st_size) {
652fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return sym;
653e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
654fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
655e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
656fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return NULL;
657e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
658e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
659124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library_on_path(const char* name, const char* const paths[]) {
660124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  char buf[512];
661124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  for (size_t i = 0; paths[i] != NULL; ++i) {
6621e980b6bc8315d00a07312b25486531247abd98cElliott Hughes    int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
663124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
664ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes      PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
665124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      continue;
6661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
667124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
668124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (fd != -1) {
669124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      return fd;
670124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
671124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
672124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return -1;
6731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
675124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library(const char* name) {
676ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ opening %s ]", name);
6771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
678124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // If the name contains a slash, we should attempt to open it directly and not search the paths.
679124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (strchr(name, '/') != NULL) {
6806971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
6816971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    if (fd != -1) {
6826971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes      return fd;
6836971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    }
6846971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
6855ca7ed9005ea16733d7c87d7154473b7a500be0cDmitriy Ivanov#if defined(__LP64__)
686e43c4a7a665032a29cb5ec15d4adbf81ea199220Dmitriy Ivanov    return -1;
6875ca7ed9005ea16733d7c87d7154473b7a500be0cDmitriy Ivanov#endif
688124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
6891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
690124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
6911728b2396591853345507a063ed6075dfd251706Elliott Hughes  int fd = open_library_on_path(name, g_ld_library_paths);
692124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (fd == -1) {
6931728b2396591853345507a063ed6075dfd251706Elliott Hughes    fd = open_library_on_path(name, kDefaultLdPaths);
694124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
695124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return fd;
6961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
698b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic soinfo* load_library(const char* name, int dlflags, const android_dlextinfo* extinfo) {
69904dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov    int fd = -1;
70004dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov    ScopedFd file_guard(-1);
70104dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov
70204dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov    if (extinfo != NULL && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
70304dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov      fd = extinfo->library_fd;
70404dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov    } else {
70504dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov      // Open the file.
70604dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov      fd = open_library(name);
70704dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov      if (fd == -1) {
7084688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("library \"%s\" not found", name);
7091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
71004dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov      }
71104dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov
71204dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov      file_guard.reset(fd);
7132e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    }
7141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
715650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    ElfReader elf_reader(name, fd);
716d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
717d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    struct stat file_stat;
718d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
719d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      DL_ERR("unable to stat file for the library %s: %s", name, strerror(errno));
720d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      return NULL;
721d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    }
722d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
723d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // Check for symlink and other situations where
724d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // file can have different names.
725d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    for (soinfo* si = solist; si != NULL; si = si->next) {
726d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      if (si->get_st_dev() != 0 &&
727d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          si->get_st_ino() != 0 &&
728d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          si->get_st_dev() == file_stat.st_dev &&
729d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          si->get_st_ino() == file_stat.st_ino) {
730d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
731d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        return si;
732d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      }
733d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    }
734d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
735b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    if ((dlflags & RTLD_NOLOAD) != 0) {
736b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov      return NULL;
737b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    }
738b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov
739d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // Read the ELF header and load the segments.
74012bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)    if (!elf_reader.Load(extinfo)) {
7414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
74223363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
74323363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner
744489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov    soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat);
745650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    if (si == NULL) {
7464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
74723363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
748650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->base = elf_reader.load_start();
749650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->size = elf_reader.load_size();
750650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->load_bias = elf_reader.load_bias();
751650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phnum = elf_reader.phdr_count();
752650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phdr = elf_reader.loaded_phdr();
753d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
754d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // At this point we know that whatever is loaded @ base is a valid ELF
755d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // shared library whose segments are properly mapped in.
75604dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov    TRACE("[ load_library base=%p size=%zu name='%s' ]",
757d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          reinterpret_cast<void*>(si->base), si->size, si->name);
758d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
759d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    if (!soinfo_link_image(si, extinfo)) {
760d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      soinfo_free(si);
761d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      return NULL;
762d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    }
763d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
764650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    return si;
7651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
767489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanovstatic soinfo *find_loaded_library_by_name(const char* name) {
768489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  const char* search_name = SEARCH_NAME(name);
769489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  for (soinfo* si = solist; si != NULL; si = si->next) {
770489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov    if (!strcmp(search_name, si->name)) {
771489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov      return si;
77212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    }
773489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  }
774489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  return NULL;
77512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel}
77612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
777b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic soinfo* find_library_internal(const char* name, int dlflags, const android_dlextinfo* extinfo) {
778d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (name == NULL) {
779d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return somain;
780d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
781d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
782489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  soinfo* si = find_loaded_library_by_name(name);
783b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov
784b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  // Library might still be loaded, the accurate detection
785b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  // of this fact is done by load_library
786b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  if (si == NULL) {
787b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    TRACE("[ '%s' has not been found by name.  Trying harder...]", name);
788b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    si = load_library(name, dlflags, extinfo);
789b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  }
790b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov
791b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  if (si != NULL && (si->flags & FLAG_LINKED) == 0) {
792b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    DL_ERR("recursive link to \"%s\"", si->name);
793d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return NULL;
794d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
79512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
796b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  return si;
797d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
798d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
799b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic soinfo* find_library(const char* name, int dlflags, const android_dlextinfo* extinfo) {
800b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  soinfo* si = find_library_internal(name, dlflags, extinfo);
801d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
802ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count++;
803d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
804d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
805d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
806d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
807b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic void soinfo_unload(soinfo* si) {
808ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  if (si->ref_count == 1) {
809ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("unloading '%s'", si->name);
810d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallDestructors();
811d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
812d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    if ((si->flags | FLAG_NEW_SOINFO) != 0) {
813d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      si->get_children().for_each([&] (soinfo* child) {
814d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        TRACE("%s needs to unload %s", si->name, child->name);
815d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        soinfo_unload(child);
816d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      });
817d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    } else {
818d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
819d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        if (d->d_tag == DT_NEEDED) {
820d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          const char* library_name = si->strtab + d->d_un.d_val;
821d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          TRACE("%s needs to unload %s", si->name, library_name);
822b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov          soinfo* needed = find_library(library_name, RTLD_NOLOAD, NULL);
823b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov          if (needed != NULL) {
824b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            soinfo_unload(needed);
825b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov          } else {
826b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            // Not found: for example if symlink was deleted between dlopen and dlclose
827b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            // Since we cannot really handle errors at this point - print and continue.
828b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
829b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov          }
830d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        }
831d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
8321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
834d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    notify_gdb_of_unload(si);
835ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 0;
836d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov    soinfo_free(si);
837d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  } else {
838ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count--;
839c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
840d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
8411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
843a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughesvoid do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
8441728b2396591853345507a063ed6075dfd251706Elliott Hughes  snprintf(buffer, buffer_size, "%s:%s", kDefaultLdPaths[0], kDefaultLdPaths[1]);
845a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes}
846a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes
847cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesvoid do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
848cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (!get_AT_SECURE()) {
849cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    parse_LD_LIBRARY_PATH(ld_library_path);
850cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
851cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
852cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
8531a586293400d0e1d73e6eb82f7dfe9d2d9ed3c4bElliott Hughessoinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) {
854b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NOLOAD)) != 0) {
855e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    DL_ERR("invalid flags to dlopen: %x", flags);
856e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    return NULL;
857e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes  }
858012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)  if (extinfo != NULL && ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0)) {
859d38306694179962f2d784fd1828afd4af1d8a4ecDmitriy Ivanov    DL_ERR("invalid extended flags to android_dlopen_ext: %x", extinfo->flags);
860012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)    return NULL;
861012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)  }
862d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ | PROT_WRITE);
863b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  soinfo* si = find_library(name, flags, extinfo);
864d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
865d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
866d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
867d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ);
868d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
869d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
8701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
871b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovvoid do_dlclose(soinfo* si) {
872d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ | PROT_WRITE);
873b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  soinfo_unload(si);
874d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ);
8751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8774eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
8789918665a45095ad135576f005c0e5307feb366a1Chris Dearmanstatic int soinfo_relocate(soinfo* si, ElfW(Rela)* rela, unsigned count, soinfo* needed[]) {
8790266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* s;
880c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  soinfo* lsi;
881c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
882c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  for (size_t idx = 0; idx < count; ++idx, ++rela) {
8830266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    unsigned type = ELFW(R_TYPE)(rela->r_info);
8840266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    unsigned sym = ELFW(R_SYM)(rela->r_info);
8850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + si->load_bias);
8860266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Addr) sym_addr = 0;
887faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    const char* sym_name = NULL;
888c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
889c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
890c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (type == 0) { // R_*_NONE
891c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      continue;
892c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
893c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (sym != 0) {
894c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes      sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
895c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      s = soinfo_do_lookup(si, sym_name, &lsi, needed);
896c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      if (s == NULL) {
897c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        // We only allow an undefined symbol if this is a weak reference...
898c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes        s = &si->symtab[sym];
899c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
900c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
901c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          return -1;
902c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
903c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
904c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        /* IHI0044C AAELF 4.5.1.1:
905c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
906c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           Libraries are not searched to resolve weak references.
907c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           It is not an error for a weak reference to remain unsatisfied.
908c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
909c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           During linking, the value of an undefined weak reference is:
910c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - Zero if the relocation type is absolute
911c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - The address of the place if the relocation is pc-relative
912c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - The address of nominal base address if the relocation
913c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes             type is base-relative.
914c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         */
915c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
916c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (type) {
917e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__)
918e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_JUMP_SLOT:
919e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_GLOB_DAT:
920e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS64:
921e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS32:
922e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS16:
923e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_RELATIVE:
924e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland          /*
925e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * The sym_addr was initialized to be zero above, or the relocation
926e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * code below does not care about value of sym_addr.
927e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * No need to do anything.
928e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           */
929e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland          break;
930e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__)
931c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_JUMP_SLOT:
932c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_GLOB_DAT:
933c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_32:
934bb5bb2046fa0759d94995cbfb4f41137cd0b2a91Pavel Chupin        case R_X86_64_64:
935c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_RELATIVE:
936c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          // No need to do anything.
937c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          break;
938c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_PC32:
939c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          sym_addr = reloc;
940c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          break;
9414eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
942c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        default:
943faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes          DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx);
944c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          return -1;
945c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
946c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      } else {
947c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        // We got a definition.
9480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
949c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      }
950c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocSymbol);
951c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    } else {
952c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      s = NULL;
953c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
954c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
955c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    switch (type) {
956e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__)
957e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_JUMP_SLOT:
958e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
959e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n",
9610266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9620266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
963e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
964e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_GLOB_DAT:
965e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
966e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9670266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n",
9680266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9690266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
970e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
971e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS64:
972e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
973e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9740266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
9750266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9760266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
977e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
978e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS32:
979e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
980e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9810266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
9820266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9830266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
9840266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
9850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
986e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
9870266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
9880266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
9890266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT32_MIN),
9900266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT32_MAX));
991e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
992e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
993e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
994e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS16:
995e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
996e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9970266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
9980266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9990266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
10000266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
10010266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
1002e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
10030266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
10040266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
10050266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT16_MIN),
10060266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT16_MAX));
1007e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1008e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
1009e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1010e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL64:
1011e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
1012e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
10130266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
10140266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
10150266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
1016e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1017e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL32:
1018e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
1019e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
10200266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
10210266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
10220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
10230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
10240266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
1025e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
10260266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
10270266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
10280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT32_MIN),
10290266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT32_MAX));
1030e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1031e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
1032e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1033e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL16:
1034e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
1035e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
10360266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
10370266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
10380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
10390266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
10400266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
1041e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
10420266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
10430266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
10440266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT16_MIN),
10450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT16_MAX));
1046e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1047e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
1048e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1049e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1050e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_RELATIVE:
1051e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
1052e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
1053e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        if (sym) {
1054e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            DL_ERR("odd RELATIVE form...");
1055e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1056e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
10570266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n",
10580266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (si->base + rela->r_addend));
10590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (si->base + rela->r_addend);
1060e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1061e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1062e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_COPY:
106376e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich        /*
106476e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         * ET_EXEC is not supported so this should not happen.
106576e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         *
106676e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
106776e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         *
106876e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         * Section 4.7.1.10 "Dynamic relocations"
106976e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         * R_AARCH64_COPY may only appear in executable objects where e_type is
107076e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         * set to ET_EXEC.
107176e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich         */
107276e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich        DL_ERR("%s R_AARCH64_COPY relocations are not supported", si->name);
107376e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich        return -1;
1074e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_TLS_TPREL64:
10750266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
10760266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset);
1077e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1078e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_TLS_DTPREL32:
10790266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
10800266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset);
1081e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1082e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__)
1083c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_JUMP_SLOT:
1084c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocAbsolute);
1085c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1086c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1087c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
10880266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1089c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1090c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_GLOB_DAT:
1091c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocAbsolute);
1092c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1093c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1094c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
10950266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1096c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1097c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_RELATIVE:
1098c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1099c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1100c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      if (sym) {
1101c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DL_ERR("odd RELATIVE form...");
1102c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        return -1;
1103c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      }
1104c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
1105c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(si->base));
11060266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = si->base + rela->r_addend;
1107c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1108c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_32:
1109c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1110c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1111c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1112c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr), sym_name);
11130266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1114c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1115c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin    case R_X86_64_64:
1116c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      count_relocation(kRelocRelative);
1117c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      MARK(rela->r_offset);
1118c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1119c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin                 static_cast<size_t>(sym_addr), sym_name);
11200266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1121c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      break;
1122c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_PC32:
1123c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1124c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1125c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1126c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1127c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
11280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc;
1129c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
11304eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1131e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1132c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    default:
1133faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes      DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx);
1134c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      return -1;
1135c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1136c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  }
1137c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  return 0;
1138c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes}
11399918665a45095ad135576f005c0e5307feb366a1Chris Dearman
11409918665a45095ad135576f005c0e5307feb366a1Chris Dearman#else // REL, not RELA.
11419918665a45095ad135576f005c0e5307feb366a1Chris Dearman
11420266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic int soinfo_relocate(soinfo* si, ElfW(Rel)* rel, unsigned count, soinfo* needed[]) {
11430266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s;
1144d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo* lsi;
11451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    for (size_t idx = 0; idx < count; ++idx, ++rel) {
11470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        unsigned type = ELFW(R_TYPE)(rel->r_info);
11480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead.
11490266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        unsigned sym = ELFW(R_SYM)(rel->r_info);
11500266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + si->load_bias);
11510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        ElfW(Addr) sym_addr = 0;
1152faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        const char* sym_name = NULL;
11531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1154c620059479c47a78d57086d73726c9adc2f337adElliott Hughes        DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
1155d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (type == 0) { // R_*_NONE
1156d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            continue;
1157d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1158d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (sym != 0) {
1159c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes            sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
11605ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1161d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (s == NULL) {
11624eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                // We only allow an undefined symbol if this is a weak reference...
1163c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes                s = &si->symtab[sym];
1164c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1165e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes                    DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
1166e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1167e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1168e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1169e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* IHI0044C AAELF 4.5.1.1:
1170e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1171e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   Libraries are not searched to resolve weak references.
1172e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   It is not an error for a weak reference to remain
1173e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   unsatisfied.
1174e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1175e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   During linking, the value of an undefined weak reference is:
1176e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - Zero if the relocation type is absolute
1177e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - The address of the place if the relocation is pc-relative
1178bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes                   - The address of nominal base address if the relocation
1179e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                     type is base-relative.
1180e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                  */
1181e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1182e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                switch (type) {
11834eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
1184e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_JUMP_SLOT:
1185e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_GLOB_DAT:
1186e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_ABS32:
1187e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_RELATIVE:    /* Don't care. */
11884eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // sym_addr was initialized to be zero above or relocation
11894eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // code below does not care about value of sym_addr.
11904eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // No need to do anything.
11914eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    break;
11924eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
1193d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                case R_386_JMP_SLOT:
1194e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_GLOB_DAT:
1195e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_32:
1196c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                case R_386_RELATIVE:    /* Don't care. */
11974eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // sym_addr was initialized to be zero above or relocation
11984eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // code below does not care about value of sym_addr.
11994eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // No need to do anything.
1200e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
1201e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_PC32:
1202e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    sym_addr = reloc;
1203e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
12044eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1205e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
12064eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
1207e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_COPY:
12084eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // Fall through. Can't really copy if weak symbol is not found at run-time.
12094eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1210e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                default:
1211faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                    DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
1212e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1213e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1214e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            } else {
12154eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                // We got a definition.
12160266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
1217d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1218bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocSymbol);
12191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        } else {
1220e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            s = NULL;
12211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
12221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1223c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (type) {
12244eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
12251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_JUMP_SLOT:
1226bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
12271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1228ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
12290266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
12301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
12311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_GLOB_DAT:
1232bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
12331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1234ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
12350266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
12361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
12371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_ABS32:
1238bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
12391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1240ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
12410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
12421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
124334ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner        case R_ARM_REL32:
1244bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
124534ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            MARK(rel->r_offset);
1246ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
124734ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner                       reloc, sym_addr, rel->r_offset, sym_name);
12480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
124934ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            break;
12501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_COPY:
125176e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich            /*
125276e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             * ET_EXEC is not supported so this should not happen.
125376e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             *
125476e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
125576e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             *
125676e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             * Section 4.7.1.10 "Dynamic relocations"
125776e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             * R_ARM_COPY may only appear in executable objects where e_type is
125876e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             * set to ET_EXEC.
125976e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich             */
126076e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich            DL_ERR("%s R_ARM_COPY relocations are not supported", si->name);
126176e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich            return -1;
12624eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
12634eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_JMP_SLOT:
12644eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
12654eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
12664eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
12670266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
12684eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
12694eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_GLOB_DAT:
12704eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
12714eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
12724eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
12730266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
12744eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
12754eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_32:
12764eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
12774eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
12784eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
12790266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
12804eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
12814eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_PC32:
12824eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
12834eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
12844eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
12854eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                       reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
12860266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
12874eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
12884eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__mips__)
12894eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_MIPS_REL32:
12909918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__LP64__)
12919918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // MIPS Elf64_Rel entries contain compound relocations
12929918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
12939918665a45095ad135576f005c0e5307feb366a1Chris Dearman            if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
12949918665a45095ad135576f005c0e5307feb366a1Chris Dearman                ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
1295faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
12969918665a45095ad135576f005c0e5307feb366a1Chris Dearman                       type, (unsigned)ELF64_R_TYPE2(rel->r_info),
1297faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                       (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx);
12989918665a45095ad135576f005c0e5307feb366a1Chris Dearman                return -1;
12999918665a45095ad135576f005c0e5307feb366a1Chris Dearman            }
13009918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif
13014eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
13024eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
13039918665a45095ad135576f005c0e5307feb366a1Chris Dearman            TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
13049918665a45095ad135576f005c0e5307feb366a1Chris Dearman                       static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
13054eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            if (s) {
13060266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
13074eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            } else {
13080266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base;
13094eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            }
13104eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
13114eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
13124eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes
13134eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
13144eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_ARM_RELATIVE:
13154eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
13164eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_RELATIVE:
13174eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
13184eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
13194eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
13204eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            if (sym) {
13214eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                DL_ERR("odd RELATIVE form...");
13224eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                return -1;
13234eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            }
13244eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
13254eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                       reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base));
13260266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base;
13274eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
13281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        default:
1330faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
13311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return -1;
13321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
13331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
13351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
1336c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
13371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13384eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
133987c358524e479235aa6241736d2ce325f89daafcBrian Carlstromstatic bool mips_relocate_got(soinfo* si, soinfo* needed[]) {
13409918665a45095ad135576f005c0e5307feb366a1Chris Dearman    ElfW(Addr)** got = si->plt_got;
134187c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    if (got == NULL) {
134287c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom        return true;
134387c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    }
134487c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned local_gotno = si->mips_local_gotno;
134587c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned gotsym = si->mips_gotsym;
134687c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned symtabno = si->mips_symtabno;
13470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* symtab = si->symtab;
1348d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
13499918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // got[0] is the address of the lazy resolver function.
13509918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // got[1] may be used for a GNU extension.
13519918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
13529918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // FIXME: maybe this should be in a separate routine?
1353d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if ((si->flags & FLAG_LINKER) == 0) {
135487c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom        size_t g = 0;
13559918665a45095ad135576f005c0e5307feb366a1Chris Dearman        got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
13569918665a45095ad135576f005c0e5307feb366a1Chris Dearman        if (reinterpret_cast<intptr_t>(got[g]) < 0) {
13579918665a45095ad135576f005c0e5307feb366a1Chris Dearman            got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
1358d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
13599918665a45095ad135576f005c0e5307feb366a1Chris Dearman        // Relocate the local GOT entries.
1360d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        for (; g < local_gotno; g++) {
13619918665a45095ad135576f005c0e5307feb366a1Chris Dearman            got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + si->load_bias);
1362d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1363d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1364d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
13659918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // Now for the global GOT entries...
13660266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* sym = symtab + gotsym;
1367d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got + local_gotno;
136887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
13699918665a45095ad135576f005c0e5307feb366a1Chris Dearman        // This is an undefined reference... try to locate it.
13709918665a45095ad135576f005c0e5307feb366a1Chris Dearman        const char* sym_name = si->strtab + sym->st_name;
1371d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        soinfo* lsi;
13729918665a45095ad135576f005c0e5307feb366a1Chris Dearman        ElfW(Sym)* s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1373d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (s == NULL) {
13749918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // We only allow an undefined symbol if this is a weak reference.
1375d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            s = &symtab[g];
1376c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
13774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("cannot locate \"%s\"...", sym_name);
137887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom                return false;
1379d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1380d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *got = 0;
13819918665a45095ad135576f005c0e5307feb366a1Chris Dearman        } else {
13829918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // FIXME: is this sufficient?
13839918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // For reference see NetBSD link loader
13849918665a45095ad135576f005c0e5307feb366a1Chris 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
13859918665a45095ad135576f005c0e5307feb366a1Chris Dearman            *got = reinterpret_cast<ElfW(Addr)*>(lsi->load_bias + s->st_value);
1386d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1387d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
138887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    return true;
1389d7daacb46372132ae3f0121647074936c304b572Raghu Gandham}
1390d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1391d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1392812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallArray(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
1393ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  if (functions == NULL) {
1394d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1395d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
13968215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1397c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
13988215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1399ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int begin = reverse ? (count - 1) : 0;
1400ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int end = reverse ? -1 : count;
1401ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int step = reverse ? -1 : 1;
14028215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1403ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  for (int i = begin; i != end; i += step) {
1404ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1405ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    CallFunction("function", functions[i]);
1406d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1407d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1408ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s for '%s' ]", array_name, name);
14091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1411812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallFunction(const char* function_name __unused, linker_function_t function) {
1412db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
1413d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1414d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1415d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1416ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
1417d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  function();
1418ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
1419db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes
1420db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1421db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // are still writable. This happens with our debug malloc (see http://b/7941716).
1422d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ | PROT_WRITE);
14239181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov}
14249181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
1425d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallPreInitConstructors() {
14268147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
14278147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // but ignored in a shared library.
1428d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1429d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
1430e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1431d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallConstructors() {
1432d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (constructors_called) {
1433d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1434d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1435e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1436d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // We set constructors_called before actually calling the constructors, otherwise it doesn't
1437d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // protect against recursive constructor calls. One simple example of constructor recursion
1438d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1439d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 1. The program depends on libc, so libc's constructor is called here.
1440d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1441d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 3. dlopen() calls the constructors on the newly created
1442d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    soinfo for libc_malloc_debug_leak.so.
1443d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 4. The debug .so depends on libc, so CallConstructors is
1444d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    called again with the libc soinfo. If it doesn't trigger the early-
1445d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    out above, the libc constructor will be called again (recursively!).
1446d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  constructors_called = true;
1447d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
14488147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) {
14498147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes    // The GNU dynamic linker silently ignores these, but we warn the developer.
1450c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
14518147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes          name, preinit_array_count);
1452d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
14531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1454d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  get_children().for_each([] (soinfo* si) {
1455d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    si->CallConstructors();
1456d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  });
14571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14588147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  TRACE("\"%s\": calling constructors", name);
14598147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
14608147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_INIT should be called before DT_INIT_ARRAY if both are present.
1461d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_INIT", init_func);
1462d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
1463e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov}
14648215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1465d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallDestructors() {
14668147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  TRACE("\"%s\": calling destructors", name);
14678147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
14688147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_FINI_ARRAY must be parsed in reverse order.
1469d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
14708147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
14718147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_FINI should be called after DT_FINI_ARRAY if both are present.
1472d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_FINI", fini_func);
1473b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov
1474b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  // This is needed on second call to dlopen
1475b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  // after library has been unloaded with RTLD_NODELETE
1476b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  constructors_called = false;
14771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1479d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::add_child(soinfo* child) {
1480d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1481d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return;
1482d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1483d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1484d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  this->children.push_front(child);
1485d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  child->parents.push_front(this);
1486d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1487d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1488d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::remove_all_links() {
1489d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1490d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return;
1491d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1492d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1493d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  // 1. Untie connected soinfos from 'this'.
1494d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  children.for_each([&] (soinfo* child) {
1495d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    child->parents.remove_if([&] (const soinfo* parent) {
1496d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      return parent == this;
1497d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    });
1498d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  });
1499d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1500d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  parents.for_each([&] (soinfo* parent) {
1501d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    parent->children.for_each([&] (const soinfo* child) {
1502d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      return child == this;
1503d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    });
1504d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  });
1505d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1506d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  // 2. Once everything untied - clear local lists.
1507d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  parents.clear();
1508d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  children.clear();
1509d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1510d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1511d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::set_st_dev(dev_t dev) {
1512d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1513d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return;
1514d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1515d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1516d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  st_dev = dev;
1517d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1518d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1519d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::set_st_ino(ino_t ino) {
1520d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1521d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return;
1522d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1523d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1524d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  st_ino = ino;
1525d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1526d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1527d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovdev_t soinfo::get_st_dev() {
1528d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1529d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return 0;
1530d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1531d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1532d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  return st_dev;
1533d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov};
1534d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1535d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovino_t soinfo::get_st_ino() {
1536d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1537d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return 0;
1538d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1539d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1540d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  return st_ino;
1541d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1542d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1543d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov// This is a return on get_children() in case
1544d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov// 'this->flags' does not have FLAG_NEW_SOINFO set.
1545d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo::soinfo_list_t g_empty_list;
1546d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1547d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovsoinfo::soinfo_list_t& soinfo::get_children() {
1548d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1549d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return g_empty_list;
1550d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1551d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1552d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  return this->children;
1553d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1554d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
15551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with
15561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   /dev/null. */
15575419b9474753d25dff947c7740532f86d130c0beElliott Hughesstatic int nullify_closed_stdio() {
15581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int dev_null, i, status;
15591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int return_value = 0;
15601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
156116084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
15621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null < 0) {
15634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("cannot open /dev/null: %s", strerror(errno));
15641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return -1;
15651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1566ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
15671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If any of the stdio file descriptors is valid and not associated
15691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       with /dev/null, dup /dev/null to it.  */
15701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (i = 0; i < 3; i++) {
15711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* If it is /dev/null already, we are done. */
15724688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (i == dev_null) {
15731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
15744688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
15751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1576ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Nullifying stdio file descriptor %d]", i);
15774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
15781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        /* If file is opened, we are good. */
15804688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status != -1) {
15814688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            continue;
15824688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
15831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* The only error we allow is that the file descriptor does not
15851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           exist, in which case we dup /dev/null to it. */
15861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (errno != EBADF) {
15874688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("fcntl failed: %s", strerror(errno));
15881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
15891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
15901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
15911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* Try dupping /dev/null to this stdio file descriptor and
15931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           repeat if there is a signal.  Note that any errors in closing
15941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           the stdio descriptor are lost.  */
15954688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
15961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (status < 0) {
15974688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("dup2 failed: %s", strerror(errno));
15981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
15991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
16001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
16011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If /dev/null is not one of the stdio file descriptors, close it. */
16041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null > 2) {
1605ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
16064688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(close(dev_null));
16074688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status == -1) {
16084688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("close failed: %s", strerror(errno));
16091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
16101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
16111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return return_value;
16141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
16151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1616183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo) {
1617f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee    /* "base" might wrap around UINT32_MAX. */
16180266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Addr) base = si->load_bias;
16190266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    const ElfW(Phdr)* phdr = si->phdr;
16201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int phnum = si->phnum;
1621d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
16221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1623b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can't debug anything until the linker is relocated */
1624b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (!relocating_linker) {
1625ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        INFO("[ linking %s ]", si->name);
1626c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags);
1627b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
16281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
162963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    /* Extract dynamic section */
1630124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    size_t dynamic_count;
16310266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Word) dynamic_flags;
163212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
1633cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman                                   &dynamic_count, &dynamic_flags);
163463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    if (si->dynamic == NULL) {
1635b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1636124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
1637b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
1638124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
163963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    } else {
1640b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1641ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("dynamic = %p", si->dynamic);
1642b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
164363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    }
164463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
16454eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
164663f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    (void) phdr_table_get_arm_exidx(phdr, phnum, base,
164763f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner                                    &si->ARM_exidx, &si->ARM_exidx_count);
164863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif
164963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
16508147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes    // Extract useful information from dynamic section.
1651d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    uint32_t needed_count = 0;
16520266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1653c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1654c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes              d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1655c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (d->d_tag) {
16561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_HASH:
1657faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->nbucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[0];
1658faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->nchain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[1];
1659faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->bucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8);
1660faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->chain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8 + si->nbucket * 4);
16611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_STRTAB:
1663faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->strtab = reinterpret_cast<const char*>(base + d->d_un.d_ptr);
16641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_SYMTAB:
1666faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->symtab = reinterpret_cast<ElfW(Sym)*>(base + d->d_un.d_ptr);
16671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16684eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if !defined(__LP64__)
16691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTREL:
1670d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val != DT_REL) {
1671124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1672124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
16731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
16741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1675c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
16761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_JMPREL:
16774eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1678faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->plt_rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr);
1679c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1680faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->plt_rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr);
1681c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
16821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTRELSZ:
16844eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
16850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->plt_rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
1686c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
16870266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->plt_rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
1688c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
16891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16909918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__mips__)
16911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTGOT:
16929918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // Used by mips and mips64.
16939918665a45095ad135576f005c0e5307feb366a1Chris Dearman            si->plt_got = reinterpret_cast<ElfW(Addr)**>(base + d->d_un.d_ptr);
16941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1695c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
16961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_DEBUG:
1697bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_DEBUG entry to the address of _r_debug for GDB
1698cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman            // if the dynamic table is writable
16999918665a45095ad135576f005c0e5307feb366a1Chris Dearman// FIXME: not working currently for N64
17009918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The flags for the LOAD and DYNAMIC program headers do not agree.
17019918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The LOAD section containng the dynamic table has been mapped as
17029918665a45095ad135576f005c0e5307feb366a1Chris Dearman// read-only, but the DYNAMIC header claims it is writable.
17039918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if !(defined(__mips__) && defined(__LP64__))
170499c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            if ((dynamic_flags & PF_W) != 0) {
1705c620059479c47a78d57086d73726c9adc2f337adElliott Hughes                d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
170699c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            }
17071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17089918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif
17094eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1710c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         case DT_RELA:
1711faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr);
1712c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1713c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         case DT_RELASZ:
17140266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
1715c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1716c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_REL:
1717c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            DL_ERR("unsupported DT_REL in \"%s\"", si->name);
1718c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1719c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_RELSZ:
1720c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name);
1721c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1722c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1723c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_REL:
1724faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr);
1725c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1726c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_RELSZ:
17270266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
1728c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1729ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI         case DT_RELA:
1730124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1731124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1732c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
17331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT:
1734ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
17358147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func);
17361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI:
1738ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
17398147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func);
17401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAY:
1742ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
17438147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array);
17441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAYSZ:
17460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
17471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAY:
1749ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
17508147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array);
17511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAYSZ:
17530266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
17541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAY:
1756ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
17578147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array);
17581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAYSZ:
17600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
17611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_TEXTREL:
1763e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__)
1764e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes            DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1765e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes            return false;
1766e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else
17675135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            si->has_text_relocations = true;
17681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1769e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
17705ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_SYMBOLIC:
17715ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            si->has_DT_SYMBOLIC = true;
17725ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
1773d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        case DT_NEEDED:
1774d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            ++needed_count;
1775d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            break;
17765ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_FLAGS:
1777d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_TEXTREL) {
1778e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__)
1779e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes                DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1780e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes                return false;
1781e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else
17825ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_text_relocations = true;
1783e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
17845ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
1785d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_SYMBOLIC) {
17865ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_DT_SYMBOLIC = true;
17875ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
17885ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
17894eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
1790d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_STRSZ:
1791d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_SYMENT:
1792d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_RELENT:
1793d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             break;
1794d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_MAP:
1795bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
1796d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            {
1797006f9ad8910c945cd50e54f6c34d8c27b11c1193Benjamin Adolphi              r_debug** dp = reinterpret_cast<r_debug**>(base + d->d_un.d_ptr);
1798d7daacb46372132ae3f0121647074936c304b572Raghu Gandham              *dp = &_r_debug;
1799d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1800d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1801d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_VERSION:
1802d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_FLAGS:
1803d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_BASE_ADDRESS:
1804d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_UNREFEXTNO:
1805d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1806d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1807d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_SYMTABNO:
1808d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_symtabno = d->d_un.d_val;
1809d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1810d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1811d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_LOCAL_GOTNO:
1812d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_local_gotno = d->d_un.d_val;
1813d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1814d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1815d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_GOTSYM:
1816d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_gotsym = d->d_un.d_val;
1817d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
18184eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1819d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1820d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        default:
18214eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            DEBUG("Unused DT entry: type %p arg %p",
18224eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                  reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1823d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
18241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
18251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1827c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
1828c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          reinterpret_cast<void*>(si->base), si->strtab, si->symtab);
18291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1830124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    // Sanity checks.
1831d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (relocating_linker && needed_count != 0) {
1832d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1833d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        return false;
1834d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
1835124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->nbucket == 0) {
1836124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1837124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1838124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1839124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->strtab == 0) {
1840124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1841124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1842124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1843124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->symtab == 0) {
1844124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1845124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
18461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18487e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes    // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
1849d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_EXE) {
18501728b2396591853345507a063ed6075dfd251706Elliott Hughes        memset(g_ld_preloads, 0, sizeof(g_ld_preloads));
18517e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes        size_t preload_count = 0;
18521728b2396591853345507a063ed6075dfd251706Elliott Hughes        for (size_t i = 0; g_ld_preload_names[i] != NULL; i++) {
1853b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            soinfo* lsi = find_library(g_ld_preload_names[i], 0, NULL);
18547e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes            if (lsi != NULL) {
18551728b2396591853345507a063ed6075dfd251706Elliott Hughes                g_ld_preloads[preload_count++] = lsi;
18567e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes            } else {
18577e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                // As with glibc, failure to load an LD_PRELOAD library is just a warning.
18587e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
18591728b2396591853345507a063ed6075dfd251706Elliott Hughes                        g_ld_preload_names[i], si->name, linker_get_error_buffer());
18604fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            }
18614fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        }
18624fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
18634fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1864faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    soinfo** needed = reinterpret_cast<soinfo**>(alloca((1 + needed_count) * sizeof(soinfo*)));
1865d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo** pneeded = needed;
186612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
18670266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1868d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (d->d_tag == DT_NEEDED) {
1869d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            const char* library_name = si->strtab + d->d_un.d_val;
1870ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("%s needs %s", si->name, library_name);
1871b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            soinfo* lsi = find_library(library_name, 0, NULL);
1872d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            if (lsi == NULL) {
1873650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes                strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
18744688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1875d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                       library_name, si->name, tmp_err_buf);
1876124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
18771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
1878d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1879d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov            si->add_child(lsi);
188012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            *pneeded++ = lsi;
18811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
18821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
188312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    *pneeded = NULL;
18841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1885e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__)
18865135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
1887e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // Make segments writable to allow text relocations to work properly. We will later call
1888e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // phdr_table_protect_segments() after all of them are applied and all constructors are run.
18893d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich        DL_WARN("%s has text relocations. This is wasting memory and prevents "
18903d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich                "security hardening. Please fix.", si->name);
18915135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
18925135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't unprotect loadable segments for \"%s\": %s",
18935135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1894124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
18955135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
18965135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    }
1897e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
18985135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich
18994eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1900c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (si->plt_rela != NULL) {
1901faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s plt ]\n", si->name);
19029918665a45095ad135576f005c0e5307feb366a1Chris Dearman        if (soinfo_relocate(si, si->plt_rela, si->plt_rela_count, needed)) {
1903c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1904c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
1905c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1906c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (si->rela != NULL) {
1907faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s ]\n", si->name);
19089918665a45095ad135576f005c0e5307feb366a1Chris Dearman        if (soinfo_relocate(si, si->rela, si->rela_count, needed)) {
1909c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1910c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
1911c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1912c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1913d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si->plt_rel != NULL) {
1914faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s plt ]", si->name);
1915d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1916124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1917124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
19181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1919d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si->rel != NULL) {
1920faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s ]", si->name);
1921d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1922124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1923124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
19241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1925c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
19261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19274eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
192887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    if (!mips_relocate_got(si, needed)) {
1929124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1930d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1931d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1932d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
19331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_LINKED;
1934ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    DEBUG("[ finished linking %s ]", si->name);
19351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1936e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__)
19375135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
1938e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // All relocations are done, we can protect our segments back to read-only.
19395135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
19405135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't protect segments for \"%s\": %s",
19415135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1942124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
19435135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
19441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1945e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
19461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1947b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can also turn on GNU RELRO protection */
1948b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
19494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
19504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               si->name, strerror(errno));
1951124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
19529ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich    }
19539ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
1954183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    /* Handle serializing/sharing the RELRO segment */
1955183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
1956183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      if (phdr_table_serialize_gnu_relro(si->phdr, si->phnum, si->load_bias,
1957183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)                                         extinfo->relro_fd) < 0) {
1958183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
1959183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)               si->name, strerror(errno));
1960183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        return false;
1961183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      }
1962183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
1963183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      if (phdr_table_map_gnu_relro(si->phdr, si->phnum, si->load_bias,
1964183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)                                   extinfo->relro_fd) < 0) {
1965183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
1966183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)               si->name, strerror(errno));
1967183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        return false;
1968183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      }
1969183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    }
1970183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)
19711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    notify_gdb_of_load(si);
1972124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    return true;
19731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
19741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1975468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1976c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * This function add vdso to internal dso list.
1977c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * It helps to stack unwinding through signal handlers.
1978c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * Also, it makes bionic more like glibc.
1979c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov */
1980812fd4263a005b88f3b4222baa910114f938d594Kito Chengstatic void add_vdso(KernelArgumentBlock& args __unused) {
19814eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(AT_SYSINFO_EHDR)
19820266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
19830266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  if (ehdr_vdso == NULL) {
19840266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    return;
19850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  }
1986c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
1987d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  soinfo* si = soinfo_alloc("[vdso]", NULL);
1988ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov
19890266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
19900266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->phnum = ehdr_vdso->e_phnum;
19910266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
19920266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->size = phdr_table_get_load_size(si->phdr, si->phnum);
19930266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
1994ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov
19950dcf06f976670b5d05286b184d150f61b71c1184Torne (Richard Coles)  soinfo_link_image(si, NULL);
1996c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov#endif
1997c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov}
1998c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
1999c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov/*
2000d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * This is linker soinfo for GDB. See details below.
2001d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov */
2002d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo linker_soinfo_for_gdb;
2003d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
2004d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov/* gdb expects the linker to be in the debug shared object list.
2005d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * Without this, gdb has trouble locating the linker's ".text"
2006d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * and ".plt" sections. Gdb could also potentially use this to
2007d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
2008d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * Don't use soinfo_alloc(), because the linker shouldn't
2009d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * be on the soinfo list.
2010d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov */
2011d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
2012d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov#if defined(__LP64__)
2013d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  strlcpy(linker_soinfo_for_gdb.name, "/system/bin/linker64", sizeof(linker_soinfo_for_gdb.name));
2014d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov#else
2015d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  strlcpy(linker_soinfo_for_gdb.name, "/system/bin/linker", sizeof(linker_soinfo_for_gdb.name));
2016d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov#endif
2017d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  linker_soinfo_for_gdb.flags = FLAG_NEW_SOINFO;
2018d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  linker_soinfo_for_gdb.base = linker_base;
2019d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
2020d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  /*
2021d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   * Set the dynamic field in the link map otherwise gdb will complain with
2022d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   * the following:
2023d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   *   warning: .dynamic section for "/system/bin/linker" is not at the
2024d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   *   expected address (wrong library or version mismatch?)
2025d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   */
2026d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2027d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2028d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
2029d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov                                 &linker_soinfo_for_gdb.dynamic, NULL, NULL);
2030d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2031d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
2032d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
2033d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov/*
2034468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and
2035468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs
2036468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point.
2037468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
20380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
203942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    /* NOTE: we store the args pointer on a special location
2040ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       of the temporary TLS area in order to pass it to
2041ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       the C Library's runtime initializer.
2042ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *
2043ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       The initializer must clear the slot and reset the TLS
2044ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       to point to a different location to ensure that no other
2045ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       shared library constructor can access it.
2046ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     */
2047d3920b3a996b358e48232f417aa0a1e44a60f155Elliott Hughes  __libc_init_tls(args);
20481a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov
20491a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING
20501a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    struct timeval t0, t1;
20511a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    gettimeofday(&t0, 0);
20521a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif
20531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
205418a206c81d9743481e364384affd43306911283dElliott Hughes    // Initialize environment functions, and get to the ELF aux vectors table.
205542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    linker_env_init(args);
2056be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
20578d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // If this is a setuid/setgid program, close the security hole described in
20588d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
20598d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    if (get_AT_SECURE()) {
20608d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich        nullify_closed_stdio();
20618d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    }
20628d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich
2063d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    debuggerd_init();
20641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
206518a206c81d9743481e364384affd43306911283dElliott Hughes    // Get a few environment variables.
206661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    const char* LD_DEBUG = linker_env_get("LD_DEBUG");
206761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    if (LD_DEBUG != NULL) {
20681728b2396591853345507a063ed6075dfd251706Elliott Hughes      g_ld_debug_verbosity = atoi(LD_DEBUG);
206918a206c81d9743481e364384affd43306911283dElliott Hughes    }
2070be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
207118a206c81d9743481e364384affd43306911283dElliott Hughes    // Normally, these are cleaned by linker_env_init, but the test
207218a206c81d9743481e364384affd43306911283dElliott Hughes    // doesn't cost us anything.
207318a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpath_env = NULL;
207418a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpreload_env = NULL;
207518a206c81d9743481e364384affd43306911283dElliott Hughes    if (!get_AT_SECURE()) {
207618a206c81d9743481e364384affd43306911283dElliott Hughes      ldpath_env = linker_env_get("LD_LIBRARY_PATH");
207718a206c81d9743481e364384affd43306911283dElliott Hughes      ldpreload_env = linker_env_get("LD_PRELOAD");
20781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
20791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2080d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov    // Linker does not call constructors for its own
2081d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov    // global variables so we need to initialize
2082d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // the allocators explicitly.
20831728b2396591853345507a063ed6075dfd251706Elliott Hughes    g_soinfo_allocator.init();
2084d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    g_soinfo_links_allocator.init();
2085d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov
2086ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    INFO("[ android linker & debugger ]");
20871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2088d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    soinfo* si = soinfo_alloc(args.argv[0], NULL);
208918a206c81d9743481e364384affd43306911283dElliott Hughes    if (si == NULL) {
209018a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
20911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
20921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2093d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    /* bootstrap the link map, the main exe always needs to be first */
20941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_EXE;
20953a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(si->link_map_head);
20961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
20971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = 0;
209842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    map->l_name = args.argv[0];
20991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_prev = NULL;
21001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_next = NULL;
21011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
21021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_map = map;
21031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
21041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2105d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    init_linker_info_for_gdb(linker_base);
21061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
210742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // Extract information passed from the kernel.
21080266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
210942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->phnum = args.getauxval(AT_PHNUM);
211042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->entry = args.getauxval(AT_ENTRY);
21111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
21128180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    /* Compute the value of si->base. We can't rely on the fact that
21138180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * the first entry is the PHDR because this will not be true
21148180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * for certain executables (e.g. some in the NDK unit test suite)
21158180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     */
21168180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    si->base = 0;
2117b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2118bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    si->load_bias = 0;
2119ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    for (size_t i = 0; i < si->phnum; ++i) {
212042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      if (si->phdr[i].p_type == PT_PHDR) {
21210266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
21220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
212342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes        break;
212442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      }
21258180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    }
2126d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    si->dynamic = NULL;
2127ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 1;
21281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
21292aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich    ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
21302aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich    if (elf_hdr->e_type != ET_DYN) {
21312aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich        __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
21322aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich        exit(EXIT_FAILURE);
21332aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich    }
21342aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich
21354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
21364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_LIBRARY_PATH(ldpath_env);
21374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_PRELOAD(ldpreload_env);
21384fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
21395ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    somain = si;
21405ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
2141183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    if (!soinfo_link_image(si, NULL)) {
2142650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes        __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
214318a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
21441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
21451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2146c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov    add_vdso(args);
2147c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
2148d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallPreInitConstructors();
21499181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
21501728b2396591853345507a063ed6075dfd251706Elliott Hughes    for (size_t i = 0; g_ld_preloads[i] != NULL; ++i) {
21511728b2396591853345507a063ed6075dfd251706Elliott Hughes        g_ld_preloads[i]->CallConstructors();
2152326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng    }
2153326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng
2154d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    /* After the link_image, the si->load_bias is initialized.
2155d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2156d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * We need to update this value for so exe here. So Unwind_Backtrace
2157d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * for some arch like x86 could work correctly within so exe.
21589c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     */
2159c5db969aa4a24a76fc0964f2ff89533e832b47a6Chao-Ying Fu    map->l_addr = si->load_bias;
2160d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
2161e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
21621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING
2163faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    gettimeofday(&t1, NULL);
2164ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
21651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2166faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes               (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
21671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
21681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
2169ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2170bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocAbsolute],
2171bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocRelative],
2172bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocCopy],
2173bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocSymbol]);
21741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
21751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
21761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
21771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned n;
21781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned i;
21791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned count = 0;
2180d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        for (n = 0; n < 4096; n++) {
2181d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (bitmask[n]) {
21821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                unsigned x = bitmask[n];
2183e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__LP64__)
2184e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland                for (i = 0; i < 32; i++) {
2185e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#else
2186d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                for (i = 0; i < 8; i++) {
2187e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#endif
2188d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    if (x & 1) {
2189d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                        count++;
2190d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    }
21911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    x >>= 1;
21921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                }
21931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
21941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
2195ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
21961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
21971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
21981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
21991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES
22001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    fflush(stdout);
22011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
22021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2203c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
22041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si->entry;
22051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2206468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2207bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only
2208bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library
2209bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself.
2210bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *
2211bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input:
2212bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    elf    -> address of ELF header, assumed to be at the start of the file.
2213bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return:
2214bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    load bias, i.e. add the value of any p_vaddr in the file to get
2215bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    the corresponding address in memory.
2216bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */
22170266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
22180266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) offset = elf->e_phoff;
2219faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes  const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
22200266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
2221fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
22220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
2223fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (phdr->p_type == PT_LOAD) {
22240266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
2225bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    }
2226fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
2227fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return 0;
2228bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner}
2229bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
2230468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
2231468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This
2232468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and
2233468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation().
2234468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich *
2235468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own
2236468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern
2237468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault.
2238468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
22390266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesextern "C" ElfW(Addr) __linker_init(void* raw_args) {
2240d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  // Initialize static variables.
2241d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  solist = get_libdl_info();
2242d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  sonext = get_libdl_info();
2243d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
224442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  KernelArgumentBlock args(raw_args);
224542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
22460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
22470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
2248faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes  ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
224942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
225042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  soinfo linker_so;
225142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  memset(&linker_so, 0, sizeof(soinfo));
225242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
2253b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes  strcpy(linker_so.name, "[dynamic linker]");
225442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.base = linker_addr;
225542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
225642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
2257d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom  linker_so.dynamic = NULL;
225842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phdr = phdr;
225942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phnum = elf_hdr->e_phnum;
226042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.flags |= FLAG_LINKER;
226142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
2262183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)  if (!soinfo_link_image(&linker_so, NULL)) {
226342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // It would be nice to print an error message, but if the linker
226442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // can't link itself, there's no guarantee that we'll be able to
2265b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    // call write() (because it involves a GOT reference). We may as
2266b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    // well try though...
2267b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    const char* msg = "CANNOT LINK EXECUTABLE: ";
2268b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, msg, strlen(msg));
2269b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2270b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, "\n", 1);
2271b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    _exit(EXIT_FAILURE);
227242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  }
2273468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
227442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // We have successfully fixed our own relocations. It's safe to run
227542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // the main part of the linker now.
22761728b2396591853345507a063ed6075dfd251706Elliott Hughes  args.abort_message_ptr = &g_abort_message;
22770266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
22785419b9474753d25dff947c7740532f86d130c0beElliott Hughes
2279d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ);
2280d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
228142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // Return the address that the calling assembly stub should jump to.
228242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  return start_address;
2283468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
2284