linker.cpp revision 489e498434f53269c44e3c13039eb630e86e1fd9
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 68512bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)static soinfo* load_library(const char* name, 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 713d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // Read the ELF header and load the segments. 71412bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) if (!elf_reader.Load(extinfo)) { 7154688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 71623363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner } 71723363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner 718489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat); 719650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes if (si == NULL) { 7204688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 72123363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner } 722650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes si->base = elf_reader.load_start(); 723650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes si->size = elf_reader.load_size(); 724650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes si->load_bias = elf_reader.load_bias(); 725650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes si->phnum = elf_reader.phdr_count(); 726650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes si->phdr = elf_reader.loaded_phdr(); 727d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 728d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // At this point we know that whatever is loaded @ base is a valid ELF 729d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // shared library whose segments are properly mapped in. 730d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov TRACE("[ find_library_internal base=%p size=%zu name='%s' ]", 731d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov reinterpret_cast<void*>(si->base), si->size, si->name); 732d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 733d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if (!soinfo_link_image(si, extinfo)) { 734d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov soinfo_free(si); 735d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return NULL; 736d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 737d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 738650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes return si; 7391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 7401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 741489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanovstatic soinfo *find_loaded_library_by_name(const char* name) { 742489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov const char* search_name = SEARCH_NAME(name); 743489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov for (soinfo* si = solist; si != NULL; si = si->next) { 744489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov if (!strcmp(search_name, si->name)) { 745489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov return si; 74612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel } 747489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov } 748489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov return NULL; 74912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel} 75012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel 75112bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)static soinfo* find_library_internal(const char* name, const android_dlextinfo* extinfo) { 752d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (name == NULL) { 753d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return somain; 754d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 755d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 756489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov soinfo* si = find_loaded_library_by_name(name); 757d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si != NULL) { 758d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si->flags & FLAG_LINKED) { 759d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return si; 760d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 761d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes DL_ERR("OOPS: recursive link to \"%s\"", si->name); 762d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return NULL; 763d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 76412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel 765ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ '%s' has not been loaded yet. Locating...]", name); 766d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return load_library(name, extinfo); 767d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes} 768d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 76912bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles)static soinfo* find_library(const char* name, const android_dlextinfo* extinfo) { 77012bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) soinfo* si = find_library_internal(name, extinfo); 771d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si != NULL) { 772ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->ref_count++; 773d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 774d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return si; 775d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes} 776d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 777d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic int soinfo_unload(soinfo* si) { 778ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes if (si->ref_count == 1) { 779ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("unloading '%s'", si->name); 780d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->CallDestructors(); 781d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 782d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if ((si->flags | FLAG_NEW_SOINFO) != 0) { 783d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov si->get_children().for_each([&] (soinfo* child) { 784d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov TRACE("%s needs to unload %s", si->name, child->name); 785d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov soinfo_unload(child); 786d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov }); 787d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } else { 788d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) { 789d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if (d->d_tag == DT_NEEDED) { 790d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov const char* library_name = si->strtab + d->d_un.d_val; 791d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov TRACE("%s needs to unload %s", si->name, library_name); 792489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov soinfo_unload(find_loaded_library_by_name(library_name)); 793d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 794d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 7951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 7961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 797d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes notify_gdb_of_unload(si); 798ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->ref_count = 0; 799d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov soinfo_free(si); 800d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } else { 801ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->ref_count--; 802c620059479c47a78d57086d73726c9adc2f337adElliott Hughes TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count); 803d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 804d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return 0; 8051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 8061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 807a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughesvoid do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) { 8081728b2396591853345507a063ed6075dfd251706Elliott Hughes snprintf(buffer, buffer_size, "%s:%s", kDefaultLdPaths[0], kDefaultLdPaths[1]); 809a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes} 810a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes 811cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesvoid do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) { 812cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes if (!get_AT_SECURE()) { 813cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes parse_LD_LIBRARY_PATH(ld_library_path); 814cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes } 815cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes} 816cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes 817012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles)soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) { 818e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) { 819e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes DL_ERR("invalid flags to dlopen: %x", flags); 820e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes return NULL; 821e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes } 822012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) if (extinfo != NULL && ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0)) { 823012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) DL_ERR("invalid extended flags to android_dlopen_ext: %x", extinfo->flags); 824012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) return NULL; 825012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) } 826d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov protect_data(PROT_READ | PROT_WRITE); 82712bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) soinfo* si = find_library(name, extinfo); 828d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si != NULL) { 829d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->CallConstructors(); 830d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov somain->add_child(si); 831d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 832d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov protect_data(PROT_READ); 833d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return si; 834d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes} 8351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 836d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesint do_dlclose(soinfo* si) { 837d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov protect_data(PROT_READ | PROT_WRITE); 838d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes int result = soinfo_unload(si); 839d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov protect_data(PROT_READ); 840d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return result; 8411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 8421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8434eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA) 8449918665a45095ad135576f005c0e5307feb366a1Chris Dearmanstatic int soinfo_relocate(soinfo* si, ElfW(Rela)* rela, unsigned count, soinfo* needed[]) { 8450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Sym)* s; 846c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes soinfo* lsi; 847c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 848c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes for (size_t idx = 0; idx < count; ++idx, ++rela) { 8490266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes unsigned type = ELFW(R_TYPE)(rela->r_info); 8500266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes unsigned sym = ELFW(R_SYM)(rela->r_info); 8510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + si->load_bias); 8520266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) sym_addr = 0; 853faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes const char* sym_name = NULL; 854c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 855c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DEBUG("Processing '%s' relocation at index %zd", si->name, idx); 856c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (type == 0) { // R_*_NONE 857c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes continue; 858c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 859c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (sym != 0) { 860c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name); 861c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes s = soinfo_do_lookup(si, sym_name, &lsi, needed); 862c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (s == NULL) { 863c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes // We only allow an undefined symbol if this is a weak reference... 864c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes s = &si->symtab[sym]; 865c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (ELF_ST_BIND(s->st_info) != STB_WEAK) { 866c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name); 867c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return -1; 868c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 869c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 870c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes /* IHI0044C AAELF 4.5.1.1: 871c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 872c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes Libraries are not searched to resolve weak references. 873c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes It is not an error for a weak reference to remain unsatisfied. 874c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 875c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes During linking, the value of an undefined weak reference is: 876c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes - Zero if the relocation type is absolute 877c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes - The address of the place if the relocation is pc-relative 878c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes - The address of nominal base address if the relocation 879c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes type is base-relative. 880c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes */ 881c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 882c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes switch (type) { 883e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__) 884e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_JUMP_SLOT: 885e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_GLOB_DAT: 886e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_ABS64: 887e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_ABS32: 888e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_ABS16: 889e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_RELATIVE: 890e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland /* 891e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland * The sym_addr was initialized to be zero above, or the relocation 892e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland * code below does not care about value of sym_addr. 893e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland * No need to do anything. 894e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland */ 895e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 896e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__) 897c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_JUMP_SLOT: 898c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_GLOB_DAT: 899c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_32: 900c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_RELATIVE: 901c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes // No need to do anything. 902c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 903c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_PC32: 904c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes sym_addr = reloc; 905c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 9064eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 907c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes default: 908faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx); 909c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return -1; 910c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 911c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } else { 912c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes // We got a definition. 9130266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias); 914c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 915c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes count_relocation(kRelocSymbol); 916c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } else { 917c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes s = NULL; 918c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 919c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 920c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes switch (type) { 921e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__) 922e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_JUMP_SLOT: 923e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocAbsolute); 924e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 9250266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n", 9260266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), sym_name); 9270266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend); 928e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 929e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_GLOB_DAT: 930e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocAbsolute); 931e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 9320266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n", 9330266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), sym_name); 9340266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend); 935e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 936e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_ABS64: 937e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocAbsolute); 938e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 9390266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n", 9400266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), sym_name); 9410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend); 942e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 943e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_ABS32: 944e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocAbsolute); 945e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 9460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n", 9470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), sym_name); 9480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) && 9490266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) { 9500266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend); 951e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } else { 9520266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", 9530266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)), 9540266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(INT32_MIN), 9550266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(UINT32_MAX)); 956e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland return -1; 957e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } 958e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 959e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_ABS16: 960e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocAbsolute); 961e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 9620266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n", 9630266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), sym_name); 9640266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) && 9650266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) { 9660266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend); 967e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } else { 9680266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", 9690266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)), 9700266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(INT16_MIN), 9710266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(UINT16_MAX)); 972e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland return -1; 973e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } 974e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 975e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_PREL64: 976e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocRelative); 977e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 9780266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n", 9790266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name); 9800266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset; 981e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 982e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_PREL32: 983e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocRelative); 984e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 9850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n", 9860266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name); 9870266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) && 9880266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) { 9890266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset); 990e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } else { 9910266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", 9920266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)), 9930266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(INT32_MIN), 9940266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(UINT32_MAX)); 995e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland return -1; 996e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } 997e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 998e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_PREL16: 999e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocRelative); 1000e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 10010266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n", 10020266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name); 10030266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) && 10040266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) { 10050266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset); 1006e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } else { 10070266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", 10080266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)), 10090266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(INT16_MIN), 10100266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(UINT16_MAX)); 1011e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland return -1; 1012e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } 1013e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1014e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland 1015e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_RELATIVE: 1016e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocRelative); 1017e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 1018e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland if (sym) { 1019e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland DL_ERR("odd RELATIVE form..."); 1020e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland return -1; 1021e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } 10220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n", 10230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (si->base + rela->r_addend)); 10240266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = (si->base + rela->r_addend); 1025e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1026e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland 1027e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_COPY: 1028b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov /* 1029b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov * ET_EXEC is not supported so this should not happen. 1030b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov * 1031b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf 1032b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov * 1033b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov * Section 4.7.1.10 "Dynamic relocations" 1034b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov * R_AARCH64_COPY may only appear in executable objects where e_type is 1035b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov * set to ET_EXEC. 1036b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov */ 1037b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov DL_ERR("%s R_AARCH64_COPY relocations are not supported", si->name); 1038b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov return -1; 1039e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_TLS_TPREL64: 10400266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n", 10410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), rela->r_offset); 1042e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1043e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_TLS_DTPREL32: 10440266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n", 10450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), rela->r_offset); 1046e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1047e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__) 1048c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_JUMP_SLOT: 1049c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes count_relocation(kRelocAbsolute); 1050c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes MARK(rela->r_offset); 1051c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc), 1052c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes static_cast<size_t>(sym_addr + rela->r_addend), sym_name); 10530266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend; 1054c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1055c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_GLOB_DAT: 1056c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes count_relocation(kRelocAbsolute); 1057c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes MARK(rela->r_offset); 1058c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc), 1059c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes static_cast<size_t>(sym_addr + rela->r_addend), sym_name); 10600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend; 1061c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1062c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_RELATIVE: 1063c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes count_relocation(kRelocRelative); 1064c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes MARK(rela->r_offset); 1065c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (sym) { 1066c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DL_ERR("odd RELATIVE form..."); 1067c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return -1; 1068c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1069c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc), 1070c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes static_cast<size_t>(si->base)); 10710266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = si->base + rela->r_addend; 1072c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1073c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_32: 1074c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes count_relocation(kRelocRelative); 1075c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes MARK(rela->r_offset); 1076c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc), 1077c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes static_cast<size_t>(sym_addr), sym_name); 10780266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend; 1079c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1080c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin case R_X86_64_64: 1081c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin count_relocation(kRelocRelative); 1082c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin MARK(rela->r_offset); 1083c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc), 1084c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin static_cast<size_t>(sym_addr), sym_name); 10850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend; 1086c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin break; 1087c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_PC32: 1088c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes count_relocation(kRelocRelative); 1089c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes MARK(rela->r_offset); 1090c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s", 1091c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc), 1092c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name); 10930266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc; 1094c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 10954eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 1096e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland 1097c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes default: 1098faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx); 1099c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return -1; 1100c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1101c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1102c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return 0; 1103c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes} 11049918665a45095ad135576f005c0e5307feb366a1Chris Dearman 11059918665a45095ad135576f005c0e5307feb366a1Chris Dearman#else // REL, not RELA. 11069918665a45095ad135576f005c0e5307feb366a1Chris Dearman 11070266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic int soinfo_relocate(soinfo* si, ElfW(Rel)* rel, unsigned count, soinfo* needed[]) { 11080266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Sym)* s; 1109d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom soinfo* lsi; 11101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes for (size_t idx = 0; idx < count; ++idx, ++rel) { 11120266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes unsigned type = ELFW(R_TYPE)(rel->r_info); 11130266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead. 11140266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes unsigned sym = ELFW(R_SYM)(rel->r_info); 11150266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + si->load_bias); 11160266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) sym_addr = 0; 1117faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes const char* sym_name = NULL; 11181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1119c620059479c47a78d57086d73726c9adc2f337adElliott Hughes DEBUG("Processing '%s' relocation at index %zd", si->name, idx); 1120d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if (type == 0) { // R_*_NONE 1121d7daacb46372132ae3f0121647074936c304b572Raghu Gandham continue; 1122d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1123d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (sym != 0) { 1124c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name); 11255ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel s = soinfo_do_lookup(si, sym_name, &lsi, needed); 1126d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (s == NULL) { 11274eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // We only allow an undefined symbol if this is a weak reference... 1128c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes s = &si->symtab[sym]; 1129c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (ELF_ST_BIND(s->st_info) != STB_WEAK) { 1130e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name); 1131e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan return -1; 1132e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan } 1133e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1134e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan /* IHI0044C AAELF 4.5.1.1: 1135e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1136e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan Libraries are not searched to resolve weak references. 1137e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan It is not an error for a weak reference to remain 1138e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan unsatisfied. 1139e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1140e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan During linking, the value of an undefined weak reference is: 1141e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan - Zero if the relocation type is absolute 1142e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan - The address of the place if the relocation is pc-relative 1143bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes - The address of nominal base address if the relocation 1144e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan type is base-relative. 1145e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan */ 1146e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1147e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan switch (type) { 11484eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__) 1149e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_JUMP_SLOT: 1150e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_GLOB_DAT: 1151e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_ABS32: 1152e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_RELATIVE: /* Don't care. */ 11534eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // sym_addr was initialized to be zero above or relocation 11544eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // code below does not care about value of sym_addr. 11554eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // No need to do anything. 11564eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 11574eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__) 1158d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case R_386_JMP_SLOT: 1159e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_386_GLOB_DAT: 1160e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_386_32: 1161c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_386_RELATIVE: /* Don't care. */ 11624eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // sym_addr was initialized to be zero above or relocation 11634eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // code below does not care about value of sym_addr. 11644eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // No need to do anything. 1165e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan break; 1166e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_386_PC32: 1167e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan sym_addr = reloc; 1168e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan break; 11694eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 1170e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 11714eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__) 1172e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_COPY: 11734eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // Fall through. Can't really copy if weak symbol is not found at run-time. 11744eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 1175e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan default: 1176faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx); 1177e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan return -1; 1178e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan } 1179e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan } else { 11804eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // We got a definition. 11810266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias); 1182d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1183bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocSymbol); 11841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 1185e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan s = NULL; 11861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 11871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1188c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes switch (type) { 11894eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__) 11901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_ARM_JUMP_SLOT: 1191bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocAbsolute); 11921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 1193ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name); 11940266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr; 11951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 11961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_ARM_GLOB_DAT: 1197bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocAbsolute); 11981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 1199ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name); 12000266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr; 12011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 12021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_ARM_ABS32: 1203bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocAbsolute); 12041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 1205ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name); 12060266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr; 12071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 120834ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner case R_ARM_REL32: 1209bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocRelative); 121034ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner MARK(rel->r_offset); 1211ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s", 121234ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner reloc, sym_addr, rel->r_offset, sym_name); 12130266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset; 121434ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner break; 12151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_ARM_COPY: 1216b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov /* 12176275f2083415d22a6ce0de55645079cd47e0cc80Dmitriy Ivanov * ET_EXEC is not supported so this should not happen. 12186275f2083415d22a6ce0de55645079cd47e0cc80Dmitriy Ivanov * 1219b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf 1220b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov * 1221b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov * Section 4.7.1.10 "Dynamic relocations" 1222b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov * R_ARM_COPY may only appear in executable objects where e_type is 1223b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov * set to ET_EXEC. 1224b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov */ 1225b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov DL_ERR("%s R_ARM_COPY relocations are not supported", si->name); 1226b906e13c55c9fe9b4157ba548534a0230434882bDmitriy Ivanov return -1; 12274eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__) 12284eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_386_JMP_SLOT: 12294eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes count_relocation(kRelocAbsolute); 12304eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes MARK(rel->r_offset); 12314eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name); 12320266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr; 12334eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 12344eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_386_GLOB_DAT: 12354eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes count_relocation(kRelocAbsolute); 12364eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes MARK(rel->r_offset); 12374eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name); 12380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr; 12394eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 12404eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_386_32: 12414eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes count_relocation(kRelocRelative); 12424eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes MARK(rel->r_offset); 12434eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name); 12440266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr; 12454eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 12464eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_386_PC32: 12474eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes count_relocation(kRelocRelative); 12484eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes MARK(rel->r_offset); 12494eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s", 12504eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes reloc, (sym_addr - reloc), sym_addr, reloc, sym_name); 12510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc); 12524eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 12534eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__mips__) 12544eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_MIPS_REL32: 12559918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__LP64__) 12569918665a45095ad135576f005c0e5307feb366a1Chris Dearman // MIPS Elf64_Rel entries contain compound relocations 12579918665a45095ad135576f005c0e5307feb366a1Chris Dearman // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case 12589918665a45095ad135576f005c0e5307feb366a1Chris Dearman if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 || 12599918665a45095ad135576f005c0e5307feb366a1Chris Dearman ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) { 1260faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)", 12619918665a45095ad135576f005c0e5307feb366a1Chris Dearman type, (unsigned)ELF64_R_TYPE2(rel->r_info), 1262faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx); 12639918665a45095ad135576f005c0e5307feb366a1Chris Dearman return -1; 12649918665a45095ad135576f005c0e5307feb366a1Chris Dearman } 12659918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif 12664eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes count_relocation(kRelocAbsolute); 12674eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes MARK(rel->r_offset); 12689918665a45095ad135576f005c0e5307feb366a1Chris Dearman TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc), 12699918665a45095ad135576f005c0e5307feb366a1Chris Dearman static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*"); 12704eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes if (s) { 12710266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr; 12724eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes } else { 12730266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base; 12744eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes } 12754eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 12764eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 12774eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes 12784eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__) 12794eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_ARM_RELATIVE: 12804eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__) 12814eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_386_RELATIVE: 12824eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 12834eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes count_relocation(kRelocRelative); 12844eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes MARK(rel->r_offset); 12854eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes if (sym) { 12864eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes DL_ERR("odd RELATIVE form..."); 12874eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes return -1; 12884eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes } 12894eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p", 12904eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base)); 12910266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base; 12924eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 12931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 12941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project default: 1295faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx); 12961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return -1; 12971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 12981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 12991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 13001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 1301c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 13021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 13034eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__) 130487c358524e479235aa6241736d2ce325f89daafcBrian Carlstromstatic bool mips_relocate_got(soinfo* si, soinfo* needed[]) { 13059918665a45095ad135576f005c0e5307feb366a1Chris Dearman ElfW(Addr)** got = si->plt_got; 130687c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom if (got == NULL) { 130787c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom return true; 130887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom } 130987c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom unsigned local_gotno = si->mips_local_gotno; 131087c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom unsigned gotsym = si->mips_gotsym; 131187c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom unsigned symtabno = si->mips_symtabno; 13120266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Sym)* symtab = si->symtab; 1313d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 13149918665a45095ad135576f005c0e5307feb366a1Chris Dearman // got[0] is the address of the lazy resolver function. 13159918665a45095ad135576f005c0e5307feb366a1Chris Dearman // got[1] may be used for a GNU extension. 13169918665a45095ad135576f005c0e5307feb366a1Chris Dearman // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start). 13179918665a45095ad135576f005c0e5307feb366a1Chris Dearman // FIXME: maybe this should be in a separate routine? 1318d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if ((si->flags & FLAG_LINKER) == 0) { 131987c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom size_t g = 0; 13209918665a45095ad135576f005c0e5307feb366a1Chris Dearman got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef); 13219918665a45095ad135576f005c0e5307feb366a1Chris Dearman if (reinterpret_cast<intptr_t>(got[g]) < 0) { 13229918665a45095ad135576f005c0e5307feb366a1Chris Dearman got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed); 1323d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 13249918665a45095ad135576f005c0e5307feb366a1Chris Dearman // Relocate the local GOT entries. 1325d7daacb46372132ae3f0121647074936c304b572Raghu Gandham for (; g < local_gotno; g++) { 13269918665a45095ad135576f005c0e5307feb366a1Chris Dearman got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + si->load_bias); 1327d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1328d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1329d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 13309918665a45095ad135576f005c0e5307feb366a1Chris Dearman // Now for the global GOT entries... 13310266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Sym)* sym = symtab + gotsym; 1332d7daacb46372132ae3f0121647074936c304b572Raghu Gandham got = si->plt_got + local_gotno; 133387c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom for (size_t g = gotsym; g < symtabno; g++, sym++, got++) { 13349918665a45095ad135576f005c0e5307feb366a1Chris Dearman // This is an undefined reference... try to locate it. 13359918665a45095ad135576f005c0e5307feb366a1Chris Dearman const char* sym_name = si->strtab + sym->st_name; 1336d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom soinfo* lsi; 13379918665a45095ad135576f005c0e5307feb366a1Chris Dearman ElfW(Sym)* s = soinfo_do_lookup(si, sym_name, &lsi, needed); 1338d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if (s == NULL) { 13399918665a45095ad135576f005c0e5307feb366a1Chris Dearman // We only allow an undefined symbol if this is a weak reference. 1340d7daacb46372132ae3f0121647074936c304b572Raghu Gandham s = &symtab[g]; 1341c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (ELF_ST_BIND(s->st_info) != STB_WEAK) { 13424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("cannot locate \"%s\"...", sym_name); 134387c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom return false; 1344d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1345d7daacb46372132ae3f0121647074936c304b572Raghu Gandham *got = 0; 13469918665a45095ad135576f005c0e5307feb366a1Chris Dearman } else { 13479918665a45095ad135576f005c0e5307feb366a1Chris Dearman // FIXME: is this sufficient? 13489918665a45095ad135576f005c0e5307feb366a1Chris Dearman // For reference see NetBSD link loader 13499918665a45095ad135576f005c0e5307feb366a1Chris 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 13509918665a45095ad135576f005c0e5307feb366a1Chris Dearman *got = reinterpret_cast<ElfW(Addr)*>(lsi->load_bias + s->st_value); 1351d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1352d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 135387c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom return true; 1354d7daacb46372132ae3f0121647074936c304b572Raghu Gandham} 1355d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif 1356d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1357812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallArray(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) { 1358ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes if (functions == NULL) { 1359d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return; 1360d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 13618215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner 1362c620059479c47a78d57086d73726c9adc2f337adElliott Hughes TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name); 13638215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner 1364ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes int begin = reverse ? (count - 1) : 0; 1365ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes int end = reverse ? -1 : count; 1366ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes int step = reverse ? -1 : 1; 13678215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner 1368ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes for (int i = begin; i != end; i += step) { 1369ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]); 1370ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes CallFunction("function", functions[i]); 1371d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 1372d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 1373ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ Done calling %s for '%s' ]", array_name, name); 13741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 13751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1376812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallFunction(const char* function_name __unused, linker_function_t function) { 1377db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) { 1378d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return; 1379d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 1380d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 1381ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name); 1382d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes function(); 1383ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name); 1384db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes 1385db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures 1386db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes // are still writable. This happens with our debug malloc (see http://b/7941716). 1387d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov protect_data(PROT_READ | PROT_WRITE); 13889181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov} 13899181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov 1390d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallPreInitConstructors() { 13918147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // DT_PREINIT_ARRAY functions are called before any other constructors for executables, 13928147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // but ignored in a shared library. 1393d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false); 1394d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes} 1395e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov 1396d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallConstructors() { 1397d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (constructors_called) { 1398d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return; 1399d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 1400e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov 1401d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // We set constructors_called before actually calling the constructors, otherwise it doesn't 1402d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // protect against recursive constructor calls. One simple example of constructor recursion 1403d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so: 1404d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // 1. The program depends on libc, so libc's constructor is called here. 1405d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so. 1406d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // 3. dlopen() calls the constructors on the newly created 1407d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // soinfo for libc_malloc_debug_leak.so. 1408d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // 4. The debug .so depends on libc, so CallConstructors is 1409d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // called again with the libc soinfo. If it doesn't trigger the early- 1410d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // out above, the libc constructor will be called again (recursively!). 1411d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes constructors_called = true; 1412d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 14138147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) { 14148147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // The GNU dynamic linker silently ignores these, but we warn the developer. 1415c620059479c47a78d57086d73726c9adc2f337adElliott Hughes PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!", 14168147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes name, preinit_array_count); 1417d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 14181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1419d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov get_children().for_each([] (soinfo* si) { 1420d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov si->CallConstructors(); 1421d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov }); 14221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14238147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes TRACE("\"%s\": calling constructors", name); 14248147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes 14258147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // DT_INIT should be called before DT_INIT_ARRAY if both are present. 1426d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallFunction("DT_INIT", init_func); 1427d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallArray("DT_INIT_ARRAY", init_array, init_array_count, false); 1428e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov} 14298215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner 1430d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallDestructors() { 14318147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes TRACE("\"%s\": calling destructors", name); 14328147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes 14338147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // DT_FINI_ARRAY must be parsed in reverse order. 1434d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true); 14358147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes 14368147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // DT_FINI should be called after DT_FINI_ARRAY if both are present. 1437d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallFunction("DT_FINI", fini_func); 14381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 14391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1440d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::add_child(soinfo* child) { 1441d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if ((this->flags & FLAG_NEW_SOINFO) == 0) { 1442d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return; 1443d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1444d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1445d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov this->children.push_front(child); 1446d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov child->parents.push_front(this); 1447d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 1448d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1449d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::remove_all_links() { 1450d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if ((this->flags & FLAG_NEW_SOINFO) == 0) { 1451d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return; 1452d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1453d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1454d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // 1. Untie connected soinfos from 'this'. 1455d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov children.for_each([&] (soinfo* child) { 1456d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov child->parents.remove_if([&] (const soinfo* parent) { 1457d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return parent == this; 1458d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov }); 1459d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov }); 1460d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1461d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov parents.for_each([&] (soinfo* parent) { 1462d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov parent->children.for_each([&] (const soinfo* child) { 1463d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return child == this; 1464d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov }); 1465d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov }); 1466d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1467d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // 2. Once everything untied - clear local lists. 1468d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov parents.clear(); 1469d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov children.clear(); 1470d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 1471d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1472d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::set_st_dev(dev_t dev) { 1473d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if ((this->flags & FLAG_NEW_SOINFO) == 0) { 1474d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return; 1475d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1476d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1477d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov st_dev = dev; 1478d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 1479d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1480d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::set_st_ino(ino_t ino) { 1481d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if ((this->flags & FLAG_NEW_SOINFO) == 0) { 1482d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return; 1483d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1484d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1485d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov st_ino = ino; 1486d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 1487d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1488d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovdev_t soinfo::get_st_dev() { 1489d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if ((this->flags & FLAG_NEW_SOINFO) == 0) { 1490d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return 0; 1491d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1492d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1493d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return st_dev; 1494d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}; 1495d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1496d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovino_t soinfo::get_st_ino() { 1497d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if ((this->flags & FLAG_NEW_SOINFO) == 0) { 1498d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return 0; 1499d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1500d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1501d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return st_ino; 1502d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 1503d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1504d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov// This is a return on get_children() in case 1505d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov// 'this->flags' does not have FLAG_NEW_SOINFO set. 1506d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo::soinfo_list_t g_empty_list; 1507d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1508d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovsoinfo::soinfo_list_t& soinfo::get_children() { 1509d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if ((this->flags & FLAG_NEW_SOINFO) == 0) { 1510d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return g_empty_list; 1511d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1512d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1513d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return this->children; 1514d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 1515d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 15161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with 15171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /dev/null. */ 15185419b9474753d25dff947c7740532f86d130c0beElliott Hughesstatic int nullify_closed_stdio() { 15191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int dev_null, i, status; 15201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int return_value = 0; 15211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 152216084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)); 15231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (dev_null < 0) { 15244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("cannot open /dev/null: %s", strerror(errno)); 15251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return -1; 15261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1527ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null); 15281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* If any of the stdio file descriptors is valid and not associated 15301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project with /dev/null, dup /dev/null to it. */ 15311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (i = 0; i < 3; i++) { 15321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* If it is /dev/null already, we are done. */ 15334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (i == dev_null) { 15341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project continue; 15354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 15361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1537ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ Nullifying stdio file descriptor %d]", i); 15384688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL)); 15391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes /* If file is opened, we are good. */ 15414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (status != -1) { 15424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes continue; 15434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 15441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* The only error we allow is that the file descriptor does not 15461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project exist, in which case we dup /dev/null to it. */ 15471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (errno != EBADF) { 15484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("fcntl failed: %s", strerror(errno)); 15491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return_value = -1; 15501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project continue; 15511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 15521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* Try dupping /dev/null to this stdio file descriptor and 15541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project repeat if there is a signal. Note that any errors in closing 15551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project the stdio descriptor are lost. */ 15564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes status = TEMP_FAILURE_RETRY(dup2(dev_null, i)); 15571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (status < 0) { 15584688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("dup2 failed: %s", strerror(errno)); 15591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return_value = -1; 15601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project continue; 15611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 15621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 15631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* If /dev/null is not one of the stdio file descriptors, close it. */ 15651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (dev_null > 2) { 1566ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null); 15674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes status = TEMP_FAILURE_RETRY(close(dev_null)); 15684688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (status == -1) { 15694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("close failed: %s", strerror(errno)); 15701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return_value = -1; 15711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 15721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 15731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return return_value; 15751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 15761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1577183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo) { 1578f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee /* "base" might wrap around UINT32_MAX. */ 15790266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) base = si->load_bias; 15800266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes const ElfW(Phdr)* phdr = si->phdr; 15811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int phnum = si->phnum; 1582d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom bool relocating_linker = (si->flags & FLAG_LINKER) != 0; 15831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1584b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner /* We can't debug anything until the linker is relocated */ 1585b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (!relocating_linker) { 1586ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes INFO("[ linking %s ]", si->name); 1587c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags); 1588b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner } 15891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 159063f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner /* Extract dynamic section */ 1591124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes size_t dynamic_count; 15920266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Word) dynamic_flags; 159312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic, 1594cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman &dynamic_count, &dynamic_flags); 159563f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner if (si->dynamic == NULL) { 1596b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (!relocating_linker) { 1597124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name); 1598b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner } 1599124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 160063f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner } else { 1601b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (!relocating_linker) { 1602ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes DEBUG("dynamic = %p", si->dynamic); 1603b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner } 160463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner } 160563f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner 16064eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__) 160763f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner (void) phdr_table_get_arm_exidx(phdr, phnum, base, 160863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner &si->ARM_exidx, &si->ARM_exidx_count); 160963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif 161063f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner 16118147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // Extract useful information from dynamic section. 1612d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom uint32_t needed_count = 0; 16130266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) { 1614c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p", 1615c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val)); 1616c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes switch (d->d_tag) { 16171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_HASH: 1618faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->nbucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[0]; 1619faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->nchain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[1]; 1620faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->bucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8); 1621faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->chain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8 + si->nbucket * 4); 16221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 16231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_STRTAB: 1624faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->strtab = reinterpret_cast<const char*>(base + d->d_un.d_ptr); 16251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 16261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_SYMTAB: 1627faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->symtab = reinterpret_cast<ElfW(Sym)*>(base + d->d_un.d_ptr); 16281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 16294eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if !defined(__LP64__) 16301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PLTREL: 1631d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (d->d_un.d_val != DT_REL) { 1632124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("unsupported DT_RELA in \"%s\"", si->name); 1633124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 16341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 16351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 1636c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 16371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_JMPREL: 16384eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA) 1639faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->plt_rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr); 1640c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else 1641faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->plt_rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr); 1642c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 16431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 16441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PLTRELSZ: 16454eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA) 16460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->plt_rela_count = d->d_un.d_val / sizeof(ElfW(Rela)); 1647c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else 16480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->plt_rel_count = d->d_un.d_val / sizeof(ElfW(Rel)); 1649c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 16501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 16519918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__mips__) 16521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PLTGOT: 16539918665a45095ad135576f005c0e5307feb366a1Chris Dearman // Used by mips and mips64. 16549918665a45095ad135576f005c0e5307feb366a1Chris Dearman si->plt_got = reinterpret_cast<ElfW(Addr)**>(base + d->d_un.d_ptr); 16551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 1656c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 16571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_DEBUG: 1658bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes // Set the DT_DEBUG entry to the address of _r_debug for GDB 1659cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman // if the dynamic table is writable 16609918665a45095ad135576f005c0e5307feb366a1Chris Dearman// FIXME: not working currently for N64 16619918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The flags for the LOAD and DYNAMIC program headers do not agree. 16629918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The LOAD section containng the dynamic table has been mapped as 16639918665a45095ad135576f005c0e5307feb366a1Chris Dearman// read-only, but the DYNAMIC header claims it is writable. 16649918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if !(defined(__mips__) && defined(__LP64__)) 166599c32055cb50443f722c55914869c1f6bb941959Elliott Hughes if ((dynamic_flags & PF_W) != 0) { 1666c620059479c47a78d57086d73726c9adc2f337adElliott Hughes d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug); 166799c32055cb50443f722c55914869c1f6bb941959Elliott Hughes } 16681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 16699918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif 16704eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA) 1671c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case DT_RELA: 1672faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr); 1673c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1674c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case DT_RELASZ: 16750266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->rela_count = d->d_un.d_val / sizeof(ElfW(Rela)); 1676c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1677c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case DT_REL: 1678c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DL_ERR("unsupported DT_REL in \"%s\"", si->name); 1679c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return false; 1680c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case DT_RELSZ: 1681c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name); 1682c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return false; 1683c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else 1684c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case DT_REL: 1685faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr); 1686c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1687c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case DT_RELSZ: 16880266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->rel_count = d->d_un.d_val / sizeof(ElfW(Rel)); 1689c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1690ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI case DT_RELA: 1691124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("unsupported DT_RELA in \"%s\"", si->name); 1692124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1693c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 16941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_INIT: 1695ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr); 16968147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func); 16971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 16981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_FINI: 1699ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr); 17008147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func); 17011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 17021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_INIT_ARRAY: 1703ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr); 17048147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array); 17051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 17061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_INIT_ARRAYSZ: 17070266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr)); 17081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 17091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_FINI_ARRAY: 1710ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr); 17118147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array); 17121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 17131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_FINI_ARRAYSZ: 17140266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr)); 17151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 17161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PREINIT_ARRAY: 1717ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr); 17188147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array); 17191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 17201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PREINIT_ARRAYSZ: 17210266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr)); 17221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 17231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_TEXTREL: 1724e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__) 1725e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name); 1726e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes return false; 1727e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else 17285135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich si->has_text_relocations = true; 17291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 1730e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif 17315ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel case DT_SYMBOLIC: 17325ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel si->has_DT_SYMBOLIC = true; 17335ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel break; 1734d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom case DT_NEEDED: 1735d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom ++needed_count; 1736d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom break; 17375ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel case DT_FLAGS: 1738d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (d->d_un.d_val & DF_TEXTREL) { 1739e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__) 1740e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name); 1741e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes return false; 1742e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else 17435ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel si->has_text_relocations = true; 1744e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif 17455ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 1746d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (d->d_un.d_val & DF_SYMBOLIC) { 17475ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel si->has_DT_SYMBOLIC = true; 17485ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 17495ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel break; 17504eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__) 1751d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_STRSZ: 1752d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_SYMENT: 1753d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_RELENT: 1754d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1755d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_RLD_MAP: 1756bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB. 1757d7daacb46372132ae3f0121647074936c304b572Raghu Gandham { 1758006f9ad8910c945cd50e54f6c34d8c27b11c1193Benjamin Adolphi r_debug** dp = reinterpret_cast<r_debug**>(base + d->d_un.d_ptr); 1759d7daacb46372132ae3f0121647074936c304b572Raghu Gandham *dp = &_r_debug; 1760d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1761d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1762d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_RLD_VERSION: 1763d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_FLAGS: 1764d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_BASE_ADDRESS: 1765d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_UNREFEXTNO: 1766d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1767d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1768d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_SYMTABNO: 1769d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom si->mips_symtabno = d->d_un.d_val; 1770d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1771d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1772d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_LOCAL_GOTNO: 1773d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom si->mips_local_gotno = d->d_un.d_val; 1774d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1775d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1776d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_GOTSYM: 1777d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom si->mips_gotsym = d->d_un.d_val; 1778d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 17794eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 1780d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1781d7daacb46372132ae3f0121647074936c304b572Raghu Gandham default: 17824eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes DEBUG("Unused DT entry: type %p arg %p", 17834eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val)); 1784d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 17851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1788c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p", 1789c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes reinterpret_cast<void*>(si->base), si->strtab, si->symtab); 17901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1791124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes // Sanity checks. 1792d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (relocating_linker && needed_count != 0) { 1793d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries"); 1794d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom return false; 1795d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom } 1796124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (si->nbucket == 0) { 1797124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name); 1798124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1799124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 1800124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (si->strtab == 0) { 1801124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name); 1802124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1803124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 1804124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (si->symtab == 0) { 1805124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name); 1806124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 18071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 18081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 18097e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes // If this is the main executable, then load all of the libraries from LD_PRELOAD now. 1810d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si->flags & FLAG_EXE) { 18111728b2396591853345507a063ed6075dfd251706Elliott Hughes memset(g_ld_preloads, 0, sizeof(g_ld_preloads)); 18127e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes size_t preload_count = 0; 18131728b2396591853345507a063ed6075dfd251706Elliott Hughes for (size_t i = 0; g_ld_preload_names[i] != NULL; i++) { 18141728b2396591853345507a063ed6075dfd251706Elliott Hughes soinfo* lsi = find_library(g_ld_preload_names[i], NULL); 18157e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes if (lsi != NULL) { 18161728b2396591853345507a063ed6075dfd251706Elliott Hughes g_ld_preloads[preload_count++] = lsi; 18177e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes } else { 18187e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes // As with glibc, failure to load an LD_PRELOAD library is just a warning. 18197e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s", 18201728b2396591853345507a063ed6075dfd251706Elliott Hughes g_ld_preload_names[i], si->name, linker_get_error_buffer()); 18214fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer } 18224fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer } 18234fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer } 18244fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 1825faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes soinfo** needed = reinterpret_cast<soinfo**>(alloca((1 + needed_count) * sizeof(soinfo*))); 1826d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom soinfo** pneeded = needed; 182712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel 18280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) { 1829d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (d->d_tag == DT_NEEDED) { 1830d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom const char* library_name = si->strtab + d->d_un.d_val; 1831ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes DEBUG("%s needs %s", si->name, library_name); 183212bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) soinfo* lsi = find_library(library_name, NULL); 1833d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (lsi == NULL) { 1834650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf)); 18354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s", 1836d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom library_name, si->name, tmp_err_buf); 1837124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 18381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1839d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1840d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov si->add_child(lsi); 184112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel *pneeded++ = lsi; 18421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 18431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 184412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel *pneeded = NULL; 18451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1846e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__) 18475135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich if (si->has_text_relocations) { 1848e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes // Make segments writable to allow text relocations to work properly. We will later call 1849e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes // phdr_table_protect_segments() after all of them are applied and all constructors are run. 1850865119efd47ae3cae5a0c09c6a9cc184a10614c1Du Chenyang#if !defined(__i386__) // The platform itself has too many text relocations on x86. 18513d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich DL_WARN("%s has text relocations. This is wasting memory and prevents " 18523d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich "security hardening. Please fix.", si->name); 1853865119efd47ae3cae5a0c09c6a9cc184a10614c1Du Chenyang#endif 18545135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) { 18555135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich DL_ERR("can't unprotect loadable segments for \"%s\": %s", 18565135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich si->name, strerror(errno)); 1857124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 18585135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich } 18595135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich } 1860e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif 18615135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich 18624eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA) 1863c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (si->plt_rela != NULL) { 1864faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DEBUG("[ relocating %s plt ]\n", si->name); 18659918665a45095ad135576f005c0e5307feb366a1Chris Dearman if (soinfo_relocate(si, si->plt_rela, si->plt_rela_count, needed)) { 1866c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return false; 1867c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1868c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1869c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (si->rela != NULL) { 1870faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DEBUG("[ relocating %s ]\n", si->name); 18719918665a45095ad135576f005c0e5307feb366a1Chris Dearman if (soinfo_relocate(si, si->rela, si->rela_count, needed)) { 1872c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return false; 1873c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1874c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1875c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else 1876d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (si->plt_rel != NULL) { 1877faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DEBUG("[ relocating %s plt ]", si->name); 1878d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) { 1879124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1880124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 18811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1882d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (si->rel != NULL) { 1883faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DEBUG("[ relocating %s ]", si->name); 1884d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (soinfo_relocate(si, si->rel, si->rel_count, needed)) { 1885124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1886124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 18871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1888c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 18891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 18904eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__) 189187c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom if (!mips_relocate_got(si, needed)) { 1892124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1893d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1894d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif 1895d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 18961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->flags |= FLAG_LINKED; 1897ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes DEBUG("[ finished linking %s ]", si->name); 18981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1899e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__) 19005135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich if (si->has_text_relocations) { 1901e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes // All relocations are done, we can protect our segments back to read-only. 19025135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) { 19035135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich DL_ERR("can't protect segments for \"%s\": %s", 19045135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich si->name, strerror(errno)); 1905124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 19065135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich } 19071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1908e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif 19091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1910b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner /* We can also turn on GNU RELRO protection */ 1911b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) { 19124688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("can't enable GNU RELRO protection for \"%s\": %s", 19134688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes si->name, strerror(errno)); 1914124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 19159ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich } 19169ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich 1917183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) /* Handle serializing/sharing the RELRO segment */ 1918183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) { 1919183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) if (phdr_table_serialize_gnu_relro(si->phdr, si->phnum, si->load_bias, 1920183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) extinfo->relro_fd) < 0) { 1921183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) DL_ERR("failed serializing GNU RELRO section for \"%s\": %s", 1922183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) si->name, strerror(errno)); 1923183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) return false; 1924183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) } 1925183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) { 1926183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) if (phdr_table_map_gnu_relro(si->phdr, si->phnum, si->load_bias, 1927183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) extinfo->relro_fd) < 0) { 1928183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) DL_ERR("failed mapping GNU RELRO section for \"%s\": %s", 1929183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) si->name, strerror(errno)); 1930183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) return false; 1931183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) } 1932183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) } 1933183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) 19341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project notify_gdb_of_load(si); 1935124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return true; 19361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 19371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1938468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/* 1939c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * This function add vdso to internal dso list. 1940c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * It helps to stack unwinding through signal handlers. 1941c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * Also, it makes bionic more like glibc. 1942c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov */ 1943812fd4263a005b88f3b4222baa910114f938d594Kito Chengstatic void add_vdso(KernelArgumentBlock& args __unused) { 19444eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(AT_SYSINFO_EHDR) 19450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR)); 19460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes if (ehdr_vdso == NULL) { 19470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes return; 19480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes } 1949c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov 1950d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov soinfo* si = soinfo_alloc("[vdso]", NULL); 1951ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov 19520266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff); 19530266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->phnum = ehdr_vdso->e_phnum; 19540266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso); 19550266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->size = phdr_table_get_load_size(si->phdr, si->phnum); 19560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->load_bias = get_elf_exec_load_bias(ehdr_vdso); 1957ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov 19580dcf06f976670b5d05286b184d150f61b71c1184Torne (Richard Coles) soinfo_link_image(si, NULL); 1959c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov#endif 1960c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov} 1961c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov 1962c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov/* 1963d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * This is linker soinfo for GDB. See details below. 1964d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov */ 1965d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo linker_soinfo_for_gdb; 1966d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1967d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov/* gdb expects the linker to be in the debug shared object list. 1968d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * Without this, gdb has trouble locating the linker's ".text" 1969d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * and ".plt" sections. Gdb could also potentially use this to 1970d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * relocate the offset of our exported 'rtld_db_dlactivity' symbol. 1971d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * Don't use soinfo_alloc(), because the linker shouldn't 1972d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * be on the soinfo list. 1973d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov */ 1974d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic void init_linker_info_for_gdb(ElfW(Addr) linker_base) { 1975d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov#if defined(__LP64__) 1976d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov strlcpy(linker_soinfo_for_gdb.name, "/system/bin/linker64", sizeof(linker_soinfo_for_gdb.name)); 1977d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov#else 1978d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov strlcpy(linker_soinfo_for_gdb.name, "/system/bin/linker", sizeof(linker_soinfo_for_gdb.name)); 1979d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov#endif 1980d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov linker_soinfo_for_gdb.flags = FLAG_NEW_SOINFO; 1981d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov linker_soinfo_for_gdb.base = linker_base; 1982d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1983d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov /* 1984d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * Set the dynamic field in the link map otherwise gdb will complain with 1985d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * the following: 1986d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * warning: .dynamic section for "/system/bin/linker" is not at the 1987d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * expected address (wrong library or version mismatch?) 1988d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov */ 1989d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base); 1990d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff); 1991d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base, 1992d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov &linker_soinfo_for_gdb.dynamic, NULL, NULL); 1993d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov insert_soinfo_into_debug_map(&linker_soinfo_for_gdb); 1994d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 1995d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1996d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov/* 1997468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and 1998468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs 1999468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point. 2000468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */ 20010266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) { 200242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes /* NOTE: we store the args pointer on a special location 2003ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner * of the temporary TLS area in order to pass it to 2004ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner * the C Library's runtime initializer. 2005ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner * 2006ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner * The initializer must clear the slot and reset the TLS 2007ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner * to point to a different location to ensure that no other 2008ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner * shared library constructor can access it. 2009ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner */ 2010d3920b3a996b358e48232f417aa0a1e44a60f155Elliott Hughes __libc_init_tls(args); 20111a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov 20121a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING 20131a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov struct timeval t0, t1; 20141a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov gettimeofday(&t0, 0); 20151a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif 20161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 201718a206c81d9743481e364384affd43306911283dElliott Hughes // Initialize environment functions, and get to the ELF aux vectors table. 201842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_env_init(args); 2019be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner 20208d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich // If this is a setuid/setgid program, close the security hole described in 20218d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc 20228d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich if (get_AT_SECURE()) { 20238d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich nullify_closed_stdio(); 20248d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich } 20258d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich 2026d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom debuggerd_init(); 20271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 202818a206c81d9743481e364384affd43306911283dElliott Hughes // Get a few environment variables. 202961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes const char* LD_DEBUG = linker_env_get("LD_DEBUG"); 203061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes if (LD_DEBUG != NULL) { 20311728b2396591853345507a063ed6075dfd251706Elliott Hughes g_ld_debug_verbosity = atoi(LD_DEBUG); 203218a206c81d9743481e364384affd43306911283dElliott Hughes } 2033be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner 203418a206c81d9743481e364384affd43306911283dElliott Hughes // Normally, these are cleaned by linker_env_init, but the test 203518a206c81d9743481e364384affd43306911283dElliott Hughes // doesn't cost us anything. 203618a206c81d9743481e364384affd43306911283dElliott Hughes const char* ldpath_env = NULL; 203718a206c81d9743481e364384affd43306911283dElliott Hughes const char* ldpreload_env = NULL; 203818a206c81d9743481e364384affd43306911283dElliott Hughes if (!get_AT_SECURE()) { 203918a206c81d9743481e364384affd43306911283dElliott Hughes ldpath_env = linker_env_get("LD_LIBRARY_PATH"); 204018a206c81d9743481e364384affd43306911283dElliott Hughes ldpreload_env = linker_env_get("LD_PRELOAD"); 20411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 20421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2043d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov // Linker does not call constructors for its own 2044d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov // global variables so we need to initialize 2045d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // the allocators explicitly. 20461728b2396591853345507a063ed6075dfd251706Elliott Hughes g_soinfo_allocator.init(); 2047d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov g_soinfo_links_allocator.init(); 2048d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov 2049ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes INFO("[ android linker & debugger ]"); 20501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2051d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov soinfo* si = soinfo_alloc(args.argv[0], NULL); 205218a206c81d9743481e364384affd43306911283dElliott Hughes if (si == NULL) { 205318a206c81d9743481e364384affd43306911283dElliott Hughes exit(EXIT_FAILURE); 20541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 20551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2056d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich /* bootstrap the link map, the main exe always needs to be first */ 20571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->flags |= FLAG_EXE; 20583a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes link_map* map = &(si->link_map_head); 20591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 20601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_addr = 0; 206142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes map->l_name = args.argv[0]; 20621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_prev = NULL; 20631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_next = NULL; 20641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 20651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _r_debug.r_map = map; 20661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project r_debug_tail = map; 20671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2068d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov init_linker_info_for_gdb(linker_base); 20691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 207042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes // Extract information passed from the kernel. 20710266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR)); 207242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes si->phnum = args.getauxval(AT_PHNUM); 207342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes si->entry = args.getauxval(AT_ENTRY); 20741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 20758180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner /* Compute the value of si->base. We can't rely on the fact that 20768180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner * the first entry is the PHDR because this will not be true 20778180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner * for certain executables (e.g. some in the NDK unit test suite) 20788180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner */ 20798180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner si->base = 0; 2080b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner si->size = phdr_table_get_load_size(si->phdr, si->phnum); 2081bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner si->load_bias = 0; 2082ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes for (size_t i = 0; i < si->phnum; ++i) { 208342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes if (si->phdr[i].p_type == PT_PHDR) { 20840266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr; 20850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset; 208642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes break; 208742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes } 20888180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner } 2089d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom si->dynamic = NULL; 2090ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->ref_count = 1; 20911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 20922aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base); 20932aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich if (elf_hdr->e_type != ET_DYN) { 20942aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n"); 20952aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich exit(EXIT_FAILURE); 20962aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich } 20972aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich 20984688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid). 20994688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes parse_LD_LIBRARY_PATH(ldpath_env); 21004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes parse_LD_PRELOAD(ldpreload_env); 21014fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 21025ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel somain = si; 21035ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel 2104183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) if (!soinfo_link_image(si, NULL)) { 2105650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer()); 210618a206c81d9743481e364384affd43306911283dElliott Hughes exit(EXIT_FAILURE); 21071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 21081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2109c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov add_vdso(args); 2110c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov 2111d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->CallPreInitConstructors(); 21129181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov 21131728b2396591853345507a063ed6075dfd251706Elliott Hughes for (size_t i = 0; g_ld_preloads[i] != NULL; ++i) { 21141728b2396591853345507a063ed6075dfd251706Elliott Hughes g_ld_preloads[i]->CallConstructors(); 2115326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng } 2116326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng 2117d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom /* After the link_image, the si->load_bias is initialized. 2118d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom * For so lib, the map->l_addr will be updated in notify_gdb_of_load. 2119d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom * We need to update this value for so exe here. So Unwind_Backtrace 2120d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom * for some arch like x86 could work correctly within so exe. 21219c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin */ 2122c5db969aa4a24a76fc0964f2ff89533e832b47a6Chao-Ying Fu map->l_addr = si->load_bias; 2123d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->CallConstructors(); 2124e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov 21251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING 2126faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes gettimeofday(&t1, NULL); 2127ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) ( 21281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) - 2129faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec))); 21301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 21311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS 2132ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0], 2133bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes linker_stats.count[kRelocAbsolute], 2134bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes linker_stats.count[kRelocRelative], 2135bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes linker_stats.count[kRelocCopy], 2136bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes linker_stats.count[kRelocSymbol]); 21371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 21381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES 21391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project { 21401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned n; 21411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned i; 21421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned count = 0; 2143d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom for (n = 0; n < 4096; n++) { 2144d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (bitmask[n]) { 21451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned x = bitmask[n]; 2146e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__LP64__) 2147e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland for (i = 0; i < 32; i++) { 2148e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#else 2149d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom for (i = 0; i < 8; i++) { 2150e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#endif 2151d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (x & 1) { 2152d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom count++; 2153d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom } 21541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project x >>= 1; 21551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 21561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 21571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2158ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4); 21591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 21601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 21611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 21621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES 21631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project fflush(stdout); 21641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 21651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2166c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry)); 21671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return si->entry; 21681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 2169468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich 2170bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only 2171bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library 2172bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself. 2173bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * 2174bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input: 2175bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * elf -> address of ELF header, assumed to be at the start of the file. 2176bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return: 2177bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * load bias, i.e. add the value of any p_vaddr in the file to get 2178bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * the corresponding address in memory. 2179bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */ 21800266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) { 21810266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) offset = elf->e_phoff; 2182faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset); 21830266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum; 2184fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng 21850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) { 2186fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng if (phdr->p_type == PT_LOAD) { 21870266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr; 2188bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner } 2189fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng } 2190fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng return 0; 2191bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner} 2192bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner 2193468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/* 2194468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This 2195468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and 2196468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation(). 2197468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * 2198468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own 2199468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern 2200468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault. 2201468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */ 22020266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesextern "C" ElfW(Addr) __linker_init(void* raw_args) { 2203d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // Initialize static variables. 2204d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov solist = get_libdl_info(); 2205d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov sonext = get_libdl_info(); 2206d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 220742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes KernelArgumentBlock args(raw_args); 220842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes 22090266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) linker_addr = args.getauxval(AT_BASE); 22100266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr); 2211faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff); 221242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes 221342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes soinfo linker_so; 221442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes memset(&linker_so, 0, sizeof(soinfo)); 221542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes 2216b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes strcpy(linker_so.name, "[dynamic linker]"); 221742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_so.base = linker_addr; 221842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum); 221942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_so.load_bias = get_elf_exec_load_bias(elf_hdr); 2220d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom linker_so.dynamic = NULL; 222142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_so.phdr = phdr; 222242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_so.phnum = elf_hdr->e_phnum; 222342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_so.flags |= FLAG_LINKER; 222442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes 2225183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) if (!soinfo_link_image(&linker_so, NULL)) { 222642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes // It would be nice to print an error message, but if the linker 222742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes // can't link itself, there's no guarantee that we'll be able to 2228b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes // call write() (because it involves a GOT reference). We may as 2229b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes // well try though... 2230b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes const char* msg = "CANNOT LINK EXECUTABLE: "; 2231b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes write(2, msg, strlen(msg)); 2232b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf)); 2233b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes write(2, "\n", 1); 2234b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes _exit(EXIT_FAILURE); 223542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes } 2236468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich 223742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes // We have successfully fixed our own relocations. It's safe to run 223842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes // the main part of the linker now. 22391728b2396591853345507a063ed6075dfd251706Elliott Hughes args.abort_message_ptr = &g_abort_message; 22400266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr); 22415419b9474753d25dff947c7740532f86d130c0beElliott Hughes 2242d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov protect_data(PROT_READ); 2243d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 224442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes // Return the address that the calling assembly stub should jump to. 224542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes return start_address; 2246468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich} 2247