linker.cpp revision b648a8a57ee42533a5bf127225a252f73ca2cbbc
11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
2943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan * Copyright (C) 2008, 2009 The Android Open Source Project
31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved.
41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without
61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions
71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met:
81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  * Redistributions of source code must retain the above copyright
91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer.
101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *  * Redistributions in binary form must reproduce the above copyright
111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    notice, this list of conditions and the following disclaimer in
121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    the documentation and/or other materials provided with the
131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *    distribution.
141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE.
271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
294688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <dlfcn.h>
304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <errno.h>
314688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <fcntl.h>
320266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes#include <inttypes.h>
334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <pthread.h>
341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdio.h>
351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdlib.h>
361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <string.h>
371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <sys/atomics.h>
384688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <sys/mman.h>
394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <sys/stat.h>
404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <unistd.h>
411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Private C library headers.
43eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/bionic_tls.h"
44eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/KernelArgumentBlock.h"
45eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/ScopedPthreadMutexLocker.h"
461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker.h"
481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker_debug.h"
49be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner#include "linker_environ.h"
5023363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner#include "linker_phdr.h"
51d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov#include "linker_allocator.h"
521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Do NOT use malloc() and friends or pthread_*() code here.
561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Don't use printf() either; it's caused mysterious memory
571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * corruption in the past.
581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The linker runs before we bring up libc and it's easiest
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to make sure it does not depend on any complex libc features
601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * open issues / todo:
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - cleaner error reporting
641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - after linking, set as much stuff as possible to READONLY
651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   and NOEXEC
664688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes */
671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
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
1281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
129bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstruct linker_stats_t {
130bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    int count[kRelocMax];
131bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
132bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
133bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic linker_stats_t linker_stats;
134bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
135bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind kind) {
136bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    ++linker_stats.count[kind];
137bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
138bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
139bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind) {
140bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
1411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
144bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic unsigned bitmask[4096];
145e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__LP64__)
146e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#define MARK(offset) \
147e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    do { \
148e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        if ((((offset) >> 12) >> 5) < 4096) \
149e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            bitmask[((offset) >> 12) >> 5] |= (1 << (((offset) >> 12) & 31)); \
150faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    } while (0)
151e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#else
152bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(offset) \
153bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    do { \
154bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
155faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    } while (0)
156e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#endif
157bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
158bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(x) do {} while (0)
1591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// You shouldn't try to call memory-allocating functions in the dynamic linker.
1624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Guard against the most obvious ones.
1638f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#define DISALLOW_ALLOCATION(return_type, name, ...) \
1648f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    return_type name __VA_ARGS__ \
1658f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    { \
1664688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
1678f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        __libc_format_log(ANDROID_LOG_FATAL, "linker", "%s", msg); \
1688f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        write(2, msg, strlen(msg)); \
1698f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        abort(); \
1704688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
171812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, malloc, (size_t u __unused));
172812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void, free, (void* u __unused));
173812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused));
174812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused));
1752e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1760353195f344666256dba474a15c9ba22cf0cccc9Dima Zavinstatic char tmp_err_buf[768];
1772e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavinstatic char __linker_dl_err_buf[768];
1782e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
179650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hugheschar* linker_get_error_buffer() {
1805419b9474753d25dff947c7740532f86d130c0beElliott Hughes  return &__linker_dl_err_buf[0];
1812e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin}
1822e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
183650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughessize_t linker_get_error_buffer_size() {
184650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  return sizeof(__linker_dl_err_buf);
185650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes}
186650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
1871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
1881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This function is an empty stub where GDB locates a breakpoint to get notified
1891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about linker activity.
1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1915419b9474753d25dff947c7740532f86d130c0beElliott Hughesextern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1931728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
1943a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic r_debug _r_debug = {1, NULL, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
1953a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic link_map* r_debug_tail = 0;
1961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1973a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic void insert_soinfo_into_debug_map(soinfo* info) {
198bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    // Copy the necessary fields into the debug structure.
1993a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(info->link_map_head);
200ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov    map->l_addr = info->load_bias;
201faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    map->l_name = reinterpret_cast<char*>(info->name);
2023a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    map->l_ld = info->dynamic;
2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Stick the new library at the end of the list.
2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * gdb tends to care more about libc than it does
2061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * about leaf libraries, and ordering it this way
2071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * reduces the back-and-forth over the wire.
2081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
2091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (r_debug_tail) {
2101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        r_debug_tail->l_next = map;
2111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = r_debug_tail;
2121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    } else {
2141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        _r_debug.r_map = map;
2151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = 0;
2161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
2191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
221bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void remove_soinfo_from_debug_map(soinfo* info) {
2223a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(info->link_map_head);
2235e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
224bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (r_debug_tail == map) {
2255e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        r_debug_tail = map->l_prev;
226bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2275e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
228bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_prev) {
229bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_prev->l_next = map->l_next;
230bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
231bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_next) {
232bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_next->l_prev = map->l_prev;
233bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2345e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2355e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
236bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_load(soinfo* info) {
2371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (info->flags & FLAG_EXE) {
2381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        // GDB already knows about the main executable
2391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
2401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2421728b2396591853345507a063ed6075dfd251706Elliott Hughes    ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
2431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2443a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_ADD;
2451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(info);
2481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2493a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_CONSISTENT;
2501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2515e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2525e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
253bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_unload(soinfo* info) {
2545e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    if (info->flags & FLAG_EXE) {
2555e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        // GDB already knows about the main executable
2565e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        return;
2575e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    }
2585e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2591728b2396591853345507a063ed6075dfd251706Elliott Hughes    ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
2605e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2613a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_DELETE;
2625e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2635e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2645e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    remove_soinfo_from_debug_map(info);
2655e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2663a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    _r_debug.r_state = r_debug::RT_CONSISTENT;
2675e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
27018a206c81d9743481e364384affd43306911283dElliott Hughesvoid notify_gdb_of_libraries() {
2713a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  _r_debug.r_state = r_debug::RT_ADD;
2723a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  rtld_db_dlactivity();
2733a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  _r_debug.r_state = r_debug::RT_CONSISTENT;
2743a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes  rtld_db_dlactivity();
2751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
277d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy IvanovLinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
278d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  return g_soinfo_links_allocator.alloc();
279d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
280d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
281d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
282d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  g_soinfo_links_allocator.free(entry);
283d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
284d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
285d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic void protect_data(int protection) {
286d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  g_soinfo_allocator.protect_all(protection);
287d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  g_soinfo_links_allocator.protect_all(protection);
288d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
289d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
290d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo* soinfo_alloc(const char* name, struct stat* file_stat) {
291ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (strlen(name) >= SOINFO_NAME_LEN) {
292ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    DL_ERR("library name \"%s\" too long", name);
293ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return NULL;
294ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
295ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
2961728b2396591853345507a063ed6075dfd251706Elliott Hughes  soinfo* si = g_soinfo_allocator.alloc();
2971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
298ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Initialize the new element.
299ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  memset(si, 0, sizeof(soinfo));
300ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  strlcpy(si->name, name, sizeof(si->name));
301d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  si->flags = FLAG_NEW_SOINFO;
302d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
303d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if (file_stat != NULL) {
304d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    si->set_st_dev(file_stat->st_dev);
305d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    si->set_st_ino(file_stat->st_ino);
306d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
307d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
308ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext->next = si;
309ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext = si;
3101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
311ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("name %s: allocated soinfo @ %p", name, si);
312ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  return si;
3131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
315faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesstatic void soinfo_free(soinfo* si) {
3164688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si == NULL) {
3174688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return;
3184688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
3194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
320d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    if (si->base != 0 && si->size != 0) {
321d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      munmap(reinterpret_cast<void*>(si->base), si->size);
322d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    }
323d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
3241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *prev = NULL, *trav;
3251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
326ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("name %s: freeing soinfo @ %p", si->name, si);
3271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
328d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (trav = solist; trav != NULL; trav = trav->next) {
3291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (trav == si)
3301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
3311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        prev = trav;
3321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (trav == NULL) {
334d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        /* si was not in solist */
3354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("name \"%s\" is not in solist!", si->name);
3361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
3371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
339d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // clear links to/from si
340d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    si->remove_all_links();
341d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
342be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    /* prev will never be NULL, because the first entry in solist is
3431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       always the static libdl_info.
3441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    */
3451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    prev->next = si->next;
346d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si == sonext) {
347d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        sonext = prev;
348d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
349d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov
3501728b2396591853345507a063ed6075dfd251706Elliott Hughes    g_soinfo_allocator.free(si);
3511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
353cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
354cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_path(const char* path, const char* delimiters,
355cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes                       const char** array, char* buf, size_t buf_size, size_t max_count) {
356cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (path == NULL) {
357cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return;
358cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
359cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
360cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t len = strlcpy(buf, path, buf_size);
361cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
362cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t i = 0;
363cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  char* buf_p = buf;
364cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
365cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (*array[i] != '\0') {
366cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      ++i;
367cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    }
368cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
369cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
370cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // Forget the last path if we had to truncate; this occurs if the 2nd to
371cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // last char isn't '\0' (i.e. wasn't originally a delimiter).
372cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
373cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    array[i - 1] = NULL;
374cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  } else {
375cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    array[i] = NULL;
376cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
377cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
378cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
379cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_LIBRARY_PATH(const char* path) {
3801728b2396591853345507a063ed6075dfd251706Elliott Hughes  parse_path(path, ":", g_ld_library_paths,
3811728b2396591853345507a063ed6075dfd251706Elliott Hughes             g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX);
382cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
383cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
384cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_PRELOAD(const char* path) {
385cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // We have historically supported ':' as well as ' ' in LD_PRELOAD.
3861728b2396591853345507a063ed6075dfd251706Elliott Hughes  parse_path(path, " :", g_ld_preload_names,
3871728b2396591853345507a063ed6075dfd251706Elliott Hughes             g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX);
388cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
389cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
3904eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
3914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* For a given PC, find the .so that it belongs to.
3931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the base address of the .ARM.exidx section
3941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for that .so, and the number of 8-byte entries
3951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in that section (via *pcount).
3961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Intended to be called by libc's __gnu_Unwind_Find_exidx().
3981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
3993b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes * This function is exposed via dlfcn.cpp and libdl.so.
4001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
401faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
4021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned addr = (unsigned)pc;
4031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
404faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    for (soinfo* si = solist; si != 0; si = si->next) {
405468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if ((addr >= si->base) && (addr < (si->base + si->size))) {
406468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            *pcount = si->ARM_exidx_count;
407f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            return (_Unwind_Ptr)si->ARM_exidx;
4081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
4091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
410faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    *pcount = 0;
4111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return NULL;
4121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4134688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
41424053a461e7a20f34002262c1bb122023134989dChristopher Ferris#endif
4154688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
4161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Here, we only have to provide a callback to iterate across all the
4171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * loaded libraries. gcc_eh does the rest. */
418faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesint dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
4191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int rv = 0;
420bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    for (soinfo* si = solist; si != NULL; si = si->next) {
421bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        dl_phdr_info dl_info;
4223a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes        dl_info.dlpi_addr = si->link_map_head.l_addr;
4233a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes        dl_info.dlpi_name = si->link_map_head.l_name;
4241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phdr = si->phdr;
4251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phnum = si->phnum;
426bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        rv = cb(&dl_info, sizeof(dl_phdr_info), data);
427bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        if (rv != 0) {
4281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
429bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        }
4301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return rv;
4321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
4340266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Sym)* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
4350266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* symtab = si->symtab;
4360266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  const char* strtab = si->strtab;
4371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd",
4390266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes             name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
4401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
4420266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s = symtab + n;
4430266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    if (strcmp(strtab + s->st_name, name)) continue;
4441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    /* only concern ourselves with global and weak symbol definitions */
4460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    switch (ELF_ST_BIND(s->st_info)) {
4470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      case STB_GLOBAL:
4480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      case STB_WEAK:
4490266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if (s->st_shndx == SHN_UNDEF) {
4500266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        continue;
4510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      }
4521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4530266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
4540266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                 name, si->name, reinterpret_cast<void*>(s->st_value),
4550266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                 static_cast<size_t>(s->st_size));
4560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      return s;
4571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4580266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  }
4591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  return NULL;
4611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
463d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic unsigned elfhash(const char* _name) {
464faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    const unsigned char* name = reinterpret_cast<const unsigned char*>(_name);
4651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned h = 0, g;
4661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
467faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    while (*name) {
4681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h = (h << 4) + *name++;
4691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        g = h & 0xf0000000;
4701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g;
4711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g >> 24;
4721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return h;
4741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4760266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Sym)* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
477943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
4780266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s = NULL;
4796ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
480c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin    if (si != NULL && somain != NULL) {
4815ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        /*
482c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * Local scope is executable scope. Just start looking into it right away
483c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * for the shortcut.
4845ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel         */
4855ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
486c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        if (si == somain) {
487c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
4885ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (s != NULL) {
489c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
4905ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                goto done;
4915ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
492c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        } else {
493c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
494c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
495c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
496c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with symbolic relocations disabled, the
497c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * first place to look to resolve external references is the main
498c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * executable.
499c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
500c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
501c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (!si->has_DT_SYMBOLIC) {
502ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s",
50361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
504c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
505c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
506c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
507c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
508c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
509c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5105ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
511c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Look for symbols in the local scope (the object who is
5124eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes             * searching). This happens with C++ templates on x86 for some
513c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * reason.
514c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             *
515c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Notes on weak symbols:
516c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * The ELF specs are ambiguous about treatment of weak definitions in
517c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * dynamic linking.  Some systems return the first definition found
518c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * and some the first non-weak definition.   This is system dependent.
519c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Here we return the first definition found for simplicity.  */
520c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
521c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
522c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (s != NULL) {
523c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
524c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                goto done;
525c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
526c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
527c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
528c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with -Bsymbolic and symbol is not found
529c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * in the local scope, try to find the symbol in the main executable.
530c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
531c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
532c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (si->has_DT_SYMBOLIC) {
533ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s after local scope",
53461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
535c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
536c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
537c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
538c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
539c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
540c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5415ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
542d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    }
5436ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5444fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* Next, look for it in the preloads list */
5451728b2396591853345507a063ed6075dfd251706Elliott Hughes    for (int i = 0; g_ld_preloads[i] != NULL; i++) {
5461728b2396591853345507a063ed6075dfd251706Elliott Hughes        s = soinfo_elf_lookup(g_ld_preloads[i], elf_hash, name);
547d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (s != NULL) {
5481728b2396591853345507a063ed6075dfd251706Elliott Hughes            *lsi = g_ld_preloads[i];
5494fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            goto done;
5505ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5514fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
5524fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
553d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (int i = 0; needed[i] != NULL; i++) {
554ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        DEBUG("%s: looking up %s in %s",
55561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes              si->name, name, needed[i]->name);
5565ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_elf_lookup(needed[i], elf_hash, name);
5575ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if (s != NULL) {
5585ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            *lsi = needed[i];
55912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            goto done;
5605ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5616ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5626ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5636ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevdone:
564d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (s != NULL) {
565c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
566c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   "found in %s, base = %p, load bias = %p",
567c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   si->name, name, reinterpret_cast<void*>(s->st_value),
568c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   (*lsi)->name, reinterpret_cast<void*>((*lsi)->base),
569c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   reinterpret_cast<void*>((*lsi)->load_bias));
5706ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        return s;
5716ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5726ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
573943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
5746ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev}
5756ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
576d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3).  It performs symbol lookup only within the
5776ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev   specified soinfo object and not in any of its dependencies.
578d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom
579d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
580d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   that it should do a breadth first search through the dependency
581d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   tree. This agrees with the ELF spec (aka System V Application
582d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   Binary Interface) where in Chapter 5 it discuss resolving "Shared
583d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   Object Dependencies" in breadth first search order.
5846ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
5850266ae5f884d72da58f33a072e865ba131234a5eElliott HughesElfW(Sym)* dlsym_handle_lookup(soinfo* si, const char* name) {
58616084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    return soinfo_elf_lookup(si, elfhash(name), name);
5871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
5881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
589d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3) to performs a global symbol lookup. If the
590d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   start value is null (for RTLD_DEFAULT), the search starts at the
591d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   beginning of the global solist. Otherwise the search starts at the
592d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   specified soinfo (for RTLD_NEXT).
5936ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
5940266ae5f884d72da58f33a072e865ba131234a5eElliott HughesElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
595cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  unsigned elf_hash = elfhash(name);
5961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
597cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (start == NULL) {
598cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    start = solist;
599cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6001698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer
6010266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* s = NULL;
602cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
603cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    s = soinfo_elf_lookup(si, elf_hash, name);
604cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (s != NULL) {
605cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      *found = si;
606cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      break;
6071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
608cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
610cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (s != NULL) {
611c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
612c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes               name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
613cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
615cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  return s;
6161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
618fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Chengsoinfo* find_containing_library(const void* p) {
6190266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
620fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (soinfo* si = solist; si != NULL; si = si->next) {
621fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (address >= si->base && address - si->base < si->size) {
622fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return si;
623e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
624fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
625fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return NULL;
626e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
627e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
6280266ae5f884d72da58f33a072e865ba131234a5eElliott HughesElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr) {
6290266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - si->base;
630fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
631fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // Search the library's symbol table for any defined symbol which
632fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // contains this address.
633fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (size_t i = 0; i < si->nchain; ++i) {
6340266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* sym = &si->symtab[i];
635fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (sym->st_shndx != SHN_UNDEF &&
636fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr >= sym->st_value &&
637fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr < sym->st_value + sym->st_size) {
638fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return sym;
639e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
640fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
641e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
642fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return NULL;
643e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
644e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
645124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library_on_path(const char* name, const char* const paths[]) {
646124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  char buf[512];
647124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  for (size_t i = 0; paths[i] != NULL; ++i) {
6481e980b6bc8315d00a07312b25486531247abd98cElliott Hughes    int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
649124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
650ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes      PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
651124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      continue;
6521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
653124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
654124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (fd != -1) {
655124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      return fd;
656124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
657124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
658124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return -1;
6591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
661124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library(const char* name) {
662ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ opening %s ]", name);
6631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
664124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // If the name contains a slash, we should attempt to open it directly and not search the paths.
665124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (strchr(name, '/') != NULL) {
6666971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
6676971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    if (fd != -1) {
6686971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes      return fd;
6696971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    }
6706971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
6715ca7ed9005ea16733d7c87d7154473b7a500be0cDmitriy Ivanov#if defined(__LP64__)
672489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov    // TODO: uncomment this after bug b/7465467 is fixed.
673489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov    // return -1;
6745ca7ed9005ea16733d7c87d7154473b7a500be0cDmitriy Ivanov#endif
675124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
6761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
677124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
6781728b2396591853345507a063ed6075dfd251706Elliott Hughes  int fd = open_library_on_path(name, g_ld_library_paths);
679124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (fd == -1) {
6801728b2396591853345507a063ed6075dfd251706Elliott Hughes    fd = open_library_on_path(name, kDefaultLdPaths);
681124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
682124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return fd;
6831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
685b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic soinfo* load_library(const char* name, int dlflags, const android_dlextinfo* extinfo) {
6864688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Open the file.
687650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    int fd = open_library(name);
688650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    if (fd == -1) {
6894688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("library \"%s\" not found", name);
6901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
6912e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    }
6921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
693650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    ElfReader elf_reader(name, fd);
694d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
695d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    struct stat file_stat;
696d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
697d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      DL_ERR("unable to stat file for the library %s: %s", name, strerror(errno));
698d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      return NULL;
699d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    }
700d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
701d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // Check for symlink and other situations where
702d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // file can have different names.
703d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    for (soinfo* si = solist; si != NULL; si = si->next) {
704d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      if (si->get_st_dev() != 0 &&
705d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          si->get_st_ino() != 0 &&
706d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          si->get_st_dev() == file_stat.st_dev &&
707d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          si->get_st_ino() == file_stat.st_ino) {
708d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name);
709d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        return si;
710d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      }
711d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    }
712d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
713b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    if ((dlflags & RTLD_NOLOAD) != 0) {
714b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov      return NULL;
715b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    }
716b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov
717d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // Read the ELF header and load the segments.
71812bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)    if (!elf_reader.Load(extinfo)) {
7194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
72023363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
72123363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner
722489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov    soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat);
723650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    if (si == NULL) {
7244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
72523363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
726650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->base = elf_reader.load_start();
727650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->size = elf_reader.load_size();
728650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->load_bias = elf_reader.load_bias();
729650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phnum = elf_reader.phdr_count();
730650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phdr = elf_reader.loaded_phdr();
731d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
732d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // At this point we know that whatever is loaded @ base is a valid ELF
733d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // shared library whose segments are properly mapped in.
734d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    TRACE("[ find_library_internal base=%p size=%zu name='%s' ]",
735d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          reinterpret_cast<void*>(si->base), si->size, si->name);
736d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
737d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    if (!soinfo_link_image(si, extinfo)) {
738d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      soinfo_free(si);
739d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      return NULL;
740d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    }
741d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
742650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    return si;
7431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
745489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanovstatic soinfo *find_loaded_library_by_name(const char* name) {
746489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  const char* search_name = SEARCH_NAME(name);
747489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  for (soinfo* si = solist; si != NULL; si = si->next) {
748489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov    if (!strcmp(search_name, si->name)) {
749489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov      return si;
75012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    }
751489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  }
752489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  return NULL;
75312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel}
75412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
755b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic soinfo* find_library_internal(const char* name, int dlflags, const android_dlextinfo* extinfo) {
756d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (name == NULL) {
757d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return somain;
758d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
759d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
760489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov  soinfo* si = find_loaded_library_by_name(name);
761b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov
762b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  // Library might still be loaded, the accurate detection
763b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  // of this fact is done by load_library
764b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  if (si == NULL) {
765b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    TRACE("[ '%s' has not been found by name.  Trying harder...]", name);
766b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    si = load_library(name, dlflags, extinfo);
767b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  }
768b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov
769b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  if (si != NULL && (si->flags & FLAG_LINKED) == 0) {
770b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    DL_ERR("recursive link to \"%s\"", si->name);
771d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return NULL;
772d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
77312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
774b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  return si;
775d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
776d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
777b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic soinfo* find_library(const char* name, int dlflags, const android_dlextinfo* extinfo) {
778b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  soinfo* si = find_library_internal(name, dlflags, extinfo);
779d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
780ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count++;
781d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
782d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
783d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
784d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
785b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic void soinfo_unload(soinfo* si) {
786ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  if (si->ref_count == 1) {
787ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("unloading '%s'", si->name);
788d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallDestructors();
789d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
790d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    if ((si->flags | FLAG_NEW_SOINFO) != 0) {
791d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      si->get_children().for_each([&] (soinfo* child) {
792d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        TRACE("%s needs to unload %s", si->name, child->name);
793d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        soinfo_unload(child);
794d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      });
795d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    } else {
796d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
797d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        if (d->d_tag == DT_NEEDED) {
798d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          const char* library_name = si->strtab + d->d_un.d_val;
799d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov          TRACE("%s needs to unload %s", si->name, library_name);
800b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov          soinfo* needed = find_library(library_name, RTLD_NOLOAD, NULL);
801b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov          if (needed != NULL) {
802b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            soinfo_unload(needed);
803b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov          } else {
804b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            // Not found: for example if symlink was deleted between dlopen and dlclose
805b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            // Since we cannot really handle errors at this point - print and continue.
806b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name);
807b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov          }
808d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov        }
809d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
8101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
812d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    notify_gdb_of_unload(si);
813ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 0;
814d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov    soinfo_free(si);
815d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  } else {
816ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count--;
817c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
818d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
8191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
821a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughesvoid do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
8221728b2396591853345507a063ed6075dfd251706Elliott Hughes  snprintf(buffer, buffer_size, "%s:%s", kDefaultLdPaths[0], kDefaultLdPaths[1]);
823a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes}
824a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes
825cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesvoid do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
826cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (!get_AT_SECURE()) {
827cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    parse_LD_LIBRARY_PATH(ld_library_path);
828cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
829cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
830cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
831b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovsoinfo* do_dlopen(const char* name, int flags, soinfo* caller, const android_dlextinfo* extinfo) {
832b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NOLOAD)) != 0) {
833e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    DL_ERR("invalid flags to dlopen: %x", flags);
834e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    return NULL;
835e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes  }
836012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)  if (extinfo != NULL && ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0)) {
837012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)    DL_ERR("invalid extended flags to android_dlopen_ext: %x", extinfo->flags);
838012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)    return NULL;
839012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)  }
840d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ | PROT_WRITE);
841b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  soinfo* si = find_library(name, flags, extinfo);
842d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
843d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
844b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    if (caller != NULL) {
845b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov      caller->add_child(si);
846b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov    }
847d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
848d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ);
849d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
850d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
8511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
852b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovvoid do_dlclose(soinfo* si) {
853d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ | PROT_WRITE);
854b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  soinfo_unload(si);
855d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ);
8561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8584eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
8599918665a45095ad135576f005c0e5307feb366a1Chris Dearmanstatic int soinfo_relocate(soinfo* si, ElfW(Rela)* rela, unsigned count, soinfo* needed[]) {
8600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Sym)* s;
861c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  soinfo* lsi;
862c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
863c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  for (size_t idx = 0; idx < count; ++idx, ++rela) {
8640266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    unsigned type = ELFW(R_TYPE)(rela->r_info);
8650266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    unsigned sym = ELFW(R_SYM)(rela->r_info);
8660266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + si->load_bias);
8670266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Addr) sym_addr = 0;
868faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    const char* sym_name = NULL;
869c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
870c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
871c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (type == 0) { // R_*_NONE
872c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      continue;
873c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
874c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (sym != 0) {
875c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes      sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
876c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      s = soinfo_do_lookup(si, sym_name, &lsi, needed);
877c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      if (s == NULL) {
878c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        // We only allow an undefined symbol if this is a weak reference...
879c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes        s = &si->symtab[sym];
880c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
881c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
882c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          return -1;
883c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
884c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
885c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        /* IHI0044C AAELF 4.5.1.1:
886c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
887c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           Libraries are not searched to resolve weak references.
888c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           It is not an error for a weak reference to remain unsatisfied.
889c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
890c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           During linking, the value of an undefined weak reference is:
891c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - Zero if the relocation type is absolute
892c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - The address of the place if the relocation is pc-relative
893c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - The address of nominal base address if the relocation
894c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes             type is base-relative.
895c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         */
896c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
897c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (type) {
898e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__)
899e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_JUMP_SLOT:
900e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_GLOB_DAT:
901e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS64:
902e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS32:
903e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_ABS16:
904e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        case R_AARCH64_RELATIVE:
905e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland          /*
906e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * The sym_addr was initialized to be zero above, or the relocation
907e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * code below does not care about value of sym_addr.
908e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           * No need to do anything.
909e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland           */
910e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland          break;
911e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__)
912c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_JUMP_SLOT:
913c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_GLOB_DAT:
914c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_32:
915c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_RELATIVE:
916c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          // No need to do anything.
917c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          break;
918c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_PC32:
919c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          sym_addr = reloc;
920c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          break;
9214eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
922c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        default:
923faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes          DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx);
924c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          return -1;
925c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
926c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      } else {
927c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        // We got a definition.
9280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
929c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      }
930c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocSymbol);
931c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    } else {
932c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      s = NULL;
933c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
934c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
935c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    switch (type) {
936e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__)
937e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_JUMP_SLOT:
938e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
939e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9400266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n",
9410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9420266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
943e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
944e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_GLOB_DAT:
945e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
946e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n",
9480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9490266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend);
950e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
951e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS64:
952e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
953e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9540266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
9550266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
957e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
958e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS32:
959e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
960e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9610266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
9620266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9630266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
9640266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
9650266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
966e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
9670266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
9680266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
9690266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT32_MIN),
9700266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT32_MAX));
971e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
972e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
973e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
974e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_ABS16:
975e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocAbsolute);
976e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9770266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
9780266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), sym_name);
9790266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) &&
9800266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
9810266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend);
982e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
9830266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
9840266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)),
9850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT16_MIN),
9860266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT16_MAX));
987e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
988e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
989e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
990e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL64:
991e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
992e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
9930266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
9940266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
9950266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset;
996e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
997e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL32:
998e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
999e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
10000266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
10010266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
10020266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
10030266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) {
10040266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
1005e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
10060266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
10070266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
10080266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT32_MIN),
10090266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT32_MAX));
1010e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1011e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
1012e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1013e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_PREL16:
1014e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
1015e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
10160266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
10170266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name);
10180266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) &&
10190266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) {
10200266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset);
1021e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        } else {
10220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
10230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)),
10240266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(INT16_MIN),
10250266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   static_cast<ElfW(Addr)>(UINT16_MAX));
1026e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1027e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
1028e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1029e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1030e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_RELATIVE:
1031e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        count_relocation(kRelocRelative);
1032e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        MARK(rela->r_offset);
1033e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        if (sym) {
1034e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            DL_ERR("odd RELATIVE form...");
1035e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland            return -1;
1036e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        }
10370266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n",
10380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (si->base + rela->r_addend));
10390266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        *reinterpret_cast<ElfW(Addr)*>(reloc) = (si->base + rela->r_addend);
1040e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1041e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1042e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_COPY:
1043b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov        /*
1044b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         * ET_EXEC is not supported so this should not happen.
1045b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         *
1046b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1047b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         *
1048b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         * Section 4.7.1.10 "Dynamic relocations"
1049b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         * R_AARCH64_COPY may only appear in executable objects where e_type is
1050b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         * set to ET_EXEC.
1051b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov         */
1052b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov        DL_ERR("%s R_AARCH64_COPY relocations are not supported", si->name);
1053b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov        return -1;
1054e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_TLS_TPREL64:
10550266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
10560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset);
1057e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1058e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland    case R_AARCH64_TLS_DTPREL32:
10590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
10600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                   reloc, (sym_addr + rela->r_addend), rela->r_offset);
1061e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland        break;
1062e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__)
1063c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_JUMP_SLOT:
1064c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocAbsolute);
1065c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1066c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1067c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
10680266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1069c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1070c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_GLOB_DAT:
1071c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocAbsolute);
1072c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1073c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
1074c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
10750266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1076c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1077c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_RELATIVE:
1078c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1079c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1080c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      if (sym) {
1081c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DL_ERR("odd RELATIVE form...");
1082c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        return -1;
1083c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      }
1084c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
1085c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(si->base));
10860266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = si->base + rela->r_addend;
1087c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1088c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_32:
1089c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1090c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1091c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1092c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr), sym_name);
10930266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1094c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
1095c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin    case R_X86_64_64:
1096c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      count_relocation(kRelocRelative);
1097c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      MARK(rela->r_offset);
1098c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
1099c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin                 static_cast<size_t>(sym_addr), sym_name);
11000266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend;
1101c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      break;
1102c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_PC32:
1103c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
1104c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
1105c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
1106c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
1107c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
11080266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc;
1109c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
11104eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1111e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland
1112c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    default:
1113faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes      DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx);
1114c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      return -1;
1115c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1116c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  }
1117c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  return 0;
1118c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes}
11199918665a45095ad135576f005c0e5307feb366a1Chris Dearman
11209918665a45095ad135576f005c0e5307feb366a1Chris Dearman#else // REL, not RELA.
11219918665a45095ad135576f005c0e5307feb366a1Chris Dearman
11220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic int soinfo_relocate(soinfo* si, ElfW(Rel)* rel, unsigned count, soinfo* needed[]) {
11230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* s;
1124d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo* lsi;
11251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    for (size_t idx = 0; idx < count; ++idx, ++rel) {
11270266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        unsigned type = ELFW(R_TYPE)(rel->r_info);
11280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead.
11290266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        unsigned sym = ELFW(R_SYM)(rel->r_info);
11300266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + si->load_bias);
11310266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        ElfW(Addr) sym_addr = 0;
1132faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        const char* sym_name = NULL;
11331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1134c620059479c47a78d57086d73726c9adc2f337adElliott Hughes        DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
1135d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (type == 0) { // R_*_NONE
1136d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            continue;
1137d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1138d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (sym != 0) {
1139c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes            sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name);
11405ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1141d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (s == NULL) {
11424eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                // We only allow an undefined symbol if this is a weak reference...
1143c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes                s = &si->symtab[sym];
1144c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1145e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes                    DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
1146e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1147e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1148e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1149e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* IHI0044C AAELF 4.5.1.1:
1150e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1151e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   Libraries are not searched to resolve weak references.
1152e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   It is not an error for a weak reference to remain
1153e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   unsatisfied.
1154e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1155e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   During linking, the value of an undefined weak reference is:
1156e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - Zero if the relocation type is absolute
1157e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - The address of the place if the relocation is pc-relative
1158bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes                   - The address of nominal base address if the relocation
1159e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                     type is base-relative.
1160e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                  */
1161e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1162e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                switch (type) {
11634eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
1164e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_JUMP_SLOT:
1165e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_GLOB_DAT:
1166e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_ABS32:
1167e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_RELATIVE:    /* Don't care. */
11684eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // sym_addr was initialized to be zero above or relocation
11694eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // code below does not care about value of sym_addr.
11704eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // No need to do anything.
11714eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    break;
11724eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
1173d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                case R_386_JMP_SLOT:
1174e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_GLOB_DAT:
1175e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_32:
1176c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                case R_386_RELATIVE:    /* Don't care. */
11774eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // sym_addr was initialized to be zero above or relocation
11784eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // code below does not care about value of sym_addr.
11794eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // No need to do anything.
1180e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
1181e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_PC32:
1182e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    sym_addr = reloc;
1183e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
11844eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1185e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
11864eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
1187e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_COPY:
11884eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // Fall through. Can't really copy if weak symbol is not found at run-time.
11894eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1190e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                default:
1191faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                    DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
1192e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1193e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1194e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            } else {
11954eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                // We got a definition.
11960266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias);
1197d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1198bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocSymbol);
11991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        } else {
1200e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            s = NULL;
12011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
12021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1203c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (type) {
12044eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
12051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_JUMP_SLOT:
1206bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
12071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1208ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
12090266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
12101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
12111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_GLOB_DAT:
1212bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
12131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1214ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
12150266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
12161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
12171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_ABS32:
1218bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
12191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1220ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
12210266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
12221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
122334ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner        case R_ARM_REL32:
1224bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
122534ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            MARK(rel->r_offset);
1226ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
122734ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner                       reloc, sym_addr, rel->r_offset, sym_name);
12280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
122934ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            break;
12301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_COPY:
1231b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov            /*
12326275f2083415d22a6ce0de55645079cd47e0cc80Dmitriy Ivanov             * ET_EXEC is not supported so this should not happen.
12336275f2083415d22a6ce0de55645079cd47e0cc80Dmitriy Ivanov             *
1234b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov             * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1235b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov             *
1236b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov             * Section 4.7.1.10 "Dynamic relocations"
1237b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov             * R_ARM_COPY may only appear in executable objects where e_type is
1238b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov             * set to ET_EXEC.
1239b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov             */
1240b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov            DL_ERR("%s R_ARM_COPY relocations are not supported", si->name);
1241b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov            return -1;
12424eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
12434eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_JMP_SLOT:
12444eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
12454eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
12464eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
12470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
12484eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
12494eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_GLOB_DAT:
12504eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
12514eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
12524eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
12530266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr;
12544eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
12554eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_32:
12564eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
12574eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
12584eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
12590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
12604eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
12614eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_PC32:
12624eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
12634eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
12644eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
12654eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                       reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
12660266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
12674eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
12684eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__mips__)
12694eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_MIPS_REL32:
12709918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__LP64__)
12719918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // MIPS Elf64_Rel entries contain compound relocations
12729918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case
12739918665a45095ad135576f005c0e5307feb366a1Chris Dearman            if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 ||
12749918665a45095ad135576f005c0e5307feb366a1Chris Dearman                ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) {
1275faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)",
12769918665a45095ad135576f005c0e5307feb366a1Chris Dearman                       type, (unsigned)ELF64_R_TYPE2(rel->r_info),
1277faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes                       (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx);
12789918665a45095ad135576f005c0e5307feb366a1Chris Dearman                return -1;
12799918665a45095ad135576f005c0e5307feb366a1Chris Dearman            }
12809918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif
12814eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
12824eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
12839918665a45095ad135576f005c0e5307feb366a1Chris Dearman            TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc),
12849918665a45095ad135576f005c0e5307feb366a1Chris Dearman                       static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*");
12854eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            if (s) {
12860266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
12874eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            } else {
12880266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes                *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base;
12894eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            }
12904eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
12914eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
12924eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes
12934eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
12944eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_ARM_RELATIVE:
12954eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
12964eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_RELATIVE:
12974eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
12984eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
12994eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
13004eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            if (sym) {
13014eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                DL_ERR("odd RELATIVE form...");
13024eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                return -1;
13034eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            }
13044eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
13054eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                       reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base));
13060266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base;
13074eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
13081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        default:
1310faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
13111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return -1;
13121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
13131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
13151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
1316c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
13171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13184eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
131987c358524e479235aa6241736d2ce325f89daafcBrian Carlstromstatic bool mips_relocate_got(soinfo* si, soinfo* needed[]) {
13209918665a45095ad135576f005c0e5307feb366a1Chris Dearman    ElfW(Addr)** got = si->plt_got;
132187c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    if (got == NULL) {
132287c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom        return true;
132387c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    }
132487c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned local_gotno = si->mips_local_gotno;
132587c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned gotsym = si->mips_gotsym;
132687c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned symtabno = si->mips_symtabno;
13270266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* symtab = si->symtab;
1328d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
13299918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // got[0] is the address of the lazy resolver function.
13309918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // got[1] may be used for a GNU extension.
13319918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start).
13329918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // FIXME: maybe this should be in a separate routine?
1333d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if ((si->flags & FLAG_LINKER) == 0) {
133487c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom        size_t g = 0;
13359918665a45095ad135576f005c0e5307feb366a1Chris Dearman        got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef);
13369918665a45095ad135576f005c0e5307feb366a1Chris Dearman        if (reinterpret_cast<intptr_t>(got[g]) < 0) {
13379918665a45095ad135576f005c0e5307feb366a1Chris Dearman            got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed);
1338d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
13399918665a45095ad135576f005c0e5307feb366a1Chris Dearman        // Relocate the local GOT entries.
1340d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        for (; g < local_gotno; g++) {
13419918665a45095ad135576f005c0e5307feb366a1Chris Dearman            got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + si->load_bias);
1342d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1343d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1344d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
13459918665a45095ad135576f005c0e5307feb366a1Chris Dearman    // Now for the global GOT entries...
13460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Sym)* sym = symtab + gotsym;
1347d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got + local_gotno;
134887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
13499918665a45095ad135576f005c0e5307feb366a1Chris Dearman        // This is an undefined reference... try to locate it.
13509918665a45095ad135576f005c0e5307feb366a1Chris Dearman        const char* sym_name = si->strtab + sym->st_name;
1351d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        soinfo* lsi;
13529918665a45095ad135576f005c0e5307feb366a1Chris Dearman        ElfW(Sym)* s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1353d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (s == NULL) {
13549918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // We only allow an undefined symbol if this is a weak reference.
1355d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            s = &symtab[g];
1356c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
13574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("cannot locate \"%s\"...", sym_name);
135887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom                return false;
1359d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1360d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *got = 0;
13619918665a45095ad135576f005c0e5307feb366a1Chris Dearman        } else {
13629918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // FIXME: is this sufficient?
13639918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // For reference see NetBSD link loader
13649918665a45095ad135576f005c0e5307feb366a1Chris 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
13659918665a45095ad135576f005c0e5307feb366a1Chris Dearman            *got = reinterpret_cast<ElfW(Addr)*>(lsi->load_bias + s->st_value);
1366d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1367d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
136887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    return true;
1369d7daacb46372132ae3f0121647074936c304b572Raghu Gandham}
1370d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1371d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1372812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallArray(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) {
1373ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  if (functions == NULL) {
1374d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1375d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
13768215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1377c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
13788215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1379ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int begin = reverse ? (count - 1) : 0;
1380ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int end = reverse ? -1 : count;
1381ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int step = reverse ? -1 : 1;
13828215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1383ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  for (int i = begin; i != end; i += step) {
1384ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1385ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    CallFunction("function", functions[i]);
1386d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1387d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1388ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s for '%s' ]", array_name, name);
13891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
13901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1391812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallFunction(const char* function_name __unused, linker_function_t function) {
1392db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
1393d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1394d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1395d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1396ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
1397d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  function();
1398ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
1399db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes
1400db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1401db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // are still writable. This happens with our debug malloc (see http://b/7941716).
1402d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ | PROT_WRITE);
14039181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov}
14049181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
1405d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallPreInitConstructors() {
14068147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
14078147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // but ignored in a shared library.
1408d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1409d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
1410e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1411d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallConstructors() {
1412d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (constructors_called) {
1413d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1414d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1415e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1416d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // We set constructors_called before actually calling the constructors, otherwise it doesn't
1417d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // protect against recursive constructor calls. One simple example of constructor recursion
1418d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1419d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 1. The program depends on libc, so libc's constructor is called here.
1420d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1421d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 3. dlopen() calls the constructors on the newly created
1422d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    soinfo for libc_malloc_debug_leak.so.
1423d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 4. The debug .so depends on libc, so CallConstructors is
1424d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    called again with the libc soinfo. If it doesn't trigger the early-
1425d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    out above, the libc constructor will be called again (recursively!).
1426d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  constructors_called = true;
1427d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
14288147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) {
14298147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes    // The GNU dynamic linker silently ignores these, but we warn the developer.
1430c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
14318147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes          name, preinit_array_count);
1432d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
14331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1434d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  get_children().for_each([] (soinfo* si) {
1435d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    si->CallConstructors();
1436d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  });
14371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14388147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  TRACE("\"%s\": calling constructors", name);
14398147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
14408147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_INIT should be called before DT_INIT_ARRAY if both are present.
1441d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_INIT", init_func);
1442d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
1443e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov}
14448215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1445d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallDestructors() {
14468147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  TRACE("\"%s\": calling destructors", name);
14478147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
14488147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_FINI_ARRAY must be parsed in reverse order.
1449d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
14508147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
14518147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_FINI should be called after DT_FINI_ARRAY if both are present.
1452d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_FINI", fini_func);
1453b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov
1454b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  // This is needed on second call to dlopen
1455b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  // after library has been unloaded with RTLD_NODELETE
1456b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov  constructors_called = false;
14571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1459d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::add_child(soinfo* child) {
1460d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1461d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return;
1462d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1463d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1464d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  this->children.push_front(child);
1465d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  child->parents.push_front(this);
1466d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1467d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1468d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::remove_all_links() {
1469d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1470d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return;
1471d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1472d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1473d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  // 1. Untie connected soinfos from 'this'.
1474d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  children.for_each([&] (soinfo* child) {
1475d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    child->parents.remove_if([&] (const soinfo* parent) {
1476d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      return parent == this;
1477d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    });
1478d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  });
1479d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1480d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  parents.for_each([&] (soinfo* parent) {
1481d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    parent->children.for_each([&] (const soinfo* child) {
1482d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov      return child == this;
1483d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    });
1484d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  });
1485d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1486d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  // 2. Once everything untied - clear local lists.
1487d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  parents.clear();
1488d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  children.clear();
1489d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1490d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1491d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::set_st_dev(dev_t dev) {
1492d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1493d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return;
1494d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1495d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1496d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  st_dev = dev;
1497d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1498d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1499d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::set_st_ino(ino_t ino) {
1500d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1501d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return;
1502d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1503d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1504d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  st_ino = ino;
1505d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1506d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1507d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovdev_t soinfo::get_st_dev() {
1508d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1509d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return 0;
1510d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1511d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1512d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  return st_dev;
1513d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov};
1514d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1515d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovino_t soinfo::get_st_ino() {
1516d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1517d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return 0;
1518d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1519d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1520d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  return st_ino;
1521d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1522d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1523d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov// This is a return on get_children() in case
1524d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov// 'this->flags' does not have FLAG_NEW_SOINFO set.
1525d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo::soinfo_list_t g_empty_list;
1526d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1527d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovsoinfo::soinfo_list_t& soinfo::get_children() {
1528d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  if ((this->flags & FLAG_NEW_SOINFO) == 0) {
1529d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    return g_empty_list;
1530d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  }
1531d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1532d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  return this->children;
1533d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
1534d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
15351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with
15361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   /dev/null. */
15375419b9474753d25dff947c7740532f86d130c0beElliott Hughesstatic int nullify_closed_stdio() {
15381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int dev_null, i, status;
15391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int return_value = 0;
15401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
154116084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
15421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null < 0) {
15434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("cannot open /dev/null: %s", strerror(errno));
15441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return -1;
15451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1546ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
15471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If any of the stdio file descriptors is valid and not associated
15491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       with /dev/null, dup /dev/null to it.  */
15501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (i = 0; i < 3; i++) {
15511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* If it is /dev/null already, we are done. */
15524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (i == dev_null) {
15531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
15544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
15551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1556ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Nullifying stdio file descriptor %d]", i);
15574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
15581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        /* If file is opened, we are good. */
15604688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status != -1) {
15614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            continue;
15624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
15631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* The only error we allow is that the file descriptor does not
15651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           exist, in which case we dup /dev/null to it. */
15661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (errno != EBADF) {
15674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("fcntl failed: %s", strerror(errno));
15681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
15691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
15701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
15711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* Try dupping /dev/null to this stdio file descriptor and
15731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           repeat if there is a signal.  Note that any errors in closing
15741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           the stdio descriptor are lost.  */
15754688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
15761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (status < 0) {
15774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("dup2 failed: %s", strerror(errno));
15781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
15791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
15801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
15811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
15821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If /dev/null is not one of the stdio file descriptors, close it. */
15841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null > 2) {
1585ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
15864688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(close(dev_null));
15874688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status == -1) {
15884688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("close failed: %s", strerror(errno));
15891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
15901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
15911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
15921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
15931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return return_value;
15941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
15951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1596183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo) {
1597f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee    /* "base" might wrap around UINT32_MAX. */
15980266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Addr) base = si->load_bias;
15990266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    const ElfW(Phdr)* phdr = si->phdr;
16001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int phnum = si->phnum;
1601d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
16021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1603b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can't debug anything until the linker is relocated */
1604b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (!relocating_linker) {
1605ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        INFO("[ linking %s ]", si->name);
1606c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags);
1607b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
16081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
160963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    /* Extract dynamic section */
1610124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    size_t dynamic_count;
16110266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    ElfW(Word) dynamic_flags;
161212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
1613cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman                                   &dynamic_count, &dynamic_flags);
161463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    if (si->dynamic == NULL) {
1615b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1616124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
1617b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
1618124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
161963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    } else {
1620b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1621ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("dynamic = %p", si->dynamic);
1622b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
162363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    }
162463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
16254eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
162663f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    (void) phdr_table_get_arm_exidx(phdr, phnum, base,
162763f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner                                    &si->ARM_exidx, &si->ARM_exidx_count);
162863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif
162963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
16308147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes    // Extract useful information from dynamic section.
1631d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    uint32_t needed_count = 0;
16320266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1633c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1634c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes              d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1635c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (d->d_tag) {
16361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_HASH:
1637faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->nbucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[0];
1638faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->nchain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[1];
1639faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->bucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8);
1640faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->chain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8 + si->nbucket * 4);
16411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_STRTAB:
1643faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->strtab = reinterpret_cast<const char*>(base + d->d_un.d_ptr);
16441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_SYMTAB:
1646faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->symtab = reinterpret_cast<ElfW(Sym)*>(base + d->d_un.d_ptr);
16471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16484eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if !defined(__LP64__)
16491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTREL:
1650d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val != DT_REL) {
1651124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1652124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
16531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
16541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1655c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
16561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_JMPREL:
16574eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1658faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->plt_rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr);
1659c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1660faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->plt_rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr);
1661c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
16621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTRELSZ:
16644eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
16650266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->plt_rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
1666c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
16670266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->plt_rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
1668c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
16691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16709918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__mips__)
16711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTGOT:
16729918665a45095ad135576f005c0e5307feb366a1Chris Dearman            // Used by mips and mips64.
16739918665a45095ad135576f005c0e5307feb366a1Chris Dearman            si->plt_got = reinterpret_cast<ElfW(Addr)**>(base + d->d_un.d_ptr);
16741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1675c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
16761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_DEBUG:
1677bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_DEBUG entry to the address of _r_debug for GDB
1678cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman            // if the dynamic table is writable
16799918665a45095ad135576f005c0e5307feb366a1Chris Dearman// FIXME: not working currently for N64
16809918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The flags for the LOAD and DYNAMIC program headers do not agree.
16819918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The LOAD section containng the dynamic table has been mapped as
16829918665a45095ad135576f005c0e5307feb366a1Chris Dearman// read-only, but the DYNAMIC header claims it is writable.
16839918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if !(defined(__mips__) && defined(__LP64__))
168499c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            if ((dynamic_flags & PF_W) != 0) {
1685c620059479c47a78d57086d73726c9adc2f337adElliott Hughes                d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
168699c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            }
16871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
16889918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif
16894eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1690c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         case DT_RELA:
1691faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr);
1692c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1693c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         case DT_RELASZ:
16940266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->rela_count = d->d_un.d_val / sizeof(ElfW(Rela));
1695c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1696c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_REL:
1697c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            DL_ERR("unsupported DT_REL in \"%s\"", si->name);
1698c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1699c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_RELSZ:
1700c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name);
1701c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1702c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1703c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_REL:
1704faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes            si->rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr);
1705c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1706c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_RELSZ:
17070266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->rel_count = d->d_un.d_val / sizeof(ElfW(Rel));
1708c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1709ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI         case DT_RELA:
1710124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1711124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1712c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
17131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT:
1714ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
17158147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func);
17161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI:
1718ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
17198147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func);
17201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAY:
1722ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
17238147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array);
17241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAYSZ:
17260266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
17271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAY:
1729ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
17308147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array);
17311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAYSZ:
17330266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
17341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAY:
1736ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
17378147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array);
17381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAYSZ:
17400266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes            si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr));
17411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
17421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_TEXTREL:
1743e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__)
1744e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes            DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1745e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes            return false;
1746e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else
17475135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            si->has_text_relocations = true;
17481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1749e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
17505ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_SYMBOLIC:
17515ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            si->has_DT_SYMBOLIC = true;
17525ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
1753d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        case DT_NEEDED:
1754d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            ++needed_count;
1755d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            break;
17565ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_FLAGS:
1757d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_TEXTREL) {
1758e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__)
1759e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes                DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1760e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes                return false;
1761e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else
17625ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_text_relocations = true;
1763e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
17645ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
1765d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_SYMBOLIC) {
17665ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_DT_SYMBOLIC = true;
17675ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
17685ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
17694eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
1770d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_STRSZ:
1771d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_SYMENT:
1772d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_RELENT:
1773d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             break;
1774d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_MAP:
1775bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
1776d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            {
1777006f9ad8910c945cd50e54f6c34d8c27b11c1193Benjamin Adolphi              r_debug** dp = reinterpret_cast<r_debug**>(base + d->d_un.d_ptr);
1778d7daacb46372132ae3f0121647074936c304b572Raghu Gandham              *dp = &_r_debug;
1779d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1780d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1781d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_VERSION:
1782d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_FLAGS:
1783d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_BASE_ADDRESS:
1784d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_UNREFEXTNO:
1785d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1786d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1787d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_SYMTABNO:
1788d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_symtabno = d->d_un.d_val;
1789d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1790d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1791d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_LOCAL_GOTNO:
1792d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_local_gotno = d->d_un.d_val;
1793d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1794d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1795d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_GOTSYM:
1796d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_gotsym = d->d_un.d_val;
1797d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
17984eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1799d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1800d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        default:
18014eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            DEBUG("Unused DT entry: type %p arg %p",
18024eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                  reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1803d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
18041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
18051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1807c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
1808c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          reinterpret_cast<void*>(si->base), si->strtab, si->symtab);
18091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1810124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    // Sanity checks.
1811d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (relocating_linker && needed_count != 0) {
1812d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1813d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        return false;
1814d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
1815124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->nbucket == 0) {
1816124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1817124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1818124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1819124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->strtab == 0) {
1820124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1821124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1822124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1823124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->symtab == 0) {
1824124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1825124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
18261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18287e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes    // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
1829d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_EXE) {
18301728b2396591853345507a063ed6075dfd251706Elliott Hughes        memset(g_ld_preloads, 0, sizeof(g_ld_preloads));
18317e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes        size_t preload_count = 0;
18321728b2396591853345507a063ed6075dfd251706Elliott Hughes        for (size_t i = 0; g_ld_preload_names[i] != NULL; i++) {
1833b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            soinfo* lsi = find_library(g_ld_preload_names[i], 0, NULL);
18347e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes            if (lsi != NULL) {
18351728b2396591853345507a063ed6075dfd251706Elliott Hughes                g_ld_preloads[preload_count++] = lsi;
18367e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes            } else {
18377e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                // As with glibc, failure to load an LD_PRELOAD library is just a warning.
18387e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
18391728b2396591853345507a063ed6075dfd251706Elliott Hughes                        g_ld_preload_names[i], si->name, linker_get_error_buffer());
18404fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            }
18414fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        }
18424fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
18434fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1844faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    soinfo** needed = reinterpret_cast<soinfo**>(alloca((1 + needed_count) * sizeof(soinfo*)));
1845d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo** pneeded = needed;
184612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
18470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1848d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (d->d_tag == DT_NEEDED) {
1849d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            const char* library_name = si->strtab + d->d_un.d_val;
1850ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("%s needs %s", si->name, library_name);
1851b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov            soinfo* lsi = find_library(library_name, 0, NULL);
1852d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            if (lsi == NULL) {
1853650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes                strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
18544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1855d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                       library_name, si->name, tmp_err_buf);
1856124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
18571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
1858d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1859d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov            si->add_child(lsi);
186012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            *pneeded++ = lsi;
18611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
18621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
186312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    *pneeded = NULL;
18641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1865e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__)
18665135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
1867e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // Make segments writable to allow text relocations to work properly. We will later call
1868e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // phdr_table_protect_segments() after all of them are applied and all constructors are run.
1869865119efd47ae3cae5a0c09c6a9cc184a10614c1Du Chenyang#if !defined(__i386__) // The platform itself has too many text relocations on x86.
18703d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich        DL_WARN("%s has text relocations. This is wasting memory and prevents "
18713d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich                "security hardening. Please fix.", si->name);
1872865119efd47ae3cae5a0c09c6a9cc184a10614c1Du Chenyang#endif
18735135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
18745135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't unprotect loadable segments for \"%s\": %s",
18755135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1876124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
18775135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
18785135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    }
1879e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
18805135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich
18814eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1882c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (si->plt_rela != NULL) {
1883faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s plt ]\n", si->name);
18849918665a45095ad135576f005c0e5307feb366a1Chris Dearman        if (soinfo_relocate(si, si->plt_rela, si->plt_rela_count, needed)) {
1885c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1886c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
1887c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1888c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (si->rela != NULL) {
1889faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s ]\n", si->name);
18909918665a45095ad135576f005c0e5307feb366a1Chris Dearman        if (soinfo_relocate(si, si->rela, si->rela_count, needed)) {
1891c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1892c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
1893c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1894c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1895d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si->plt_rel != NULL) {
1896faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s plt ]", si->name);
1897d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1898124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1899124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
19001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1901d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si->rel != NULL) {
1902faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes        DEBUG("[ relocating %s ]", si->name);
1903d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1904124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1905124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
19061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1907c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
19081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19094eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
191087c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    if (!mips_relocate_got(si, needed)) {
1911124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1912d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1913d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1914d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
19151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_LINKED;
1916ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    DEBUG("[ finished linking %s ]", si->name);
19171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1918e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__)
19195135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
1920e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // All relocations are done, we can protect our segments back to read-only.
19215135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
19225135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't protect segments for \"%s\": %s",
19235135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1924124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
19255135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
19261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1927e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
19281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1929b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can also turn on GNU RELRO protection */
1930b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
19314688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
19324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               si->name, strerror(errno));
1933124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
19349ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich    }
19359ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
1936183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    /* Handle serializing/sharing the RELRO segment */
1937183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
1938183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      if (phdr_table_serialize_gnu_relro(si->phdr, si->phnum, si->load_bias,
1939183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)                                         extinfo->relro_fd) < 0) {
1940183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
1941183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)               si->name, strerror(errno));
1942183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        return false;
1943183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      }
1944183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
1945183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      if (phdr_table_map_gnu_relro(si->phdr, si->phnum, si->load_bias,
1946183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)                                   extinfo->relro_fd) < 0) {
1947183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
1948183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)               si->name, strerror(errno));
1949183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)        return false;
1950183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)      }
1951183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    }
1952183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)
19531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    notify_gdb_of_load(si);
1954124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    return true;
19551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
19561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1957468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1958c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * This function add vdso to internal dso list.
1959c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * It helps to stack unwinding through signal handlers.
1960c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * Also, it makes bionic more like glibc.
1961c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov */
1962812fd4263a005b88f3b4222baa910114f938d594Kito Chengstatic void add_vdso(KernelArgumentBlock& args __unused) {
19634eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(AT_SYSINFO_EHDR)
19640266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
19650266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  if (ehdr_vdso == NULL) {
19660266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    return;
19670266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  }
1968c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
1969d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  soinfo* si = soinfo_alloc("[vdso]", NULL);
1970ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov
19710266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
19720266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->phnum = ehdr_vdso->e_phnum;
19730266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
19740266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->size = phdr_table_get_load_size(si->phdr, si->phnum);
19750266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
1976ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov
19770dcf06f976670b5d05286b184d150f61b71c1184Torne (Richard Coles)  soinfo_link_image(si, NULL);
1978c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov#endif
1979c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov}
1980c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
1981c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov/*
1982d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * This is linker soinfo for GDB. See details below.
1983d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov */
1984d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo linker_soinfo_for_gdb;
1985d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
1986d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov/* gdb expects the linker to be in the debug shared object list.
1987d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * Without this, gdb has trouble locating the linker's ".text"
1988d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * and ".plt" sections. Gdb could also potentially use this to
1989d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1990d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * Don't use soinfo_alloc(), because the linker shouldn't
1991d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * be on the soinfo list.
1992d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov */
1993d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
1994d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov#if defined(__LP64__)
1995d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  strlcpy(linker_soinfo_for_gdb.name, "/system/bin/linker64", sizeof(linker_soinfo_for_gdb.name));
1996d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov#else
1997d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  strlcpy(linker_soinfo_for_gdb.name, "/system/bin/linker", sizeof(linker_soinfo_for_gdb.name));
1998d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov#endif
1999d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  linker_soinfo_for_gdb.flags = FLAG_NEW_SOINFO;
2000d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  linker_soinfo_for_gdb.base = linker_base;
2001d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
2002d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  /*
2003d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   * Set the dynamic field in the link map otherwise gdb will complain with
2004d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   * the following:
2005d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   *   warning: .dynamic section for "/system/bin/linker" is not at the
2006d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   *   expected address (wrong library or version mismatch?)
2007d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov   */
2008d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
2009d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
2010d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
2011d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov                                 &linker_soinfo_for_gdb.dynamic, NULL, NULL);
2012d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  insert_soinfo_into_debug_map(&linker_soinfo_for_gdb);
2013d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}
2014d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
2015d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov/*
2016468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and
2017468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs
2018468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point.
2019468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
20200266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
202142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    /* NOTE: we store the args pointer on a special location
2022ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       of the temporary TLS area in order to pass it to
2023ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       the C Library's runtime initializer.
2024ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *
2025ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       The initializer must clear the slot and reset the TLS
2026ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       to point to a different location to ensure that no other
2027ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       shared library constructor can access it.
2028ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     */
2029d3920b3a996b358e48232f417aa0a1e44a60f155Elliott Hughes  __libc_init_tls(args);
20301a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov
20311a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING
20321a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    struct timeval t0, t1;
20331a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    gettimeofday(&t0, 0);
20341a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif
20351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
203618a206c81d9743481e364384affd43306911283dElliott Hughes    // Initialize environment functions, and get to the ELF aux vectors table.
203742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    linker_env_init(args);
2038be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
20398d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // If this is a setuid/setgid program, close the security hole described in
20408d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
20418d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    if (get_AT_SECURE()) {
20428d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich        nullify_closed_stdio();
20438d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    }
20448d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich
2045d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    debuggerd_init();
20461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
204718a206c81d9743481e364384affd43306911283dElliott Hughes    // Get a few environment variables.
204861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    const char* LD_DEBUG = linker_env_get("LD_DEBUG");
204961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    if (LD_DEBUG != NULL) {
20501728b2396591853345507a063ed6075dfd251706Elliott Hughes      g_ld_debug_verbosity = atoi(LD_DEBUG);
205118a206c81d9743481e364384affd43306911283dElliott Hughes    }
2052be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
205318a206c81d9743481e364384affd43306911283dElliott Hughes    // Normally, these are cleaned by linker_env_init, but the test
205418a206c81d9743481e364384affd43306911283dElliott Hughes    // doesn't cost us anything.
205518a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpath_env = NULL;
205618a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpreload_env = NULL;
205718a206c81d9743481e364384affd43306911283dElliott Hughes    if (!get_AT_SECURE()) {
205818a206c81d9743481e364384affd43306911283dElliott Hughes      ldpath_env = linker_env_get("LD_LIBRARY_PATH");
205918a206c81d9743481e364384affd43306911283dElliott Hughes      ldpreload_env = linker_env_get("LD_PRELOAD");
20601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
20611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2062d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov    // Linker does not call constructors for its own
2063d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov    // global variables so we need to initialize
2064d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    // the allocators explicitly.
20651728b2396591853345507a063ed6075dfd251706Elliott Hughes    g_soinfo_allocator.init();
2066d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    g_soinfo_links_allocator.init();
2067d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov
2068ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    INFO("[ android linker & debugger ]");
20691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2070d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    soinfo* si = soinfo_alloc(args.argv[0], NULL);
207118a206c81d9743481e364384affd43306911283dElliott Hughes    if (si == NULL) {
207218a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
20731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
20741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2075d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    /* bootstrap the link map, the main exe always needs to be first */
20761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_EXE;
20773a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes    link_map* map = &(si->link_map_head);
20781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
20791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = 0;
208042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    map->l_name = args.argv[0];
20811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_prev = NULL;
20821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_next = NULL;
20831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
20841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_map = map;
20851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
20861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2087d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov    init_linker_info_for_gdb(linker_base);
20881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
208942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // Extract information passed from the kernel.
20900266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes    si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
209142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->phnum = args.getauxval(AT_PHNUM);
209242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->entry = args.getauxval(AT_ENTRY);
20931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
20948180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    /* Compute the value of si->base. We can't rely on the fact that
20958180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * the first entry is the PHDR because this will not be true
20968180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * for certain executables (e.g. some in the NDK unit test suite)
20978180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     */
20988180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    si->base = 0;
2099b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    si->size = phdr_table_get_load_size(si->phdr, si->phnum);
2100bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    si->load_bias = 0;
2101ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    for (size_t i = 0; i < si->phnum; ++i) {
210242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      if (si->phdr[i].p_type == PT_PHDR) {
21030266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
21040266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes        si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
210542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes        break;
210642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      }
21078180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    }
2108d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    si->dynamic = NULL;
2109ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 1;
21101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
21112aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich    ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
21122aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich    if (elf_hdr->e_type != ET_DYN) {
21132aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich        __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
21142aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich        exit(EXIT_FAILURE);
21152aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich    }
21162aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich
21174688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
21184688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_LIBRARY_PATH(ldpath_env);
21194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_PRELOAD(ldpreload_env);
21204fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
21215ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    somain = si;
21225ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
2123183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)    if (!soinfo_link_image(si, NULL)) {
2124650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes        __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
212518a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
21261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
21271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2128c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov    add_vdso(args);
2129c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
2130d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallPreInitConstructors();
21319181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
21321728b2396591853345507a063ed6075dfd251706Elliott Hughes    for (size_t i = 0; g_ld_preloads[i] != NULL; ++i) {
21331728b2396591853345507a063ed6075dfd251706Elliott Hughes        g_ld_preloads[i]->CallConstructors();
2134326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng    }
2135326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng
2136d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    /* After the link_image, the si->load_bias is initialized.
2137d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
2138d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * We need to update this value for so exe here. So Unwind_Backtrace
2139d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * for some arch like x86 could work correctly within so exe.
21409c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     */
2141c5db969aa4a24a76fc0964f2ff89533e832b47a6Chao-Ying Fu    map->l_addr = si->load_bias;
2142d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
2143e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
21441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING
2145faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes    gettimeofday(&t1, NULL);
2146ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
21471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
2148faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes               (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
21491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
21501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
2151ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
2152bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocAbsolute],
2153bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocRelative],
2154bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocCopy],
2155bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocSymbol]);
21561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
21571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
21581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
21591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned n;
21601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned i;
21611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned count = 0;
2162d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        for (n = 0; n < 4096; n++) {
2163d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (bitmask[n]) {
21641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                unsigned x = bitmask[n];
2165e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__LP64__)
2166e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland                for (i = 0; i < 32; i++) {
2167e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#else
2168d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                for (i = 0; i < 8; i++) {
2169e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#endif
2170d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    if (x & 1) {
2171d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                        count++;
2172d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    }
21731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    x >>= 1;
21741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                }
21751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
21761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
2177ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
21781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
21791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
21801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
21811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES
21821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    fflush(stdout);
21831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
21841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2185c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
21861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si->entry;
21871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2188468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
2189bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only
2190bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library
2191bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself.
2192bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *
2193bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input:
2194bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    elf    -> address of ELF header, assumed to be at the start of the file.
2195bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return:
2196bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    load bias, i.e. add the value of any p_vaddr in the file to get
2197bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    the corresponding address in memory.
2198bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */
21990266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
22000266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) offset = elf->e_phoff;
2201faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes  const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
22020266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
2203fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
22040266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
2205fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (phdr->p_type == PT_LOAD) {
22060266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes      return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
2207bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    }
2208fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
2209fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return 0;
2210bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner}
2211bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
2212468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
2213468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This
2214468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and
2215468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation().
2216468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich *
2217468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own
2218468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern
2219468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault.
2220468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
22210266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesextern "C" ElfW(Addr) __linker_init(void* raw_args) {
2222d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  // Initialize static variables.
2223d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  solist = get_libdl_info();
2224d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  sonext = get_libdl_info();
2225d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov
222642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  KernelArgumentBlock args(raw_args);
222742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
22280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
22290266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
2230faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes  ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
223142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
223242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  soinfo linker_so;
223342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  memset(&linker_so, 0, sizeof(soinfo));
223442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
2235b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes  strcpy(linker_so.name, "[dynamic linker]");
223642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.base = linker_addr;
223742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
223842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
2239d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom  linker_so.dynamic = NULL;
224042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phdr = phdr;
224142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phnum = elf_hdr->e_phnum;
224242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.flags |= FLAG_LINKER;
224342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
2244183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)  if (!soinfo_link_image(&linker_so, NULL)) {
224542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // It would be nice to print an error message, but if the linker
224642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // can't link itself, there's no guarantee that we'll be able to
2247b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    // call write() (because it involves a GOT reference). We may as
2248b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    // well try though...
2249b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    const char* msg = "CANNOT LINK EXECUTABLE: ";
2250b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, msg, strlen(msg));
2251b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
2252b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    write(2, "\n", 1);
2253b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes    _exit(EXIT_FAILURE);
225442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  }
2255468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
225642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // We have successfully fixed our own relocations. It's safe to run
225742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // the main part of the linker now.
22581728b2396591853345507a063ed6075dfd251706Elliott Hughes  args.abort_message_ptr = &g_abort_message;
22590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes  ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
22605419b9474753d25dff947c7740532f86d130c0beElliott Hughes
2261d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov  protect_data(PROT_READ);
2262d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
226342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // Return the address that the calling assembly stub should jump to.
226442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  return start_address;
2265468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
2266