linker.cpp revision 14241402de0faa4b244b1bd6b1f0799ce169b880
11dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 2943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan * Copyright (C) 2008, 2009 The Android Open Source Project 31dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * All rights reserved. 41dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 51dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Redistribution and use in source and binary forms, with or without 61dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * modification, are permitted provided that the following conditions 71dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * are met: 81dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * * Redistributions of source code must retain the above copyright 91dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * notice, this list of conditions and the following disclaimer. 101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * * Redistributions in binary form must reproduce the above copyright 111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * notice, this list of conditions and the following disclaimer in 121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * the documentation and/or other materials provided with the 131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * distribution. 141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS 161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT 171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS 181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE 191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT, 201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING, 211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS 221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED 231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY, 241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT 251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF 261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * SUCH DAMAGE. 271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 294688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <dlfcn.h> 304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <errno.h> 314688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <fcntl.h> 320266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes#include <inttypes.h> 334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <pthread.h> 341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdio.h> 351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdlib.h> 361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <string.h> 374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <sys/mman.h> 384688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <unistd.h> 391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 400d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#include <new> 410d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov 424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Private C library headers. 43eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/bionic_tls.h" 44eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/KernelArgumentBlock.h" 45eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/ScopedPthreadMutexLocker.h" 4604dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov#include "private/ScopedFd.h" 471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker.h" 491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker_debug.h" 50be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner#include "linker_environ.h" 5123363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner#include "linker_phdr.h" 52d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov#include "linker_allocator.h" 531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<< 551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Do NOT use malloc() and friends or pthread_*() code here. 571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Don't use printf() either; it's caused mysterious memory 581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * corruption in the past. 591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The linker runs before we bring up libc and it's easiest 601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to make sure it does not depend on any complex libc features 611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * open issues / todo: 631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - cleaner error reporting 651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - after linking, set as much stuff as possible to READONLY 661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * and NOEXEC 674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes */ 681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 69489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#if defined(__LP64__) 70489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#define SEARCH_NAME(x) x 71489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#else 72489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov// Nvidia drivers are relying on the bug: 73489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov// http://code.google.com/p/android/issues/detail?id=6670 74489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov// so we continue to use base-name lookup for lp32 75489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanovstatic const char* get_base_name(const char* name) { 76489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov const char* bname = strrchr(name, '/'); 77489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov return bname ? bname + 1 : name; 78489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov} 79489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#define SEARCH_NAME(x) get_base_name(x) 80489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov#endif 81489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov 82183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo); 830266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf); 841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 851728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic LinkerAllocator<soinfo> g_soinfo_allocator; 86d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator; 87ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn 88d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo* solist; 89d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo* sonext; 90d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* somain; /* main process, always the one after libdl_info */ 911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 921728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic const char* const kDefaultLdPaths[] = { 934eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__LP64__) 94011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes "/vendor/lib64", 95011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes "/system/lib64", 96011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes#else 97124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes "/vendor/lib", 98124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes "/system/lib", 99011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes#endif 100124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes NULL 101124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes}; 102124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes 103a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPATH_BUFSIZE (LDPATH_MAX*64) 104a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPATH_MAX 8 105a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes 106a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPRELOAD_BUFSIZE (LDPRELOAD_MAX*64) 107a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes#define LDPRELOAD_MAX 8 108a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes 1091728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic char g_ld_library_paths_buffer[LDPATH_BUFSIZE]; 1101728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic const char* g_ld_library_paths[LDPATH_MAX + 1]; 111bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley 1121728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic char g_ld_preloads_buffer[LDPRELOAD_BUFSIZE]; 1131728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic const char* g_ld_preload_names[LDPRELOAD_MAX + 1]; 1144fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 1151728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic soinfo* g_ld_preloads[LDPRELOAD_MAX + 1]; 1164fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 1171728b2396591853345507a063ed6075dfd251706Elliott Hughes__LIBC_HIDDEN__ int g_ld_debug_verbosity; 1181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1191728b2396591853345507a063ed6075dfd251706Elliott Hughes__LIBC_HIDDEN__ abort_msg_t* g_abort_message = NULL; // For debuggerd. 1200d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes 121bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesenum RelocationKind { 122bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes kRelocAbsolute = 0, 123bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes kRelocRelative, 124bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes kRelocCopy, 125bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes kRelocSymbol, 126bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes kRelocMax 127bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}; 128be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner 1291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS 130bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstruct linker_stats_t { 131bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes int count[kRelocMax]; 132bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}; 133bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes 134bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic linker_stats_t linker_stats; 135bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes 136bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind kind) { 137bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes ++linker_stats.count[kind]; 138bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes} 139bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else 140bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind) { 141bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes} 1421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 1431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES 145bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic unsigned bitmask[4096]; 146e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__LP64__) 147e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#define MARK(offset) \ 148e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland do { \ 149e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland if ((((offset) >> 12) >> 5) < 4096) \ 150e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland bitmask[((offset) >> 12) >> 5] |= (1 << (((offset) >> 12) & 31)); \ 151faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes } while (0) 152e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#else 153bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(offset) \ 154bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes do { \ 155bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \ 156faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes } while (0) 157e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#endif 158bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else 159bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(x) do {} while (0) 1601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 1611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// You shouldn't try to call memory-allocating functions in the dynamic linker. 1634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Guard against the most obvious ones. 1648f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#define DISALLOW_ALLOCATION(return_type, name, ...) \ 1658f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes return_type name __VA_ARGS__ \ 1668f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes { \ 16712bf3bcdeeaa0dcdc7a4f4e4d11bc2fc3bf6bd7aDmitriy Ivanov __libc_fatal("ERROR: " #name " called from the dynamic linker!\n"); \ 1684688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 169812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, malloc, (size_t u __unused)); 170812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void, free, (void* u __unused)); 171812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, realloc, (void* u1 __unused, size_t u2 __unused)); 172812fd4263a005b88f3b4222baa910114f938d594Kito ChengDISALLOW_ALLOCATION(void*, calloc, (size_t u1 __unused, size_t u2 __unused)); 1732e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin 1740353195f344666256dba474a15c9ba22cf0cccc9Dima Zavinstatic char tmp_err_buf[768]; 1752e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavinstatic char __linker_dl_err_buf[768]; 1762e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin 177650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hugheschar* linker_get_error_buffer() { 1785419b9474753d25dff947c7740532f86d130c0beElliott Hughes return &__linker_dl_err_buf[0]; 1792e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin} 1802e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin 181650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughessize_t linker_get_error_buffer_size() { 182650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes return sizeof(__linker_dl_err_buf); 183650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes} 184650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes 1851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 1861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This function is an empty stub where GDB locates a breakpoint to get notified 1871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about linker activity. 1881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 1895419b9474753d25dff947c7740532f86d130c0beElliott Hughesextern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity(); 1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1911728b2396591853345507a063ed6075dfd251706Elliott Hughesstatic pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER; 1923a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic r_debug _r_debug = {1, NULL, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0}; 1933a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic link_map* r_debug_tail = 0; 1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1953a9c5d66dc8d41272f51482b713717af7049697eElliott Hughesstatic void insert_soinfo_into_debug_map(soinfo* info) { 196bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes // Copy the necessary fields into the debug structure. 1973a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes link_map* map = &(info->link_map_head); 198ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov map->l_addr = info->load_bias; 199faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes map->l_name = reinterpret_cast<char*>(info->name); 2003a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes map->l_ld = info->dynamic; 2011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* Stick the new library at the end of the list. 2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * gdb tends to care more about libc than it does 2041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about leaf libraries, and ordering it this way 2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * reduces the back-and-forth over the wire. 2061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 2071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (r_debug_tail) { 2081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project r_debug_tail->l_next = map; 2091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_prev = r_debug_tail; 2101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_next = 0; 2111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 2121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _r_debug.r_map = map; 2131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_prev = 0; 2141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_next = 0; 2151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project r_debug_tail = map; 2171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 2181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 219bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void remove_soinfo_from_debug_map(soinfo* info) { 2203a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes link_map* map = &(info->link_map_head); 2215e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 222bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes if (r_debug_tail == map) { 2235e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev r_debug_tail = map->l_prev; 224bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes } 2255e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 226bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes if (map->l_prev) { 227bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes map->l_prev->l_next = map->l_next; 228bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes } 229bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes if (map->l_next) { 230bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes map->l_next->l_prev = map->l_prev; 231bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes } 2325e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev} 2335e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 234bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_load(soinfo* info) { 2351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (info->flags & FLAG_EXE) { 2361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project // GDB already knows about the main executable 2371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return; 2381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2401728b2396591853345507a063ed6075dfd251706Elliott Hughes ScopedPthreadMutexLocker locker(&g__r_debug_mutex); 2411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2423a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes _r_debug.r_state = r_debug::RT_ADD; 2431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project rtld_db_dlactivity(); 2441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project insert_soinfo_into_debug_map(info); 2461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2473a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes _r_debug.r_state = r_debug::RT_CONSISTENT; 2481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project rtld_db_dlactivity(); 2495e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev} 2505e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 251bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_unload(soinfo* info) { 2525e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev if (info->flags & FLAG_EXE) { 2535e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev // GDB already knows about the main executable 2545e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev return; 2555e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev } 2565e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 2571728b2396591853345507a063ed6075dfd251706Elliott Hughes ScopedPthreadMutexLocker locker(&g__r_debug_mutex); 2585e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 2593a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes _r_debug.r_state = r_debug::RT_DELETE; 2605e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev rtld_db_dlactivity(); 2615e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 2625e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev remove_soinfo_from_debug_map(info); 2635e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 2643a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes _r_debug.r_state = r_debug::RT_CONSISTENT; 2655e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev rtld_db_dlactivity(); 2661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 2671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 26818a206c81d9743481e364384affd43306911283dElliott Hughesvoid notify_gdb_of_libraries() { 2693a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes _r_debug.r_state = r_debug::RT_ADD; 2703a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes rtld_db_dlactivity(); 2713a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes _r_debug.r_state = r_debug::RT_CONSISTENT; 2723a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes rtld_db_dlactivity(); 2731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 2741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 275d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy IvanovLinkedListEntry<soinfo>* SoinfoListAllocator::alloc() { 276d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return g_soinfo_links_allocator.alloc(); 277d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 278d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 279d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) { 280d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov g_soinfo_links_allocator.free(entry); 281d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 282d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 283d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic void protect_data(int protection) { 284d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov g_soinfo_allocator.protect_all(protection); 285d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov g_soinfo_links_allocator.protect_all(protection); 286d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 287d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 288d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo* soinfo_alloc(const char* name, struct stat* file_stat) { 289ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn if (strlen(name) >= SOINFO_NAME_LEN) { 290ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn DL_ERR("library name \"%s\" too long", name); 291ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn return NULL; 292ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn } 293ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn 2940d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat); 295d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 296ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn sonext->next = si; 297ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn sonext = si; 2981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 299ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("name %s: allocated soinfo @ %p", name, si); 300ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn return si; 3011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 3021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 303faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesstatic void soinfo_free(soinfo* si) { 3044688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (si == NULL) { 3054688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return; 3064688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 3074688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 308d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if (si->base != 0 && si->size != 0) { 309d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov munmap(reinterpret_cast<void*>(si->base), si->size); 310d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 311d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 3121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project soinfo *prev = NULL, *trav; 3131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 314ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("name %s: freeing soinfo @ %p", si->name, si); 3151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 316d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom for (trav = solist; trav != NULL; trav = trav->next) { 3171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (trav == si) 3181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 3191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project prev = trav; 3201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (trav == NULL) { 322d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom /* si was not in solist */ 3234688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("name \"%s\" is not in solist!", si->name); 3241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return; 3251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 327d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // clear links to/from si 328d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov si->remove_all_links(); 329d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 330be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner /* prev will never be NULL, because the first entry in solist is 3311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project always the static libdl_info. 3321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 3331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project prev->next = si->next; 334d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (si == sonext) { 335d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom sonext = prev; 336d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom } 337d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov 3381728b2396591853345507a063ed6075dfd251706Elliott Hughes g_soinfo_allocator.free(si); 3391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 3401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 341cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes 342cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_path(const char* path, const char* delimiters, 343cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes const char** array, char* buf, size_t buf_size, size_t max_count) { 344cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes if (path == NULL) { 345cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes return; 346cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes } 347cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes 348cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes size_t len = strlcpy(buf, path, buf_size); 349cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes 350cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes size_t i = 0; 351cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes char* buf_p = buf; 352cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) { 353cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes if (*array[i] != '\0') { 354cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes ++i; 355cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes } 356cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes } 357cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes 358cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes // Forget the last path if we had to truncate; this occurs if the 2nd to 359cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes // last char isn't '\0' (i.e. wasn't originally a delimiter). 360cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') { 361cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes array[i - 1] = NULL; 362cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes } else { 363cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes array[i] = NULL; 364cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes } 365cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes} 366cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes 367cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_LIBRARY_PATH(const char* path) { 3681728b2396591853345507a063ed6075dfd251706Elliott Hughes parse_path(path, ":", g_ld_library_paths, 3691728b2396591853345507a063ed6075dfd251706Elliott Hughes g_ld_library_paths_buffer, sizeof(g_ld_library_paths_buffer), LDPATH_MAX); 370cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes} 371cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes 372cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_PRELOAD(const char* path) { 373cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes // We have historically supported ':' as well as ' ' in LD_PRELOAD. 3741728b2396591853345507a063ed6075dfd251706Elliott Hughes parse_path(path, " :", g_ld_preload_names, 3751728b2396591853345507a063ed6075dfd251706Elliott Hughes g_ld_preloads_buffer, sizeof(g_ld_preloads_buffer), LDPRELOAD_MAX); 376cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes} 377cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes 3784eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__) 3794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 3801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* For a given PC, find the .so that it belongs to. 3811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the base address of the .ARM.exidx section 3821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for that .so, and the number of 8-byte entries 3831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in that section (via *pcount). 3841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Intended to be called by libc's __gnu_Unwind_Find_exidx(). 3861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3873b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes * This function is exposed via dlfcn.cpp and libdl.so. 3881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 389faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) { 3901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned addr = (unsigned)pc; 3911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 392faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes for (soinfo* si = solist; si != 0; si = si->next) { 393468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich if ((addr >= si->base) && (addr < (si->base + si->size))) { 394468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich *pcount = si->ARM_exidx_count; 395f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee return (_Unwind_Ptr)si->ARM_exidx; 3961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 398faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes *pcount = 0; 3991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return NULL; 4001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 4014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 40224053a461e7a20f34002262c1bb122023134989dChristopher Ferris#endif 4034688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 4041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Here, we only have to provide a callback to iterate across all the 4051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * loaded libraries. gcc_eh does the rest. */ 406faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughesint dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) { 4071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int rv = 0; 408bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes for (soinfo* si = solist; si != NULL; si = si->next) { 409bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes dl_phdr_info dl_info; 4103a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes dl_info.dlpi_addr = si->link_map_head.l_addr; 4113a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes dl_info.dlpi_name = si->link_map_head.l_name; 4121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project dl_info.dlpi_phdr = si->phdr; 4131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project dl_info.dlpi_phnum = si->phnum; 414bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes rv = cb(&dl_info, sizeof(dl_phdr_info), data); 415bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes if (rv != 0) { 4161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 417bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes } 4181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 4191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return rv; 4201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 4214688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 42202aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanovstatic ElfW(Sym)* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) { 4230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Sym)* symtab = si->symtab; 4240266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes const char* strtab = si->strtab; 4251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4260266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd", 4270266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket); 4281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4290266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) { 4300266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Sym)* s = symtab + n; 4310266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes if (strcmp(strtab + s->st_name, name)) continue; 4321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 43302aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov /* only concern ourselves with global and weak symbol definitions */ 4340266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes switch (ELF_ST_BIND(s->st_info)) { 4350266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes case STB_GLOBAL: 4360266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes case STB_WEAK: 4370266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes if (s->st_shndx == SHN_UNDEF) { 438d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov continue; 439d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov } 4401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 441d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd", 4420266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes name, si->name, reinterpret_cast<void*>(s->st_value), 4430266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<size_t>(s->st_size)); 444d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov return s; 445d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov case STB_LOCAL: 44602aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov continue; 447d97e9f546ea195686a78e539315b273393609b9eDmitriy Ivanov default: 44812bf3bcdeeaa0dcdc7a4f4e4d11bc2fc3bf6bd7aDmitriy Ivanov __libc_fatal("ERROR: Unexpected ST_BIND value: %d for '%s' in '%s'", 44912bf3bcdeeaa0dcdc7a4f4e4d11bc2fc3bf6bd7aDmitriy Ivanov ELF_ST_BIND(s->st_info), name, si->name); 4501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 4510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes } 4521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 453aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd", 454aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket); 455aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov 456aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov 4570266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes return NULL; 4581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 4591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4600d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanovsoinfo::soinfo(const char* name, const struct stat* file_stat) { 4610d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov memset(this, 0, sizeof(*this)); 4620d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov 4630d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov strlcpy(this->name, name, sizeof(this->name)); 4640d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov flags = FLAG_NEW_SOINFO; 4650d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov version = SOINFO_VERSION; 4660d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov 4670d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov if (file_stat != NULL) { 4680d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov set_st_dev(file_stat->st_dev); 4690d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov set_st_ino(file_stat->st_ino); 4700d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov } 4710d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov} 4720d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov 4739598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanovvoid soinfo::resolve_ifunc_symbols() { 4749598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov if (!get_has_ifuncs()) { 4759598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov return; 4769598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov } 477c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith 4789598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov phdr_table_unprotect_segments(phdr, phnum, load_bias); 479c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith 480c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith TRACE_TYPE(IFUNC, "CHECKING FOR IFUNCS AND PERFORMING SYMBOL UPDATES"); 481c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith 4829598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov for (size_t i = 0; i < nchain; ++i) { 4839598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov ElfW(Sym)* s = &symtab[i]; 484c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) { 485c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith // The address of the ifunc in the symbol table is the address of the 486c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith // function that chooses the function to which the ifunc will refer. 487c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith // In order to return the proper value, we run the choosing function 488c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith // in the linker and then return its result (minus the base offset). 489c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith TRACE_TYPE(IFUNC, "FOUND IFUNC"); 490c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith ElfW(Addr) (*ifunc_ptr)(); 4919598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov ifunc_ptr = reinterpret_cast<ElfW(Addr)(*)()>(s->st_value + base); 4929598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov s->st_value = (ifunc_ptr() - base); 493c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith TRACE_TYPE(IFUNC, "NEW VALUE IS %p", (void*)s->st_value); 494c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith } 495c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith } 4969598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov phdr_table_protect_segments(phdr, phnum, load_bias); 497c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith} 498c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith 499d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic unsigned elfhash(const char* _name) { 500faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes const unsigned char* name = reinterpret_cast<const unsigned char*>(_name); 5011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned h = 0, g; 5021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 503faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes while (*name) { 5041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project h = (h << 4) + *name++; 5051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project g = h & 0xf0000000; 5061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project h ^= g; 5071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project h ^= g >> 24; 5081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 5091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return h; 5101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 5111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5120266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Sym)* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) { 513943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan unsigned elf_hash = elfhash(name); 5140266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Sym)* s = NULL; 5156ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev 516c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (si != NULL && somain != NULL) { 5175ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel /* 518c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * Local scope is executable scope. Just start looking into it right away 519c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * for the shortcut. 5205ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel */ 5215ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel 522c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (si == somain) { 52302aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov s = soinfo_elf_lookup(si, elf_hash, name); 5245ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel if (s != NULL) { 525c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin *lsi = si; 5265ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel goto done; 5275ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 528c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin } else { 529c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */ 530c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin 531c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin /* 532c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * If this object was built with symbolic relocations disabled, the 533c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * first place to look to resolve external references is the main 534c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * executable. 535c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin */ 536c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin 537c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (!si->has_DT_SYMBOLIC) { 538ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes DEBUG("%s: looking up %s in executable %s", 53961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes si->name, name, somain->name); 54002aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov s = soinfo_elf_lookup(somain, elf_hash, name); 541c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (s != NULL) { 542c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin *lsi = somain; 543c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin goto done; 544c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin } 545c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin } 5465ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel 547c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin /* Look for symbols in the local scope (the object who is 5484eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes * searching). This happens with C++ templates on x86 for some 549c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * reason. 550c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * 551c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * Notes on weak symbols: 552c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * The ELF specs are ambiguous about treatment of weak definitions in 553c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * dynamic linking. Some systems return the first definition found 554c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * and some the first non-weak definition. This is system dependent. 555c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * Here we return the first definition found for simplicity. */ 556c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin 55702aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov s = soinfo_elf_lookup(si, elf_hash, name); 558c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (s != NULL) { 559c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin *lsi = si; 560c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin goto done; 561c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin } 562c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin 563c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin /* 564c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * If this object was built with -Bsymbolic and symbol is not found 565c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * in the local scope, try to find the symbol in the main executable. 566c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin */ 567c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin 568c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (si->has_DT_SYMBOLIC) { 569ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes DEBUG("%s: looking up %s in executable %s after local scope", 57061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes si->name, name, somain->name); 57102aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov s = soinfo_elf_lookup(somain, elf_hash, name); 572c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (s != NULL) { 573c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin *lsi = somain; 574c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin goto done; 575c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin } 576c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin } 5775ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 578d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich } 5796ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev 5804fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer /* Next, look for it in the preloads list */ 5811728b2396591853345507a063ed6075dfd251706Elliott Hughes for (int i = 0; g_ld_preloads[i] != NULL; i++) { 58202aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov s = soinfo_elf_lookup(g_ld_preloads[i], elf_hash, name); 583d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (s != NULL) { 5841728b2396591853345507a063ed6075dfd251706Elliott Hughes *lsi = g_ld_preloads[i]; 5854fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer goto done; 5865ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 5874fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer } 5884fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 589d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom for (int i = 0; needed[i] != NULL; i++) { 590ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes DEBUG("%s: looking up %s in %s", 59161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes si->name, name, needed[i]->name); 59202aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov s = soinfo_elf_lookup(needed[i], elf_hash, name); 5935ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel if (s != NULL) { 5945ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel *lsi = needed[i]; 59512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel goto done; 5965ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 5976ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev } 5986ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev 5996ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevdone: 600d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (s != NULL) { 601c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, " 602c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes "found in %s, base = %p, load bias = %p", 603c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes si->name, name, reinterpret_cast<void*>(s->st_value), 604c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes (*lsi)->name, reinterpret_cast<void*>((*lsi)->base), 605c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes reinterpret_cast<void*>((*lsi)->load_bias)); 6066ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev return s; 6076ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev } 6086ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev 609943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan return NULL; 6106ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev} 6116ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev 612aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov// Another soinfo list allocator to use in dlsym. We don't reuse 613aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov// SoinfoListAllocator because it is write-protected most of the time. 614aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanovstatic LinkerAllocator<LinkedListEntry<soinfo>> g_soinfo_list_allocator_rw; 615aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanovclass SoinfoListAllocatorRW { 616aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov public: 617aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov static LinkedListEntry<soinfo>* alloc() { 618aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov return g_soinfo_list_allocator_rw.alloc(); 619aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov } 620d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom 621aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov static void free(LinkedListEntry<soinfo>* ptr) { 622aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov g_soinfo_list_allocator_rw.free(ptr); 623aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov } 624aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov}; 625aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov 626aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov// This is used by dlsym(3). It performs symbol lookup only within the 627aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov// specified soinfo object and its dependencies in breadth first order. 62802aa70589d22fa9b65da43de705d6de2715870c6Dmitriy IvanovElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) { 629aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov LinkedList<soinfo, SoinfoListAllocatorRW> visit_list; 630042426ba6375f5c145379e598486ec6d675533c9Dmitriy Ivanov LinkedList<soinfo, SoinfoListAllocatorRW> visited; 631aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov visit_list.push_back(si); 632aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov soinfo* current_soinfo; 633aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov while ((current_soinfo = visit_list.pop_front()) != nullptr) { 634042426ba6375f5c145379e598486ec6d675533c9Dmitriy Ivanov if (visited.contains(current_soinfo)) { 635042426ba6375f5c145379e598486ec6d675533c9Dmitriy Ivanov continue; 636042426ba6375f5c145379e598486ec6d675533c9Dmitriy Ivanov } 637042426ba6375f5c145379e598486ec6d675533c9Dmitriy Ivanov 63802aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov ElfW(Sym)* result = soinfo_elf_lookup(current_soinfo, elfhash(name), name); 639aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov 640aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov if (result != nullptr) { 641aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov *found = current_soinfo; 642aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov visit_list.clear(); 643042426ba6375f5c145379e598486ec6d675533c9Dmitriy Ivanov visited.clear(); 644aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov return result; 645aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov } 646042426ba6375f5c145379e598486ec6d675533c9Dmitriy Ivanov visited.push_back(current_soinfo); 647aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov 648aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov current_soinfo->get_children().for_each([&](soinfo* child) { 649aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov visit_list.push_back(child); 650aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov }); 651aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov } 652aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov 653aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov visit_list.clear(); 654042426ba6375f5c145379e598486ec6d675533c9Dmitriy Ivanov visited.clear(); 655aa0f2bdbc22d4b7aec5d3f8f5f01eaeaa13414c2Dmitriy Ivanov return nullptr; 6561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 6571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 658d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3) to performs a global symbol lookup. If the 659d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom start value is null (for RTLD_DEFAULT), the search starts at the 660d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom beginning of the global solist. Otherwise the search starts at the 661d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom specified soinfo (for RTLD_NEXT). 6626ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */ 66302aa70589d22fa9b65da43de705d6de2715870c6Dmitriy IvanovElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) { 664cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes unsigned elf_hash = elfhash(name); 6651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 666cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes if (start == NULL) { 667cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes start = solist; 668cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes } 6691698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer 6700266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Sym)* s = NULL; 671cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) { 67202aa70589d22fa9b65da43de705d6de2715870c6Dmitriy Ivanov s = soinfo_elf_lookup(si, elf_hash, name); 673cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes if (s != NULL) { 674cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes *found = si; 675cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes break; 6761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 677cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes } 6781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 679cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes if (s != NULL) { 680c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p", 681c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base)); 682cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes } 6831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 684cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes return s; 6851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 6861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 687fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Chengsoinfo* find_containing_library(const void* p) { 6880266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p); 689fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng for (soinfo* si = solist; si != NULL; si = si->next) { 690fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng if (address >= si->base && address - si->base < si->size) { 691fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng return si; 692e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer } 693fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng } 694fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng return NULL; 695e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer} 696e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer 6970266ae5f884d72da58f33a072e865ba131234a5eElliott HughesElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr) { 6980266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - si->base; 699fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng 700fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng // Search the library's symbol table for any defined symbol which 701fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng // contains this address. 702fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng for (size_t i = 0; i < si->nchain; ++i) { 7030266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Sym)* sym = &si->symtab[i]; 704fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng if (sym->st_shndx != SHN_UNDEF && 705fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng soaddr >= sym->st_value && 706fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng soaddr < sym->st_value + sym->st_size) { 707fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng return sym; 708e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer } 709fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng } 710e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer 711fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng return NULL; 712e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer} 713e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer 714124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library_on_path(const char* name, const char* const paths[]) { 715124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes char buf[512]; 716124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes for (size_t i = 0; paths[i] != NULL; ++i) { 7171e980b6bc8315d00a07312b25486531247abd98cElliott Hughes int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name); 718124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (n < 0 || n >= static_cast<int>(sizeof(buf))) { 719ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name); 720124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes continue; 7211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 722124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC)); 723124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (fd != -1) { 724124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return fd; 725124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 726124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 727124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return -1; 7281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 7291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 730124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library(const char* name) { 731ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ opening %s ]", name); 7321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 733124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes // If the name contains a slash, we should attempt to open it directly and not search the paths. 734124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (strchr(name, '/') != NULL) { 7356971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC)); 7366971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes if (fd != -1) { 7376971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes return fd; 7386971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes } 7396971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now. 7405ca7ed9005ea16733d7c87d7154473b7a500be0cDmitriy Ivanov#if defined(__LP64__) 741e43c4a7a665032a29cb5ec15d4adbf81ea199220Dmitriy Ivanov return -1; 7425ca7ed9005ea16733d7c87d7154473b7a500be0cDmitriy Ivanov#endif 743124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 7441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 745124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths. 7461728b2396591853345507a063ed6075dfd251706Elliott Hughes int fd = open_library_on_path(name, g_ld_library_paths); 747124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (fd == -1) { 7481728b2396591853345507a063ed6075dfd251706Elliott Hughes fd = open_library_on_path(name, kDefaultLdPaths); 749124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 750124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return fd; 7511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 7521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 753b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic soinfo* load_library(const char* name, int dlflags, const android_dlextinfo* extinfo) { 75404dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov int fd = -1; 75504dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov ScopedFd file_guard(-1); 75604dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov 75704dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov if (extinfo != NULL && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) { 75804dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov fd = extinfo->library_fd; 75904dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov } else { 76004dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov // Open the file. 76104dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov fd = open_library(name); 76204dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov if (fd == -1) { 7634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("library \"%s\" not found", name); 7641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return NULL; 76504dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov } 76604dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov 76704dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov file_guard.reset(fd); 7682e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin } 7691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 770650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes ElfReader elf_reader(name, fd); 771d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 772d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov struct stat file_stat; 773d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) { 774d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov DL_ERR("unable to stat file for the library %s: %s", name, strerror(errno)); 775d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return NULL; 776d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 777d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 778d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // Check for symlink and other situations where 779d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // file can have different names. 780d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov for (soinfo* si = solist; si != NULL; si = si->next) { 781d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if (si->get_st_dev() != 0 && 782d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov si->get_st_ino() != 0 && 783d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov si->get_st_dev() == file_stat.st_dev && 784d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov si->get_st_ino() == file_stat.st_ino) { 785d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov TRACE("library \"%s\" is already loaded under different name/path \"%s\" - will return existing soinfo", name, si->name); 786d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return si; 787d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 788d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 789d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 790b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov if ((dlflags & RTLD_NOLOAD) != 0) { 791b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov return NULL; 792b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov } 793b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov 794d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // Read the ELF header and load the segments. 79512bbb9164578b6512b8b07a3fb093858244b7c7bTorne (Richard Coles) if (!elf_reader.Load(extinfo)) { 7964688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 79723363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner } 79823363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner 799489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov soinfo* si = soinfo_alloc(SEARCH_NAME(name), &file_stat); 800650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes if (si == NULL) { 8014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 80223363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner } 803650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes si->base = elf_reader.load_start(); 804650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes si->size = elf_reader.load_size(); 805650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes si->load_bias = elf_reader.load_bias(); 806650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes si->phnum = elf_reader.phdr_count(); 807650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes si->phdr = elf_reader.loaded_phdr(); 808d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 809d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // At this point we know that whatever is loaded @ base is a valid ELF 810d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // shared library whose segments are properly mapped in. 81104dc91ae763adc403a14c88b4c46f77b3d2d71a3Dmitriy Ivanov TRACE("[ load_library base=%p size=%zu name='%s' ]", 812d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov reinterpret_cast<void*>(si->base), si->size, si->name); 813d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 814d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if (!soinfo_link_image(si, extinfo)) { 815d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov soinfo_free(si); 816d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return NULL; 817d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 818d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 819650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes return si; 8201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 8211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 822489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanovstatic soinfo *find_loaded_library_by_name(const char* name) { 823489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov const char* search_name = SEARCH_NAME(name); 824489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov for (soinfo* si = solist; si != NULL; si = si->next) { 825489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov if (!strcmp(search_name, si->name)) { 826489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov return si; 82712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel } 828489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov } 829489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov return NULL; 83012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel} 83112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel 832b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic soinfo* find_library_internal(const char* name, int dlflags, const android_dlextinfo* extinfo) { 833d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (name == NULL) { 834d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return somain; 835d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 836d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 837489e498434f53269c44e3c13039eb630e86e1fd9Dmitriy Ivanov soinfo* si = find_loaded_library_by_name(name); 838b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov 839b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov // Library might still be loaded, the accurate detection 840b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov // of this fact is done by load_library 841b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov if (si == NULL) { 842b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov TRACE("[ '%s' has not been found by name. Trying harder...]", name); 843b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov si = load_library(name, dlflags, extinfo); 844b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov } 845b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov 846b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov if (si != NULL && (si->flags & FLAG_LINKED) == 0) { 847b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov DL_ERR("recursive link to \"%s\"", si->name); 848d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return NULL; 849d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 85012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel 851b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov return si; 852d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes} 853d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 854b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic soinfo* find_library(const char* name, int dlflags, const android_dlextinfo* extinfo) { 855b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov soinfo* si = find_library_internal(name, dlflags, extinfo); 856d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si != NULL) { 857ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->ref_count++; 858d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 859d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return si; 860d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes} 861d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 862b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovstatic void soinfo_unload(soinfo* si) { 863ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes if (si->ref_count == 1) { 864ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("unloading '%s'", si->name); 865d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->CallDestructors(); 866d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 8670d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov if (si->has_min_version(0)) { 868d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov si->get_children().for_each([&] (soinfo* child) { 869d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov TRACE("%s needs to unload %s", si->name, child->name); 870d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov soinfo_unload(child); 871d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov }); 872d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } else { 873d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) { 874d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov if (d->d_tag == DT_NEEDED) { 875d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov const char* library_name = si->strtab + d->d_un.d_val; 876d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov TRACE("%s needs to unload %s", si->name, library_name); 877b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov soinfo* needed = find_library(library_name, RTLD_NOLOAD, NULL); 878b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov if (needed != NULL) { 879b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov soinfo_unload(needed); 880b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov } else { 881b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov // Not found: for example if symlink was deleted between dlopen and dlclose 882b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov // Since we cannot really handle errors at this point - print and continue. 883b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov PRINT("warning: couldn't find %s needed by %s on unload.", library_name, si->name); 884b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov } 885d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 886d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 8871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 8881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 889d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes notify_gdb_of_unload(si); 890ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->ref_count = 0; 891d597d263bc32422402d4810ce4ec070f0227c2f7Dmitriy Ivanov soinfo_free(si); 892d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } else { 893ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->ref_count--; 894c620059479c47a78d57086d73726c9adc2f337adElliott Hughes TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count); 895d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 8961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 8971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 898a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughesvoid do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) { 8991728b2396591853345507a063ed6075dfd251706Elliott Hughes snprintf(buffer, buffer_size, "%s:%s", kDefaultLdPaths[0], kDefaultLdPaths[1]); 900a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes} 901a4aafd156068ee174012f28cd894dbecf0e4ab90Elliott Hughes 902cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesvoid do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) { 903cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes if (!get_AT_SECURE()) { 904cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes parse_LD_LIBRARY_PATH(ld_library_path); 905cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes } 906cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes} 907cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes 9081a586293400d0e1d73e6eb82f7dfe9d2d9ed3c4bElliott Hughessoinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo) { 909b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NOLOAD)) != 0) { 910e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes DL_ERR("invalid flags to dlopen: %x", flags); 911e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes return NULL; 912e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes } 913012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) if (extinfo != NULL && ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0)) { 9143a8646fc5d9f1a9304251a1d1c8ff9e523d81b40Dmitriy Ivanov DL_ERR("invalid extended flags to android_dlopen_ext: %" PRIx64, extinfo->flags); 915012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) return NULL; 916012cb4583a5f8564059142bb1900ea3a31e7cfa9Torne (Richard Coles) } 917d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov protect_data(PROT_READ | PROT_WRITE); 918b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov soinfo* si = find_library(name, flags, extinfo); 919d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si != NULL) { 920d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->CallConstructors(); 921d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 922d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov protect_data(PROT_READ); 923d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return si; 924d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes} 9251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 926b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanovvoid do_dlclose(soinfo* si) { 927d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov protect_data(PROT_READ | PROT_WRITE); 928b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov soinfo_unload(si); 929d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov protect_data(PROT_READ); 9301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 9311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 932c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith// ifuncs are only defined for x86 933c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#if defined(__i386__) 934c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smithstatic void soinfo_ifunc_relocate(soinfo* si, ElfW(Rel)* rel, unsigned count, soinfo* needed[]) { 935c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith for (size_t idx = 0; idx < count; ++idx, ++rel) { 936c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith ElfW(Sym)* s; 937c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith soinfo* lsi; 938c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith unsigned type = ELFW(R_TYPE)(rel->r_info); 939c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith unsigned sym = ELFW(R_SYM)(rel->r_info); 940c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + si->load_bias); 941c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith ElfW(Addr) sym_addr = 0; 942c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith const char* sym_name = NULL; 943c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name); 944c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith s = soinfo_do_lookup(si, sym_name, &lsi, needed); 945c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith 946c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC && type == R_386_JMP_SLOT) { 947c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith TRACE("IFUNC RELOCATION, PASS 2: %p", (void*)(sym_addr)); 948c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith ElfW(Addr) (*ifunc_ptr)(); 949c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith ifunc_ptr = reinterpret_cast<ElfW(Addr)(*)()>(s->st_value + si->base); 950c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_ptr(); 951c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith } 952c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith } 953c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith} 954c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#endif 955c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith 956c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#if defined(__x86_64__) 957c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smithstatic void soinfo_ifunc_relocate(soinfo* si, ElfW(Rela)* rela, unsigned count, soinfo* needed[]) { 958c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith for (size_t idx = 0; idx < count; ++idx, ++rela) { 959c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith ElfW(Sym)* s; 960c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith soinfo* lsi; 961c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith unsigned type = ELFW(R_TYPE)(rela->r_info); 962c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith unsigned sym = ELFW(R_SYM)(rela->r_info); 963c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + si->load_bias); 964c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith ElfW(Addr) sym_addr = 0; 965c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith const char* sym_name = NULL; 966c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name); 967c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith s = soinfo_do_lookup(si, sym_name, &lsi, needed); 968c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith 969c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC && type == R_X86_64_JUMP_SLOT) { 970c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith TRACE("IFUNC RELOCATION, PASS 2: %p", (void*)(sym_addr + rela->r_addend)); 971c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith ElfW(Addr) (*ifunc_ptr)(); 972c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith ifunc_ptr = reinterpret_cast<ElfW(Addr)(*)()>(s->st_value + si->base); 973c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith *reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_ptr(); 974c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith } 975c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith } 976c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith} 977c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#endif 978c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith 9794eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA) 9809918665a45095ad135576f005c0e5307feb366a1Chris Dearmanstatic int soinfo_relocate(soinfo* si, ElfW(Rela)* rela, unsigned count, soinfo* needed[]) { 9810266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Sym)* s; 982c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes soinfo* lsi; 983c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 984c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes for (size_t idx = 0; idx < count; ++idx, ++rela) { 9850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes unsigned type = ELFW(R_TYPE)(rela->r_info); 9860266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes unsigned sym = ELFW(R_SYM)(rela->r_info); 9870266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rela->r_offset + si->load_bias); 9880266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) sym_addr = 0; 989faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes const char* sym_name = NULL; 990c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 991c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DEBUG("Processing '%s' relocation at index %zd", si->name, idx); 992c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (type == 0) { // R_*_NONE 993c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes continue; 994c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 995c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (sym != 0) { 996c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name); 997c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes s = soinfo_do_lookup(si, sym_name, &lsi, needed); 998c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (s == NULL) { 999c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes // We only allow an undefined symbol if this is a weak reference... 1000c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes s = &si->symtab[sym]; 1001c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (ELF_ST_BIND(s->st_info) != STB_WEAK) { 1002c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name); 1003c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return -1; 1004c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1005c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 1006c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes /* IHI0044C AAELF 4.5.1.1: 1007c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 1008c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes Libraries are not searched to resolve weak references. 1009c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes It is not an error for a weak reference to remain unsatisfied. 1010c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 1011c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes During linking, the value of an undefined weak reference is: 1012c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes - Zero if the relocation type is absolute 1013c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes - The address of the place if the relocation is pc-relative 1014c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes - The address of nominal base address if the relocation 1015c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes type is base-relative. 1016c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes */ 1017c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 1018c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes switch (type) { 1019e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__) 1020e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_JUMP_SLOT: 1021e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_GLOB_DAT: 1022e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_ABS64: 1023e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_ABS32: 1024e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_ABS16: 1025e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_RELATIVE: 1026e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland /* 1027e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland * The sym_addr was initialized to be zero above, or the relocation 1028e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland * code below does not care about value of sym_addr. 1029e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland * No need to do anything. 1030e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland */ 1031e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1032e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__) 1033c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_JUMP_SLOT: 1034c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_GLOB_DAT: 1035c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_32: 1036bb5bb2046fa0759d94995cbfb4f41137cd0b2a91Pavel Chupin case R_X86_64_64: 1037c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_RELATIVE: 1038c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes // No need to do anything. 1039c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1040c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_PC32: 1041c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes sym_addr = reloc; 1042c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 10434eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 1044c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes default: 1045faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rela, idx); 1046c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return -1; 1047c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1048c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } else { 1049c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes // We got a definition. 10500266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias); 1051c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1052c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes count_relocation(kRelocSymbol); 1053c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } else { 1054c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes s = NULL; 1055c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1056c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes 1057c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes switch (type) { 1058e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__aarch64__) 1059e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_JUMP_SLOT: 1060e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocAbsolute); 1061e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 10620266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO JMP_SLOT %16llx <- %16llx %s\n", 10630266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), sym_name); 10640266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend); 1065e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1066e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_GLOB_DAT: 1067e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocAbsolute); 1068e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 10690266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO GLOB_DAT %16llx <- %16llx %s\n", 10700266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), sym_name); 10710266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + rela->r_addend); 1072e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1073e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_ABS64: 1074e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocAbsolute); 1075e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 10760266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n", 10770266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), sym_name); 10780266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend); 1079e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1080e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_ABS32: 1081e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocAbsolute); 1082e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 10830266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n", 10840266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), sym_name); 10850266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) && 10860266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) { 10870266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend); 1088e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } else { 10890266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", 10900266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)), 10910266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(INT32_MIN), 10920266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(UINT32_MAX)); 1093e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland return -1; 1094e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } 1095e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1096e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_ABS16: 1097e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocAbsolute); 1098e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 10990266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n", 11000266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), sym_name); 11010266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend))) && 11020266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ((*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) { 11030266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend); 1104e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } else { 11050266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", 11060266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes (*reinterpret_cast<ElfW(Addr)*>(reloc) + (sym_addr + rela->r_addend)), 11070266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(INT16_MIN), 11080266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(UINT16_MAX)); 1109e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland return -1; 1110e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } 1111e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1112e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_PREL64: 1113e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocRelative); 1114e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 11150266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n", 11160266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name); 11170266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr + rela->r_addend) - rela->r_offset; 1118e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1119e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_PREL32: 1120e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocRelative); 1121e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 11220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n", 11230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name); 11240266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes if ((static_cast<ElfW(Addr)>(INT32_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) && 11250266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT32_MAX))) { 11260266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset); 1127e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } else { 11280266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", 11290266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)), 11300266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(INT32_MIN), 11310266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(UINT32_MAX)); 1132e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland return -1; 1133e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } 1134e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1135e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_PREL16: 1136e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocRelative); 1137e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 11380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n", 11390266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), rela->r_offset, sym_name); 11400266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes if ((static_cast<ElfW(Addr)>(INT16_MIN) <= (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset))) && 11410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ((*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)) <= static_cast<ElfW(Addr)>(UINT16_MAX))) { 11420266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += ((sym_addr + rela->r_addend) - rela->r_offset); 1143e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } else { 11440266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx", 11450266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes (*reinterpret_cast<ElfW(Addr)*>(reloc) + ((sym_addr + rela->r_addend) - rela->r_offset)), 11460266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(INT16_MIN), 11470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes static_cast<ElfW(Addr)>(UINT16_MAX)); 1148e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland return -1; 1149e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } 1150e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1151e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland 1152e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_RELATIVE: 1153e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland count_relocation(kRelocRelative); 1154e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland MARK(rela->r_offset); 1155e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland if (sym) { 1156e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland DL_ERR("odd RELATIVE form..."); 1157e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland return -1; 1158e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland } 11590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO RELATIVE %16llx <- %16llx\n", 11600266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (si->base + rela->r_addend)); 11610266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = (si->base + rela->r_addend); 1162e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1163e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland 1164e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_COPY: 116576e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich /* 116676e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * ET_EXEC is not supported so this should not happen. 116776e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * 116876e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf 116976e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * 117076e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * Section 4.7.1.10 "Dynamic relocations" 117176e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * R_AARCH64_COPY may only appear in executable objects where e_type is 117276e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * set to ET_EXEC. 117376e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich */ 117476e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich DL_ERR("%s R_AARCH64_COPY relocations are not supported", si->name); 117576e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich return -1; 1176e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_TLS_TPREL64: 11770266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n", 11780266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), rela->r_offset); 1179e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1180e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland case R_AARCH64_TLS_DTPREL32: 11810266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n", 11820266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes reloc, (sym_addr + rela->r_addend), rela->r_offset); 1183e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland break; 1184e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#elif defined(__x86_64__) 1185c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_JUMP_SLOT: 1186c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes count_relocation(kRelocAbsolute); 1187c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes MARK(rela->r_offset); 1188c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc), 1189c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes static_cast<size_t>(sym_addr + rela->r_addend), sym_name); 1190c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) { 1191c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith si->set_has_ifuncs(true); 1192c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith } else { 1193c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend; 1194c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith } 1195c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1196c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_GLOB_DAT: 1197c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes count_relocation(kRelocAbsolute); 1198c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes MARK(rela->r_offset); 1199c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc), 1200c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes static_cast<size_t>(sym_addr + rela->r_addend), sym_name); 12010266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend; 1202c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1203c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_RELATIVE: 1204c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes count_relocation(kRelocRelative); 1205c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes MARK(rela->r_offset); 1206c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (sym) { 1207c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DL_ERR("odd RELATIVE form..."); 1208c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return -1; 1209c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1210c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc), 1211c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes static_cast<size_t>(si->base)); 12120266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = si->base + rela->r_addend; 1213c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1214c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_32: 1215c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes count_relocation(kRelocRelative); 1216c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes MARK(rela->r_offset); 1217c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc), 1218c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes static_cast<size_t>(sym_addr), sym_name); 12190266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend; 1220c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1221c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin case R_X86_64_64: 1222c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin count_relocation(kRelocRelative); 1223c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin MARK(rela->r_offset); 1224c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc), 1225c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin static_cast<size_t>(sym_addr), sym_name); 12260266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend; 1227c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin break; 1228c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_X86_64_PC32: 1229c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes count_relocation(kRelocRelative); 1230c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes MARK(rela->r_offset); 1231c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s", 1232c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc), 1233c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name); 12340266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + rela->r_addend - reloc; 1235c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 12364eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 1237e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland 1238c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes default: 1239faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DL_ERR("unknown reloc type %d @ %p (%zu)", type, rela, idx); 1240c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return -1; 1241c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1242c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 1243c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return 0; 1244c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes} 12459918665a45095ad135576f005c0e5307feb366a1Chris Dearman 12469918665a45095ad135576f005c0e5307feb366a1Chris Dearman#else // REL, not RELA. 12479918665a45095ad135576f005c0e5307feb366a1Chris Dearman 12480266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic int soinfo_relocate(soinfo* si, ElfW(Rel)* rel, unsigned count, soinfo* needed[]) { 12490266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Sym)* s; 1250d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom soinfo* lsi; 12511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 12524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes for (size_t idx = 0; idx < count; ++idx, ++rel) { 12530266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes unsigned type = ELFW(R_TYPE)(rel->r_info); 12540266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes // TODO: don't use unsigned for 'sym'. Use uint32_t or ElfW(Addr) instead. 12550266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes unsigned sym = ELFW(R_SYM)(rel->r_info); 12560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + si->load_bias); 12570266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) sym_addr = 0; 1258faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes const char* sym_name = NULL; 12591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1260c620059479c47a78d57086d73726c9adc2f337adElliott Hughes DEBUG("Processing '%s' relocation at index %zd", si->name, idx); 1261d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if (type == 0) { // R_*_NONE 1262d7daacb46372132ae3f0121647074936c304b572Raghu Gandham continue; 1263d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1264d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (sym != 0) { 1265c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes sym_name = reinterpret_cast<const char*>(si->strtab + si->symtab[sym].st_name); 12665ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel s = soinfo_do_lookup(si, sym_name, &lsi, needed); 1267d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (s == NULL) { 12684eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // We only allow an undefined symbol if this is a weak reference... 1269c62b8a4d3f73b070099d9aa594a261b5250d6cc9Elliott Hughes s = &si->symtab[sym]; 1270c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (ELF_ST_BIND(s->st_info) != STB_WEAK) { 1271e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name); 1272e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan return -1; 1273e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan } 1274e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1275e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan /* IHI0044C AAELF 4.5.1.1: 1276e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1277e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan Libraries are not searched to resolve weak references. 1278e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan It is not an error for a weak reference to remain 1279e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan unsatisfied. 1280e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1281e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan During linking, the value of an undefined weak reference is: 1282e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan - Zero if the relocation type is absolute 1283e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan - The address of the place if the relocation is pc-relative 1284bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes - The address of nominal base address if the relocation 1285e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan type is base-relative. 1286e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan */ 1287e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1288e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan switch (type) { 12894eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__) 1290e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_JUMP_SLOT: 1291e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_GLOB_DAT: 1292e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_ABS32: 1293e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_RELATIVE: /* Don't care. */ 12944eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // sym_addr was initialized to be zero above or relocation 12954eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // code below does not care about value of sym_addr. 12964eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // No need to do anything. 12974eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 12984eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__) 1299d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case R_386_JMP_SLOT: 1300e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_386_GLOB_DAT: 1301e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_386_32: 1302c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case R_386_RELATIVE: /* Don't care. */ 13034eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // sym_addr was initialized to be zero above or relocation 13044eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // code below does not care about value of sym_addr. 13054eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // No need to do anything. 1306e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan break; 1307e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_386_PC32: 1308e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan sym_addr = reloc; 1309e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan break; 13104eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 1311e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 13124eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__) 1313e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_COPY: 13144eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // Fall through. Can't really copy if weak symbol is not found at run-time. 13154eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 1316e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan default: 1317faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx); 1318e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan return -1; 1319e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan } 1320e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan } else { 13214eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes // We got a definition. 13220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes sym_addr = static_cast<ElfW(Addr)>(s->st_value + lsi->load_bias); 1323d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1324bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocSymbol); 13251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 1326e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan s = NULL; 13271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 13281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1329c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes switch (type) { 13304eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__) 13311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_ARM_JUMP_SLOT: 1332bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocAbsolute); 13331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 1334ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name); 13350266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr; 13361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 13371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_ARM_GLOB_DAT: 1338bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocAbsolute); 13391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 1340ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name); 13410266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr; 13421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 13431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_ARM_ABS32: 1344bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocAbsolute); 13451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 1346ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name); 13470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr; 13481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 134934ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner case R_ARM_REL32: 1350bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocRelative); 135134ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner MARK(rel->r_offset); 1352ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s", 135334ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner reloc, sym_addr, rel->r_offset, sym_name); 13540266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset; 135534ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner break; 13561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_ARM_COPY: 135776e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich /* 135876e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * ET_EXEC is not supported so this should not happen. 135976e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * 136076e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf 136176e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * 136276e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * Section 4.7.1.10 "Dynamic relocations" 136376e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * R_ARM_COPY may only appear in executable objects where e_type is 136476e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich * set to ET_EXEC. 136576e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich */ 136676e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich DL_ERR("%s R_ARM_COPY relocations are not supported", si->name); 136776e289c026f11126fc88841b3019fd5bb419bb67Nick Kralevich return -1; 13684eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__) 13694eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_386_JMP_SLOT: 13704eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes count_relocation(kRelocAbsolute); 13714eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes MARK(rel->r_offset); 13724eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name); 1373c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) { 1374c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith si->set_has_ifuncs(true); 1375c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith } else { 1376c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr; 1377c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith } 13784eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 13794eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_386_GLOB_DAT: 13804eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes count_relocation(kRelocAbsolute); 13814eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes MARK(rel->r_offset); 13824eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name); 13830266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr; 13844eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 13854eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_386_32: 13864eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes count_relocation(kRelocRelative); 13874eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes MARK(rel->r_offset); 13884eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name); 13890266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr; 13904eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 13914eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_386_PC32: 13924eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes count_relocation(kRelocRelative); 13934eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes MARK(rel->r_offset); 13944eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s", 13954eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes reloc, (sym_addr - reloc), sym_addr, reloc, sym_name); 13960266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc); 13974eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 13984eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__mips__) 13994eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_MIPS_REL32: 14009918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__LP64__) 14019918665a45095ad135576f005c0e5307feb366a1Chris Dearman // MIPS Elf64_Rel entries contain compound relocations 14029918665a45095ad135576f005c0e5307feb366a1Chris Dearman // We only handle the R_MIPS_NONE|R_MIPS_64|R_MIPS_REL32 case 14039918665a45095ad135576f005c0e5307feb366a1Chris Dearman if (ELF64_R_TYPE2(rel->r_info) != R_MIPS_64 || 14049918665a45095ad135576f005c0e5307feb366a1Chris Dearman ELF64_R_TYPE3(rel->r_info) != R_MIPS_NONE) { 1405faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DL_ERR("Unexpected compound relocation type:%d type2:%d type3:%d @ %p (%zu)", 14069918665a45095ad135576f005c0e5307feb366a1Chris Dearman type, (unsigned)ELF64_R_TYPE2(rel->r_info), 1407faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes (unsigned)ELF64_R_TYPE3(rel->r_info), rel, idx); 14089918665a45095ad135576f005c0e5307feb366a1Chris Dearman return -1; 14099918665a45095ad135576f005c0e5307feb366a1Chris Dearman } 14109918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif 14114eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes count_relocation(kRelocAbsolute); 14124eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes MARK(rel->r_offset); 14139918665a45095ad135576f005c0e5307feb366a1Chris Dearman TRACE_TYPE(RELO, "RELO REL32 %08zx <- %08zx %s", static_cast<size_t>(reloc), 14149918665a45095ad135576f005c0e5307feb366a1Chris Dearman static_cast<size_t>(sym_addr), sym_name ? sym_name : "*SECTIONHDR*"); 14154eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes if (s) { 14160266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr; 14174eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes } else { 14180266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base; 14194eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes } 14204eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 14214eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 14224eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes 14234eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__) 14244eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_ARM_RELATIVE: 14254eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__) 14264eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes case R_386_RELATIVE: 14274eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 14284eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes count_relocation(kRelocRelative); 14294eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes MARK(rel->r_offset); 14304eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes if (sym) { 14314eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes DL_ERR("odd RELATIVE form..."); 14324eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes return -1; 14334eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes } 14344eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p", 14354eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base)); 14360266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes *reinterpret_cast<ElfW(Addr)*>(reloc) += si->base; 14374eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes break; 14381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project default: 1440faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx); 14411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return -1; 14421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 14431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 14441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 14451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 1446c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 14471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14484eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__) 144987c358524e479235aa6241736d2ce325f89daafcBrian Carlstromstatic bool mips_relocate_got(soinfo* si, soinfo* needed[]) { 14509918665a45095ad135576f005c0e5307feb366a1Chris Dearman ElfW(Addr)** got = si->plt_got; 145187c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom if (got == NULL) { 145287c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom return true; 145387c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom } 145487c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom unsigned local_gotno = si->mips_local_gotno; 145587c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom unsigned gotsym = si->mips_gotsym; 145687c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom unsigned symtabno = si->mips_symtabno; 14570266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Sym)* symtab = si->symtab; 1458d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 14599918665a45095ad135576f005c0e5307feb366a1Chris Dearman // got[0] is the address of the lazy resolver function. 14609918665a45095ad135576f005c0e5307feb366a1Chris Dearman // got[1] may be used for a GNU extension. 14619918665a45095ad135576f005c0e5307feb366a1Chris Dearman // Set it to a recognizable address in case someone calls it (should be _rtld_bind_start). 14629918665a45095ad135576f005c0e5307feb366a1Chris Dearman // FIXME: maybe this should be in a separate routine? 1463d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if ((si->flags & FLAG_LINKER) == 0) { 146487c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom size_t g = 0; 14659918665a45095ad135576f005c0e5307feb366a1Chris Dearman got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadbeef); 14669918665a45095ad135576f005c0e5307feb366a1Chris Dearman if (reinterpret_cast<intptr_t>(got[g]) < 0) { 14679918665a45095ad135576f005c0e5307feb366a1Chris Dearman got[g++] = reinterpret_cast<ElfW(Addr)*>(0xdeadfeed); 1468d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 14699918665a45095ad135576f005c0e5307feb366a1Chris Dearman // Relocate the local GOT entries. 1470d7daacb46372132ae3f0121647074936c304b572Raghu Gandham for (; g < local_gotno; g++) { 14719918665a45095ad135576f005c0e5307feb366a1Chris Dearman got[g] = reinterpret_cast<ElfW(Addr)*>(reinterpret_cast<uintptr_t>(got[g]) + si->load_bias); 1472d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1473d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1474d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 14759918665a45095ad135576f005c0e5307feb366a1Chris Dearman // Now for the global GOT entries... 14760266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Sym)* sym = symtab + gotsym; 1477d7daacb46372132ae3f0121647074936c304b572Raghu Gandham got = si->plt_got + local_gotno; 147887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom for (size_t g = gotsym; g < symtabno; g++, sym++, got++) { 14799918665a45095ad135576f005c0e5307feb366a1Chris Dearman // This is an undefined reference... try to locate it. 14809918665a45095ad135576f005c0e5307feb366a1Chris Dearman const char* sym_name = si->strtab + sym->st_name; 1481d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom soinfo* lsi; 14829918665a45095ad135576f005c0e5307feb366a1Chris Dearman ElfW(Sym)* s = soinfo_do_lookup(si, sym_name, &lsi, needed); 1483d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if (s == NULL) { 14849918665a45095ad135576f005c0e5307feb366a1Chris Dearman // We only allow an undefined symbol if this is a weak reference. 1485d7daacb46372132ae3f0121647074936c304b572Raghu Gandham s = &symtab[g]; 1486c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (ELF_ST_BIND(s->st_info) != STB_WEAK) { 14874688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("cannot locate \"%s\"...", sym_name); 148887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom return false; 1489d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1490d7daacb46372132ae3f0121647074936c304b572Raghu Gandham *got = 0; 14919918665a45095ad135576f005c0e5307feb366a1Chris Dearman } else { 14929918665a45095ad135576f005c0e5307feb366a1Chris Dearman // FIXME: is this sufficient? 14939918665a45095ad135576f005c0e5307feb366a1Chris Dearman // For reference see NetBSD link loader 14949918665a45095ad135576f005c0e5307feb366a1Chris 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 14959918665a45095ad135576f005c0e5307feb366a1Chris Dearman *got = reinterpret_cast<ElfW(Addr)*>(lsi->load_bias + s->st_value); 1496d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1497d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 149887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom return true; 1499d7daacb46372132ae3f0121647074936c304b572Raghu Gandham} 1500d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif 1501d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1502812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallArray(const char* array_name __unused, linker_function_t* functions, size_t count, bool reverse) { 1503ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes if (functions == NULL) { 1504d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return; 1505d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 15068215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner 1507c620059479c47a78d57086d73726c9adc2f337adElliott Hughes TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name); 15088215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner 1509ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes int begin = reverse ? (count - 1) : 0; 1510ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes int end = reverse ? -1 : count; 1511ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes int step = reverse ? -1 : 1; 15128215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner 1513ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes for (int i = begin; i != end; i += step) { 1514ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]); 1515ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes CallFunction("function", functions[i]); 1516d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 1517d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 1518ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ Done calling %s for '%s' ]", array_name, name); 15191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 15201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1521812fd4263a005b88f3b4222baa910114f938d594Kito Chengvoid soinfo::CallFunction(const char* function_name __unused, linker_function_t function) { 1522db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) { 1523d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return; 1524d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 1525d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 1526ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name); 1527d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes function(); 1528ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name); 1529db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes 1530db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures 1531db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes // are still writable. This happens with our debug malloc (see http://b/7941716). 1532d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov protect_data(PROT_READ | PROT_WRITE); 15339181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov} 15349181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov 1535d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallPreInitConstructors() { 15368147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // DT_PREINIT_ARRAY functions are called before any other constructors for executables, 15378147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // but ignored in a shared library. 1538d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false); 1539d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes} 1540e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov 1541d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallConstructors() { 1542d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (constructors_called) { 1543d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return; 1544d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 1545e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov 1546d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // We set constructors_called before actually calling the constructors, otherwise it doesn't 1547d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // protect against recursive constructor calls. One simple example of constructor recursion 1548d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so: 1549d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // 1. The program depends on libc, so libc's constructor is called here. 1550d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so. 1551d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // 3. dlopen() calls the constructors on the newly created 1552d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // soinfo for libc_malloc_debug_leak.so. 1553d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // 4. The debug .so depends on libc, so CallConstructors is 1554d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // called again with the libc soinfo. If it doesn't trigger the early- 1555d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // out above, the libc constructor will be called again (recursively!). 1556d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes constructors_called = true; 1557d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 15588147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) { 15598147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // The GNU dynamic linker silently ignores these, but we warn the developer. 1560c620059479c47a78d57086d73726c9adc2f337adElliott Hughes PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!", 15618147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes name, preinit_array_count); 1562d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 15631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1564d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov get_children().for_each([] (soinfo* si) { 1565d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov si->CallConstructors(); 1566d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov }); 15671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 15688147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes TRACE("\"%s\": calling constructors", name); 15698147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes 15708147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // DT_INIT should be called before DT_INIT_ARRAY if both are present. 1571d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallFunction("DT_INIT", init_func); 1572d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallArray("DT_INIT_ARRAY", init_array, init_array_count, false); 15739598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov 15749598b8c415e2fa9f240508185fe8c964b83f538dDmitriy Ivanov resolve_ifunc_symbols(); 1575e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov} 15768215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner 1577d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallDestructors() { 15788147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes TRACE("\"%s\": calling destructors", name); 15798147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes 15808147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // DT_FINI_ARRAY must be parsed in reverse order. 1581d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true); 15828147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes 15838147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // DT_FINI should be called after DT_FINI_ARRAY if both are present. 1584d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallFunction("DT_FINI", fini_func); 1585b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov 1586b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov // This is needed on second call to dlopen 1587b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov // after library has been unloaded with RTLD_NODELETE 1588b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov constructors_called = false; 15891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 15901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1591d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::add_child(soinfo* child) { 15920d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov if (has_min_version(0)) { 15930d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov this->children.push_front(child); 15940d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov child->parents.push_front(this); 1595d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1596d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 1597d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1598d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::remove_all_links() { 15990d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov if (!has_min_version(0)) { 1600d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return; 1601d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1602d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1603d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // 1. Untie connected soinfos from 'this'. 1604d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov children.for_each([&] (soinfo* child) { 1605d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov child->parents.remove_if([&] (const soinfo* parent) { 1606d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return parent == this; 1607d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov }); 1608d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov }); 1609d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1610d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov parents.for_each([&] (soinfo* parent) { 1611d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov parent->children.for_each([&] (const soinfo* child) { 1612d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov return child == this; 1613d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov }); 1614d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov }); 1615d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1616d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov // 2. Once everything untied - clear local lists. 1617d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov parents.clear(); 1618d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov children.clear(); 1619d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 1620d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1621d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::set_st_dev(dev_t dev) { 16220d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov if (has_min_version(0)) { 16230d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov st_dev = dev; 1624d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1625d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 1626d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1627d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovvoid soinfo::set_st_ino(ino_t ino) { 16280d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov if (has_min_version(0)) { 16290d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov st_ino = ino; 1630d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1631d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 1632d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1633c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smithvoid soinfo::set_has_ifuncs(bool ifuncs) { 16340d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov if (has_min_version(1)) { 16350d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov has_ifuncs = ifuncs; 1636c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith } 1637c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith} 1638c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith 1639d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovdev_t soinfo::get_st_dev() { 16400d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov if (has_min_version(0)) { 16410d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov return st_dev; 1642d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1643d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 16440d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov return 0; 1645d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov}; 1646d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1647d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovino_t soinfo::get_st_ino() { 16480d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov if (has_min_version(0)) { 16490d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov return st_ino; 1650d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1651d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 16520d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov return 0; 1653d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 1654d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1655c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smithbool soinfo::get_has_ifuncs() { 16560d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov if (has_min_version(1)) { 16570d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov return has_ifuncs; 1658c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith } 1659c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith 16600d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov return false; 1661c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith} 1662c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith 1663d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov// This is a return on get_children() in case 1664d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov// 'this->flags' does not have FLAG_NEW_SOINFO set. 1665d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic soinfo::soinfo_list_t g_empty_list; 1666d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1667d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovsoinfo::soinfo_list_t& soinfo::get_children() { 16680d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov if (has_min_version(0)) { 16690d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov return this->children; 1670d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov } 1671d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 16720d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov return g_empty_list; 1673d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 1674d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 16751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with 16761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /dev/null. */ 16775419b9474753d25dff947c7740532f86d130c0beElliott Hughesstatic int nullify_closed_stdio() { 16781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int dev_null, i, status; 16791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int return_value = 0; 16801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 168116084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)); 16821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (dev_null < 0) { 16834688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("cannot open /dev/null: %s", strerror(errno)); 16841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return -1; 16851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1686ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null); 16871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* If any of the stdio file descriptors is valid and not associated 16891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project with /dev/null, dup /dev/null to it. */ 16901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (i = 0; i < 3; i++) { 16911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* If it is /dev/null already, we are done. */ 16924688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (i == dev_null) { 16931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project continue; 16944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 16951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1696ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ Nullifying stdio file descriptor %d]", i); 16974688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL)); 16981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16994688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes /* If file is opened, we are good. */ 17004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (status != -1) { 17014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes continue; 17024688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 17031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 17041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* The only error we allow is that the file descriptor does not 17051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project exist, in which case we dup /dev/null to it. */ 17061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (errno != EBADF) { 17074688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("fcntl failed: %s", strerror(errno)); 17081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return_value = -1; 17091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project continue; 17101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 17121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* Try dupping /dev/null to this stdio file descriptor and 17131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project repeat if there is a signal. Note that any errors in closing 17141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project the stdio descriptor are lost. */ 17154688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes status = TEMP_FAILURE_RETRY(dup2(dev_null, i)); 17161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (status < 0) { 17174688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("dup2 failed: %s", strerror(errno)); 17181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return_value = -1; 17191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project continue; 17201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 17231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* If /dev/null is not one of the stdio file descriptors, close it. */ 17241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (dev_null > 2) { 1725ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null); 17264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes status = TEMP_FAILURE_RETRY(close(dev_null)); 17274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (status == -1) { 17284688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("close failed: %s", strerror(errno)); 17291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return_value = -1; 17301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 17331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return return_value; 17341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 17351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1736183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles)static bool soinfo_link_image(soinfo* si, const android_dlextinfo* extinfo) { 1737f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee /* "base" might wrap around UINT32_MAX. */ 17380266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) base = si->load_bias; 17390266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes const ElfW(Phdr)* phdr = si->phdr; 17401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int phnum = si->phnum; 1741d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom bool relocating_linker = (si->flags & FLAG_LINKER) != 0; 17421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1743b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner /* We can't debug anything until the linker is relocated */ 1744b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (!relocating_linker) { 1745ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes INFO("[ linking %s ]", si->name); 1746c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags); 1747b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner } 17481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 174963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner /* Extract dynamic section */ 1750124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes size_t dynamic_count; 17510266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Word) dynamic_flags; 175212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic, 1753cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman &dynamic_count, &dynamic_flags); 175463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner if (si->dynamic == NULL) { 1755b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (!relocating_linker) { 1756124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name); 1757b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner } 1758124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 175963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner } else { 1760b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (!relocating_linker) { 1761ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes DEBUG("dynamic = %p", si->dynamic); 1762b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner } 176363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner } 176463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner 17654eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__) 176663f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner (void) phdr_table_get_arm_exidx(phdr, phnum, base, 176763f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner &si->ARM_exidx, &si->ARM_exidx_count); 176863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif 176963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner 17708147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes // Extract useful information from dynamic section. 1771d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom uint32_t needed_count = 0; 17720266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) { 1773c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p", 1774c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val)); 1775c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes switch (d->d_tag) { 17761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_HASH: 1777faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->nbucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[0]; 1778faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->nchain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr)[1]; 1779faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->bucket = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8); 1780faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->chain = reinterpret_cast<uint32_t*>(base + d->d_un.d_ptr + 8 + si->nbucket * 4); 17811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 17821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_STRTAB: 1783faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->strtab = reinterpret_cast<const char*>(base + d->d_un.d_ptr); 17841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 17851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_SYMTAB: 1786faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->symtab = reinterpret_cast<ElfW(Sym)*>(base + d->d_un.d_ptr); 17871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 17884eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if !defined(__LP64__) 17891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PLTREL: 1790d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (d->d_un.d_val != DT_REL) { 1791124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("unsupported DT_RELA in \"%s\"", si->name); 1792124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 17931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 1795c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 17961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_JMPREL: 17974eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA) 1798faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->plt_rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr); 1799c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else 1800faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->plt_rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr); 1801c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 18021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PLTRELSZ: 18044eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA) 18050266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->plt_rela_count = d->d_un.d_val / sizeof(ElfW(Rela)); 1806c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else 18070266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->plt_rel_count = d->d_un.d_val / sizeof(ElfW(Rel)); 1808c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 18091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18109918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if defined(__mips__) 18111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PLTGOT: 18129918665a45095ad135576f005c0e5307feb366a1Chris Dearman // Used by mips and mips64. 18139918665a45095ad135576f005c0e5307feb366a1Chris Dearman si->plt_got = reinterpret_cast<ElfW(Addr)**>(base + d->d_un.d_ptr); 18141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 1815c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 18161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_DEBUG: 1817bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes // Set the DT_DEBUG entry to the address of _r_debug for GDB 1818cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman // if the dynamic table is writable 18199918665a45095ad135576f005c0e5307feb366a1Chris Dearman// FIXME: not working currently for N64 18209918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The flags for the LOAD and DYNAMIC program headers do not agree. 18219918665a45095ad135576f005c0e5307feb366a1Chris Dearman// The LOAD section containng the dynamic table has been mapped as 18229918665a45095ad135576f005c0e5307feb366a1Chris Dearman// read-only, but the DYNAMIC header claims it is writable. 18239918665a45095ad135576f005c0e5307feb366a1Chris Dearman#if !(defined(__mips__) && defined(__LP64__)) 182499c32055cb50443f722c55914869c1f6bb941959Elliott Hughes if ((dynamic_flags & PF_W) != 0) { 1825c620059479c47a78d57086d73726c9adc2f337adElliott Hughes d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug); 182699c32055cb50443f722c55914869c1f6bb941959Elliott Hughes } 18271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18289918665a45095ad135576f005c0e5307feb366a1Chris Dearman#endif 18294eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA) 1830c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case DT_RELA: 1831faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->rela = reinterpret_cast<ElfW(Rela)*>(base + d->d_un.d_ptr); 1832c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1833c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case DT_RELASZ: 18340266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->rela_count = d->d_un.d_val / sizeof(ElfW(Rela)); 1835c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1836c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case DT_REL: 1837c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DL_ERR("unsupported DT_REL in \"%s\"", si->name); 1838c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return false; 1839c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case DT_RELSZ: 1840c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name); 1841c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return false; 1842c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else 1843c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case DT_REL: 1844faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes si->rel = reinterpret_cast<ElfW(Rel)*>(base + d->d_un.d_ptr); 1845c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1846c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes case DT_RELSZ: 18470266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->rel_count = d->d_un.d_val / sizeof(ElfW(Rel)); 1848c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes break; 1849ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI case DT_RELA: 1850124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("unsupported DT_RELA in \"%s\"", si->name); 1851124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1852c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 18531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_INIT: 1854ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr); 18558147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func); 18561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_FINI: 1858ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr); 18598147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func); 18601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_INIT_ARRAY: 1862ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr); 18638147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array); 18641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_INIT_ARRAYSZ: 18660266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr)); 18671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_FINI_ARRAY: 1869ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr); 18708147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array); 18711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_FINI_ARRAYSZ: 18730266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr)); 18741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PREINIT_ARRAY: 1876ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr); 18778147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array); 18781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PREINIT_ARRAYSZ: 18800266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(ElfW(Addr)); 18811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_TEXTREL: 1883e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__) 1884e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name); 1885e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes return false; 1886e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else 18875135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich si->has_text_relocations = true; 18881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 1889e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif 18905ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel case DT_SYMBOLIC: 18915ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel si->has_DT_SYMBOLIC = true; 18925ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel break; 1893d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom case DT_NEEDED: 1894d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom ++needed_count; 1895d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom break; 18965ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel case DT_FLAGS: 1897d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (d->d_un.d_val & DF_TEXTREL) { 1898e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__) 1899e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name); 1900e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes return false; 1901e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else 19025ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel si->has_text_relocations = true; 1903e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif 19045ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 1905d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (d->d_un.d_val & DF_SYMBOLIC) { 19065ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel si->has_DT_SYMBOLIC = true; 19075ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 19085ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel break; 19094eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__) 1910d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_STRSZ: 1911d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_SYMENT: 1912d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_RELENT: 1913d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1914d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_RLD_MAP: 1915bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB. 1916d7daacb46372132ae3f0121647074936c304b572Raghu Gandham { 1917006f9ad8910c945cd50e54f6c34d8c27b11c1193Benjamin Adolphi r_debug** dp = reinterpret_cast<r_debug**>(base + d->d_un.d_ptr); 1918d7daacb46372132ae3f0121647074936c304b572Raghu Gandham *dp = &_r_debug; 1919d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1920d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1921d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_RLD_VERSION: 1922d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_FLAGS: 1923d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_BASE_ADDRESS: 1924d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_UNREFEXTNO: 1925d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1926d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1927d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_SYMTABNO: 1928d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom si->mips_symtabno = d->d_un.d_val; 1929d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1930d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1931d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_LOCAL_GOTNO: 1932d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom si->mips_local_gotno = d->d_un.d_val; 1933d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1934d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1935d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_GOTSYM: 1936d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom si->mips_gotsym = d->d_un.d_val; 1937d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 19384eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif 1939d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1940d7daacb46372132ae3f0121647074936c304b572Raghu Gandham default: 19414eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes DEBUG("Unused DT entry: type %p arg %p", 19424eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val)); 1943d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 19441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 19451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 19461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1947c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p", 1948c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes reinterpret_cast<void*>(si->base), si->strtab, si->symtab); 19491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1950124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes // Sanity checks. 1951d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (relocating_linker && needed_count != 0) { 1952d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries"); 1953d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom return false; 1954d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom } 1955124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (si->nbucket == 0) { 1956124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name); 1957124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1958124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 1959124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (si->strtab == 0) { 1960124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name); 1961124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1962124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 1963124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (si->symtab == 0) { 1964124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name); 1965124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 19661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 19671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 19687e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes // If this is the main executable, then load all of the libraries from LD_PRELOAD now. 1969d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si->flags & FLAG_EXE) { 19701728b2396591853345507a063ed6075dfd251706Elliott Hughes memset(g_ld_preloads, 0, sizeof(g_ld_preloads)); 19717e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes size_t preload_count = 0; 19721728b2396591853345507a063ed6075dfd251706Elliott Hughes for (size_t i = 0; g_ld_preload_names[i] != NULL; i++) { 1973b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov soinfo* lsi = find_library(g_ld_preload_names[i], 0, NULL); 19747e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes if (lsi != NULL) { 19751728b2396591853345507a063ed6075dfd251706Elliott Hughes g_ld_preloads[preload_count++] = lsi; 19767e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes } else { 19777e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes // As with glibc, failure to load an LD_PRELOAD library is just a warning. 19787e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s", 19791728b2396591853345507a063ed6075dfd251706Elliott Hughes g_ld_preload_names[i], si->name, linker_get_error_buffer()); 19804fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer } 19814fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer } 19824fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer } 19834fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 1984faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes soinfo** needed = reinterpret_cast<soinfo**>(alloca((1 + needed_count) * sizeof(soinfo*))); 1985d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom soinfo** pneeded = needed; 198612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel 19870266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) { 1988d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (d->d_tag == DT_NEEDED) { 1989d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom const char* library_name = si->strtab + d->d_un.d_val; 1990ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes DEBUG("%s needs %s", si->name, library_name); 1991b648a8a57ee42533a5bf127225a252f73ca2cbbcDmitriy Ivanov soinfo* lsi = find_library(library_name, 0, NULL); 1992d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (lsi == NULL) { 1993650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf)); 19944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s", 1995d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom library_name, si->name, tmp_err_buf); 1996124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 19971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1998d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 1999d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov si->add_child(lsi); 200012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel *pneeded++ = lsi; 20011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 20021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 200312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel *pneeded = NULL; 20041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2005e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__) 20065135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich if (si->has_text_relocations) { 2007e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes // Make segments writable to allow text relocations to work properly. We will later call 2008e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes // phdr_table_protect_segments() after all of them are applied and all constructors are run. 20093d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich DL_WARN("%s has text relocations. This is wasting memory and prevents " 20103d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich "security hardening. Please fix.", si->name); 20115135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) { 20125135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich DL_ERR("can't unprotect loadable segments for \"%s\": %s", 20135135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich si->name, strerror(errno)); 2014124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 20155135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich } 20165135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich } 2017e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif 20185135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich 20194eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA) 2020c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (si->plt_rela != NULL) { 2021faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DEBUG("[ relocating %s plt ]\n", si->name); 20229918665a45095ad135576f005c0e5307feb366a1Chris Dearman if (soinfo_relocate(si, si->plt_rela, si->plt_rela_count, needed)) { 2023c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return false; 2024c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 2025c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 2026c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes if (si->rela != NULL) { 2027faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DEBUG("[ relocating %s ]\n", si->name); 20289918665a45095ad135576f005c0e5307feb366a1Chris Dearman if (soinfo_relocate(si, si->rela, si->rela_count, needed)) { 2029c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes return false; 2030c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 2031c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes } 2032c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else 2033d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (si->plt_rel != NULL) { 2034faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DEBUG("[ relocating %s plt ]", si->name); 2035d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) { 2036124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 2037124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 20381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2039d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (si->rel != NULL) { 2040faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes DEBUG("[ relocating %s ]", si->name); 2041d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (soinfo_relocate(si, si->rel, si->rel_count, needed)) { 2042124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 2043124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 20441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2045c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif 20461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2047c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith // if there are ifuncs, we need to do an additional relocation pass. 2048c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith // they cannot be resolved until the rest of the relocations are done 2049c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith // because we need to call the resolution function which may be waiting 2050c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith // on relocations. 2051c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith if(si->get_has_ifuncs()) { 2052c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#if defined(__i386__) 2053c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith soinfo_ifunc_relocate(si, si->plt_rel, si->plt_rel_count, needed); 2054c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#elif defined(__x86_64__) 2055c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith soinfo_ifunc_relocate(si, si->plt_rela, si->plt_rela_count, needed); 2056c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith#endif 2057c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith } 2058c5a13efa9bc4264be0a9a9e37c00633af01584edBrigid Smith 20594eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__) 206087c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom if (!mips_relocate_got(si, needed)) { 2061124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 2062d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 2063d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif 2064d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 20651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->flags |= FLAG_LINKED; 2066ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes DEBUG("[ finished linking %s ]", si->name); 20671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2068e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__) 20695135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich if (si->has_text_relocations) { 2070e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes // All relocations are done, we can protect our segments back to read-only. 20715135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) { 20725135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich DL_ERR("can't protect segments for \"%s\": %s", 20735135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich si->name, strerror(errno)); 2074124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 20755135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich } 20761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2077e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif 20781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2079b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner /* We can also turn on GNU RELRO protection */ 2080b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) { 20814688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("can't enable GNU RELRO protection for \"%s\": %s", 20824688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes si->name, strerror(errno)); 2083124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 20849ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich } 20859ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich 2086183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) /* Handle serializing/sharing the RELRO segment */ 2087183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) { 2088183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) if (phdr_table_serialize_gnu_relro(si->phdr, si->phnum, si->load_bias, 2089183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) extinfo->relro_fd) < 0) { 2090183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) DL_ERR("failed serializing GNU RELRO section for \"%s\": %s", 2091183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) si->name, strerror(errno)); 2092183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) return false; 2093183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) } 2094183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) } else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) { 2095183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) if (phdr_table_map_gnu_relro(si->phdr, si->phnum, si->load_bias, 2096183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) extinfo->relro_fd) < 0) { 2097183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) DL_ERR("failed mapping GNU RELRO section for \"%s\": %s", 2098183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) si->name, strerror(errno)); 2099183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) return false; 2100183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) } 2101183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) } 2102183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) 21031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project notify_gdb_of_load(si); 2104124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return true; 21051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 21061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2107468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/* 2108c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * This function add vdso to internal dso list. 2109c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * It helps to stack unwinding through signal handlers. 2110c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * Also, it makes bionic more like glibc. 2111c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov */ 2112812fd4263a005b88f3b4222baa910114f938d594Kito Chengstatic void add_vdso(KernelArgumentBlock& args __unused) { 21134eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(AT_SYSINFO_EHDR) 21140266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR)); 21150266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes if (ehdr_vdso == NULL) { 21160266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes return; 21170266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes } 2118c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov 2119d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov soinfo* si = soinfo_alloc("[vdso]", NULL); 2120ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov 21210266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff); 21220266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->phnum = ehdr_vdso->e_phnum; 21230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso); 21240266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->size = phdr_table_get_load_size(si->phdr, si->phnum); 21250266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->load_bias = get_elf_exec_load_bias(ehdr_vdso); 2126ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov 21270dcf06f976670b5d05286b184d150f61b71c1184Torne (Richard Coles) soinfo_link_image(si, NULL); 2128c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov#endif 2129c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov} 2130c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov 2131c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov/* 2132d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * This is linker soinfo for GDB. See details below. 2133d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov */ 21340d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#if defined(__LP64__) 21350d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#define LINKER_PATH "/system/bin/linker64" 21360d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#else 21370d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#define LINKER_PATH "/system/bin/linker" 21380d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov#endif 21390d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanovstatic soinfo linker_soinfo_for_gdb(LINKER_PATH, nullptr); 2140d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 2141d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov/* gdb expects the linker to be in the debug shared object list. 2142d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * Without this, gdb has trouble locating the linker's ".text" 2143d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * and ".plt" sections. Gdb could also potentially use this to 2144d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * relocate the offset of our exported 'rtld_db_dlactivity' symbol. 2145d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * Don't use soinfo_alloc(), because the linker shouldn't 2146d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * be on the soinfo list. 2147d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov */ 2148d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanovstatic void init_linker_info_for_gdb(ElfW(Addr) linker_base) { 2149d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov linker_soinfo_for_gdb.base = linker_base; 2150d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 2151d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov /* 2152d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * Set the dynamic field in the link map otherwise gdb will complain with 2153d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * the following: 2154d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * warning: .dynamic section for "/system/bin/linker" is not at the 2155d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov * expected address (wrong library or version mismatch?) 2156d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov */ 2157d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base); 2158d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff); 2159d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base, 2160d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov &linker_soinfo_for_gdb.dynamic, NULL, NULL); 2161d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov insert_soinfo_into_debug_map(&linker_soinfo_for_gdb); 2162d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov} 2163d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov 2164d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov/* 2165468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and 2166468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs 2167468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point. 2168468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */ 21690266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) { 21701a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING 21711a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov struct timeval t0, t1; 21721a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov gettimeofday(&t0, 0); 21731a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif 21741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 217518a206c81d9743481e364384affd43306911283dElliott Hughes // Initialize environment functions, and get to the ELF aux vectors table. 217642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_env_init(args); 2177be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner 21788d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich // If this is a setuid/setgid program, close the security hole described in 21798d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc 21808d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich if (get_AT_SECURE()) { 21818d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich nullify_closed_stdio(); 21828d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich } 21838d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich 2184d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom debuggerd_init(); 21851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 218618a206c81d9743481e364384affd43306911283dElliott Hughes // Get a few environment variables. 218761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes const char* LD_DEBUG = linker_env_get("LD_DEBUG"); 218861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes if (LD_DEBUG != NULL) { 21891728b2396591853345507a063ed6075dfd251706Elliott Hughes g_ld_debug_verbosity = atoi(LD_DEBUG); 219018a206c81d9743481e364384affd43306911283dElliott Hughes } 2191be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner 219218a206c81d9743481e364384affd43306911283dElliott Hughes // Normally, these are cleaned by linker_env_init, but the test 219318a206c81d9743481e364384affd43306911283dElliott Hughes // doesn't cost us anything. 219418a206c81d9743481e364384affd43306911283dElliott Hughes const char* ldpath_env = NULL; 219518a206c81d9743481e364384affd43306911283dElliott Hughes const char* ldpreload_env = NULL; 219618a206c81d9743481e364384affd43306911283dElliott Hughes if (!get_AT_SECURE()) { 219718a206c81d9743481e364384affd43306911283dElliott Hughes ldpath_env = linker_env_get("LD_LIBRARY_PATH"); 219818a206c81d9743481e364384affd43306911283dElliott Hughes ldpreload_env = linker_env_get("LD_PRELOAD"); 21991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 22001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2201ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes INFO("[ android linker & debugger ]"); 22021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2203d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov soinfo* si = soinfo_alloc(args.argv[0], NULL); 220418a206c81d9743481e364384affd43306911283dElliott Hughes if (si == NULL) { 220518a206c81d9743481e364384affd43306911283dElliott Hughes exit(EXIT_FAILURE); 22061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 22071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2208d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich /* bootstrap the link map, the main exe always needs to be first */ 22091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->flags |= FLAG_EXE; 22103a9c5d66dc8d41272f51482b713717af7049697eElliott Hughes link_map* map = &(si->link_map_head); 22111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 22121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_addr = 0; 221342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes map->l_name = args.argv[0]; 22141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_prev = NULL; 22151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_next = NULL; 22161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 22171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _r_debug.r_map = map; 22181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project r_debug_tail = map; 22191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2220d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov init_linker_info_for_gdb(linker_base); 22211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 222242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes // Extract information passed from the kernel. 22230266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR)); 222442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes si->phnum = args.getauxval(AT_PHNUM); 222542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes si->entry = args.getauxval(AT_ENTRY); 22261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 22278180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner /* Compute the value of si->base. We can't rely on the fact that 22288180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner * the first entry is the PHDR because this will not be true 22298180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner * for certain executables (e.g. some in the NDK unit test suite) 22308180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner */ 22318180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner si->base = 0; 2232b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner si->size = phdr_table_get_load_size(si->phdr, si->phnum); 2233bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner si->load_bias = 0; 2234ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes for (size_t i = 0; i < si->phnum; ++i) { 223542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes if (si->phdr[i].p_type == PT_PHDR) { 22360266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr; 22370266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset; 223842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes break; 223942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes } 22408180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner } 2241d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom si->dynamic = NULL; 2242ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes si->ref_count = 1; 22431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 22442aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base); 22452aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich if (elf_hdr->e_type != ET_DYN) { 22462aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich __libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n"); 22472aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich exit(EXIT_FAILURE); 22482aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich } 22492aebf5429bb1241a3298b5b642d38f73124c2026Nick Kralevich 22504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid). 22514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes parse_LD_LIBRARY_PATH(ldpath_env); 22524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes parse_LD_PRELOAD(ldpreload_env); 22534fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 22545ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel somain = si; 22555ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel 2256183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) if (!soinfo_link_image(si, NULL)) { 2257650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer()); 225818a206c81d9743481e364384affd43306911283dElliott Hughes exit(EXIT_FAILURE); 22591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 22601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2261c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov add_vdso(args); 2262c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov 2263d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->CallPreInitConstructors(); 22649181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov 22651728b2396591853345507a063ed6075dfd251706Elliott Hughes for (size_t i = 0; g_ld_preloads[i] != NULL; ++i) { 22661728b2396591853345507a063ed6075dfd251706Elliott Hughes g_ld_preloads[i]->CallConstructors(); 2267326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng } 2268326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng 2269d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom /* After the link_image, the si->load_bias is initialized. 2270d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom * For so lib, the map->l_addr will be updated in notify_gdb_of_load. 2271d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom * We need to update this value for so exe here. So Unwind_Backtrace 2272d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom * for some arch like x86 could work correctly within so exe. 22739c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin */ 2274c5db969aa4a24a76fc0964f2ff89533e832b47a6Chao-Ying Fu map->l_addr = si->load_bias; 2275d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->CallConstructors(); 2276e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov 22771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING 2278faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes gettimeofday(&t1, NULL); 2279ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) ( 22801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) - 2281faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec))); 22821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 22831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS 2284ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0], 2285bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes linker_stats.count[kRelocAbsolute], 2286bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes linker_stats.count[kRelocRelative], 2287bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes linker_stats.count[kRelocCopy], 2288bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes linker_stats.count[kRelocSymbol]); 22891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 22901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES 22911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project { 22921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned n; 22931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned i; 22941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned count = 0; 2295d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom for (n = 0; n < 4096; n++) { 2296d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (bitmask[n]) { 22971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned x = bitmask[n]; 2298e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#if defined(__LP64__) 2299e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland for (i = 0; i < 32; i++) { 2300e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#else 2301d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom for (i = 0; i < 8; i++) { 2302e365f9d6543bc6607864ef61257505239dde15d1Marcus Oakland#endif 2303d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom if (x & 1) { 2304d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom count++; 2305d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom } 23061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project x >>= 1; 23071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 23081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 23091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2310ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4); 23111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 23121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 23131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 23141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES 23151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project fflush(stdout); 23161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 23171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2318c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry)); 23191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return si->entry; 23201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 2321468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich 2322bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only 2323bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library 2324bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself. 2325bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * 2326bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input: 2327bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * elf -> address of ELF header, assumed to be at the start of the file. 2328bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return: 2329bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * load bias, i.e. add the value of any p_vaddr in the file to get 2330bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * the corresponding address in memory. 2331bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */ 23320266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesstatic ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) { 23330266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) offset = elf->e_phoff; 2334faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes const ElfW(Phdr)* phdr_table = reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset); 23350266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum; 2336fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng 23370266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) { 2338fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng if (phdr->p_type == PT_LOAD) { 23390266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr; 2340bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner } 2341fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng } 2342fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng return 0; 2343bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner} 2344bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner 2345efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanovextern "C" void _start(); 2346efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov 2347468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/* 2348468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This 2349468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and 2350468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation(). 2351468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * 2352468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own 2353468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern 2354468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault. 2355468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */ 23560266ae5f884d72da58f33a072e865ba131234a5eElliott Hughesextern "C" ElfW(Addr) __linker_init(void* raw_args) { 235742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes KernelArgumentBlock args(raw_args); 235842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes 23590266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) linker_addr = args.getauxval(AT_BASE); 2360efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov ElfW(Addr) entry_point = args.getauxval(AT_ENTRY); 23610266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr); 2362faf05bacd45719291b371f24b1b89543881b37f6Elliott Hughes ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff); 236342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes 23640d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov soinfo linker_so("[dynamic linker]", nullptr); 236542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes 2366efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov // If the linker is not acting as PT_INTERP entry_point is equal to 2367efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov // _start. Which means that the linker is running as an executable and 2368efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov // already linked by PT_INTERP. 2369efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov // 2370efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov // This happens when user tries to run 'adb shell /system/bin/linker' 2371efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov // see also https://code.google.com/p/android/issues/detail?id=63174 2372efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) { 2373efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov __libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]); 2374efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov } 2375efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov 237642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_so.base = linker_addr; 237742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum); 237842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_so.load_bias = get_elf_exec_load_bias(elf_hdr); 2379d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom linker_so.dynamic = NULL; 238042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_so.phdr = phdr; 238142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_so.phnum = elf_hdr->e_phnum; 238242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes linker_so.flags |= FLAG_LINKER; 238342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes 2384183ad9df536ab04ef35a397a1f4724e4e401d11fTorne (Richard Coles) if (!soinfo_link_image(&linker_so, NULL)) { 238542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes // It would be nice to print an error message, but if the linker 238642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes // can't link itself, there's no guarantee that we'll be able to 2387b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes // call write() (because it involves a GOT reference). We may as 2388b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes // well try though... 2389b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes const char* msg = "CANNOT LINK EXECUTABLE: "; 2390b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes write(2, msg, strlen(msg)); 2391b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf)); 2392b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes write(2, "\n", 1); 2393b93702a0463fa0b87bf25d7ae9bdb09a35ea6a50Elliott Hughes _exit(EXIT_FAILURE); 239442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes } 2395468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich 239614241402de0faa4b244b1bd6b1f0799ce169b880Dmitriy Ivanov __libc_init_tls(args); 239714241402de0faa4b244b1bd6b1f0799ce169b880Dmitriy Ivanov 2398efe13832dccf2cec2898b89ca4418a6aac29d3ebDmitriy Ivanov // Initialize the linker's own global variables 23994151ea73b75e274d1ff80b42d9d457a783208516Dmitriy Ivanov linker_so.CallConstructors(); 24004151ea73b75e274d1ff80b42d9d457a783208516Dmitriy Ivanov 24010d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov // Initialize static variables. Note that in order to 24020d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov // get correct libdl_info we need to call constructors 24030d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov // before get_libdl_info(). 24040d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov solist = get_libdl_info(); 24050d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov sonext = get_libdl_info(); 24060d15094287fe0f288d9c258953143fc1998b6b5aDmitriy Ivanov 240742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes // We have successfully fixed our own relocations. It's safe to run 240842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes // the main part of the linker now. 24091728b2396591853345507a063ed6075dfd251706Elliott Hughes args.abort_message_ptr = &g_abort_message; 24100266ae5f884d72da58f33a072e865ba131234a5eElliott Hughes ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr); 24115419b9474753d25dff947c7740532f86d130c0beElliott Hughes 2412d59e50063ad708509f3ad83350be33f5612c4f54Dmitriy Ivanov protect_data(PROT_READ); 2413d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 241442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes // Return the address that the calling assembly stub should jump to. 241542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes return start_address; 2416468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich} 2417