linker.cpp revision 61a9ccb41eba8c35cae6e21318aca7160a402c5b
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> 321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <linux/auxvec.h> 334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <pthread.h> 341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdio.h> 351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <stdlib.h> 361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <string.h> 371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include <sys/atomics.h> 384688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <sys/mman.h> 394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <sys/stat.h> 404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <unistd.h> 411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Private C library headers. 434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <private/bionic_tls.h> 444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#include <private/logd.h> 453b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes#include <private/ScopedPthreadMutexLocker.h> 461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker.h" 481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker_debug.h" 49be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner#include "linker_environ.h" 505c734644eebf8d01be1e86cbe20a111a5c5a2738David 'Digit' Turner#include "linker_format.h" 5123363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner#include "linker_phdr.h" 521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 53bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley/* Assume average path length of 64 and max 8 paths */ 54bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley#define LDPATH_BUFSIZE 512 55bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley#define LDPATH_MAX 8 56bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley 574fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer#define LDPRELOAD_BUFSIZE 512 584fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer#define LDPRELOAD_MAX 8 594fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<< 611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Do NOT use malloc() and friends or pthread_*() code here. 631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Don't use printf() either; it's caused mysterious memory 641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * corruption in the past. 651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The linker runs before we bring up libc and it's easiest 661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to make sure it does not depend on any complex libc features 671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * open issues / todo: 691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - are we doing everything we should for ARM_COPY relocations? 711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - cleaner error reporting 721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - after linking, set as much stuff as possible to READONLY 731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * and NOEXEC 744688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes */ 751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 76124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic bool soinfo_link_image(soinfo* si); 771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 78ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn// We can't use malloc(3) in the dynamic linker. We use a linked list of anonymous 79ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn// maps, each a single page in size. The pages are broken up into as many struct soinfo 80ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn// objects as will fit, and they're all threaded together on a free list. 81ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn#define SOINFO_PER_POOL ((PAGE_SIZE - sizeof(soinfo_pool_t*)) / sizeof(soinfo)) 82ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstruct soinfo_pool_t { 83ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn soinfo_pool_t* next; 84ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn soinfo info[SOINFO_PER_POOL]; 85ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn}; 86ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic struct soinfo_pool_t* gSoInfoPools = NULL; 87ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic soinfo* gSoInfoFreeList = NULL; 88ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn 891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic soinfo *solist = &libdl_info; 901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic soinfo *sonext = &libdl_info; 914a9afcb10151b083cd2d75253385615f459172edIliyan Malchevstatic soinfo *somain; /* main process, always the one after libdl_info */ 921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 93124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* const gSoPaths[] = { 94124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes "/vendor/lib", 95124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes "/system/lib", 96124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes NULL 97124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes}; 98124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes 99124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic char gLdPathsBuffer[LDPATH_BUFSIZE]; 100124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* gLdPaths[LDPATH_MAX + 1]; 101bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley 102124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE]; 103124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* gLdPreloadNames[LDPRELOAD_MAX + 1]; 1044fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 1054fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischerstatic soinfo *preloads[LDPRELOAD_MAX + 1]; 1064fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 10761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughesstatic int debug_verbosity; 1081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 109bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesenum RelocationKind { 110bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes kRelocAbsolute = 0, 111bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes kRelocRelative, 112bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes kRelocCopy, 113bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes kRelocSymbol, 114bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes kRelocMax 115bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}; 116be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner 1171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS 118bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstruct linker_stats_t { 119bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes int count[kRelocMax]; 120bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}; 121bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes 122bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic linker_stats_t linker_stats; 123bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes 124bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind kind) { 125bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes ++linker_stats.count[kind]; 126bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes} 127bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else 128bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind) { 129bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes} 1301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 1311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES 133bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic unsigned bitmask[4096]; 134bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(offset) \ 135bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes do { \ 136bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \ 137bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes } while(0) 138bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else 139bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(x) do {} while (0) 1401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 1411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// You shouldn't try to call memory-allocating functions in the dynamic linker. 1434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Guard against the most obvious ones. 1444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#define DISALLOW_ALLOCATION(return_type, name, ...) \ 1454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return_type name __VA_ARGS__ \ 1464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes { \ 1474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \ 1484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes __libc_android_log_write(ANDROID_LOG_FATAL, "linker", msg); \ 1494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes write(2, msg, sizeof(msg)); \ 1504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes abort(); \ 1514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 1524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#define UNUSED __attribute__((unused)) 1534688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED)); 1544688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void, free, (void* u UNUSED)); 1554688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED)); 1564688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED)); 1572e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin 1580353195f344666256dba474a15c9ba22cf0cccc9Dima Zavinstatic char tmp_err_buf[768]; 1592e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavinstatic char __linker_dl_err_buf[768]; 160e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes#define DL_ERR(fmt, x...) \ 161e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes do { \ 1623b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes format_buffer(__linker_dl_err_buf, sizeof(__linker_dl_err_buf), fmt, ##x); \ 163e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes ERROR(fmt "\n", ##x); \ 1642e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin } while(0) 1652e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin 1665419b9474753d25dff947c7740532f86d130c0beElliott Hughesconst char* linker_get_error() { 1675419b9474753d25dff947c7740532f86d130c0beElliott Hughes return &__linker_dl_err_buf[0]; 1682e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin} 1692e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin 1701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* 1711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This function is an empty stub where GDB locates a breakpoint to get notified 1721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about linker activity. 1731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 1745419b9474753d25dff947c7740532f86d130c0beElliott Hughesextern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity(); 1751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 176bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic r_debug _r_debug = {1, NULL, &rtld_db_dlactivity, 1771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project RT_CONSISTENT, 0}; 178bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic link_map* r_debug_tail = 0; 1791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1803b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughesstatic pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER; 1811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 182bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void insert_soinfo_into_debug_map(soinfo * info) { 183bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes // Copy the necessary fields into the debug structure. 184bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes link_map* map = &(info->linkmap); 1851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_addr = info->base; 1861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_name = (char*) info->name; 1875cf640c926e7d7b79934c97226433a3dc448e1ebThinker K.F Li map->l_ld = (uintptr_t)info->dynamic; 1881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* Stick the new library at the end of the list. 1901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * gdb tends to care more about libc than it does 1911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about leaf libraries, and ordering it this way 1921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * reduces the back-and-forth over the wire. 1931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (r_debug_tail) { 1951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project r_debug_tail->l_next = map; 1961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_prev = r_debug_tail; 1971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_next = 0; 1981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 1991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _r_debug.r_map = map; 2001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_prev = 0; 2011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_next = 0; 2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project r_debug_tail = map; 2041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 206bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void remove_soinfo_from_debug_map(soinfo* info) { 207bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes link_map* map = &(info->linkmap); 2085e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 209bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes if (r_debug_tail == map) { 2105e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev r_debug_tail = map->l_prev; 211bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes } 2125e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 213bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes if (map->l_prev) { 214bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes map->l_prev->l_next = map->l_next; 215bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes } 216bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes if (map->l_next) { 217bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes map->l_next->l_prev = map->l_prev; 218bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes } 2195e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev} 2205e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 221bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_load(soinfo* info) { 2221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (info->flags & FLAG_EXE) { 2231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project // GDB already knows about the main executable 2241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return; 2251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 2261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2273b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes ScopedPthreadMutexLocker locker(&gDebugMutex); 2281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _r_debug.r_state = RT_ADD; 2301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project rtld_db_dlactivity(); 2311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project insert_soinfo_into_debug_map(info); 2331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 2341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _r_debug.r_state = RT_CONSISTENT; 2351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project rtld_db_dlactivity(); 2365e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev} 2375e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 238bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_unload(soinfo* info) { 2395e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev if (info->flags & FLAG_EXE) { 2405e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev // GDB already knows about the main executable 2415e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev return; 2425e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev } 2435e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 2443b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes ScopedPthreadMutexLocker locker(&gDebugMutex); 2455e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 2465e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev _r_debug.r_state = RT_DELETE; 2475e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev rtld_db_dlactivity(); 2485e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 2495e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev remove_soinfo_from_debug_map(info); 2505e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev 2515e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev _r_debug.r_state = RT_CONSISTENT; 2525e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev rtld_db_dlactivity(); 2531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 2541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 25518a206c81d9743481e364384affd43306911283dElliott Hughesvoid notify_gdb_of_libraries() { 2561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _r_debug.r_state = RT_ADD; 2571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project rtld_db_dlactivity(); 2581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _r_debug.r_state = RT_CONSISTENT; 2591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project rtld_db_dlactivity(); 2601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 2611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 262ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic bool ensure_free_list_non_empty() { 263ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn if (gSoInfoFreeList != NULL) { 264ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn return true; 265ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn } 266ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn 267ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn // Allocate a new pool. 268ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool), 269ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn PROT_READ|PROT_WRITE, 270ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn MAP_PRIVATE|MAP_ANONYMOUS, 0, 0)); 271ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn if (pool == MAP_FAILED) { 272ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn return false; 273ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn } 274ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn 275ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn // Add the pool to our list of pools. 276ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn pool->next = gSoInfoPools; 277ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn gSoInfoPools = pool; 278ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn 279ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn // Chain the entries in the new pool onto the free list. 280ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn gSoInfoFreeList = &pool->info[0]; 281ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn soinfo* next = NULL; 282ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) { 283ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn pool->info[i].next = next; 284ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn next = &pool->info[i]; 285ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn } 286ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn 287ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn return true; 288ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn} 2891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 290d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic void set_soinfo_pool_protection(int protection) { 291d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) { 292d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (mprotect(p, sizeof(*p), protection) == -1) { 293d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes abort(); // Can't happen. 294d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 295d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 296d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes} 297d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 298ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic soinfo* soinfo_alloc(const char* name) { 299ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn if (strlen(name) >= SOINFO_NAME_LEN) { 300ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn DL_ERR("library name \"%s\" too long", name); 301ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn return NULL; 302ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn } 303ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn 304ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn if (!ensure_free_list_non_empty()) { 305ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn DL_ERR("out of memory when loading \"%s\"", name); 306ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn return NULL; 307ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn } 3081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 309ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn // Take the head element off the free list. 310ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn soinfo* si = gSoInfoFreeList; 311ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn gSoInfoFreeList = gSoInfoFreeList->next; 3121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 313ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn // Initialize the new element. 314ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn memset(si, 0, sizeof(soinfo)); 315ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn strlcpy(si->name, name, sizeof(si->name)); 316ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn sonext->next = si; 317ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn sonext = si; 3181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 31961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("name %s: allocated soinfo @ %p\n", name, si); 320ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn return si; 3211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 3221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3234688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void soinfo_free(soinfo* si) 3241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 3254688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (si == NULL) { 3264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return; 3274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 3284688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 3291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project soinfo *prev = NULL, *trav; 3301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 33161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("name %s: freeing soinfo @ %p\n", si->name, si); 3321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for(trav = solist; trav != NULL; trav = trav->next){ 3341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (trav == si) 3351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 3361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project prev = trav; 3371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (trav == NULL) { 3391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* si was not ni solist */ 3404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("name \"%s\" is not in solist!", si->name); 3411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return; 3421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 344be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner /* prev will never be NULL, because the first entry in solist is 3451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project always the static libdl_info. 3461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 3471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project prev->next = si->next; 3481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (si == sonext) sonext = prev; 349ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn si->next = gSoInfoFreeList; 350ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn gSoInfoFreeList = si; 3511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 3521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 3534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#ifdef ANDROID_ARM_LINKER 3544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 3551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* For a given PC, find the .so that it belongs to. 3561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the base address of the .ARM.exidx section 3571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for that .so, and the number of 8-byte entries 3581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in that section (via *pcount). 3591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Intended to be called by libc's __gnu_Unwind_Find_exidx(). 3611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 3623b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes * This function is exposed via dlfcn.cpp and libdl.so. 3631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 3641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount) 3651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 3661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project soinfo *si; 3671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned addr = (unsigned)pc; 3681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 369468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich for (si = solist; si != 0; si = si->next){ 370468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich if ((addr >= si->base) && (addr < (si->base + si->size))) { 371468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich *pcount = si->ARM_exidx_count; 372f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee return (_Unwind_Ptr)si->ARM_exidx; 3731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *pcount = 0; 3761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return NULL; 3771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 3784688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 379d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_X86_LINKER) || defined(ANDROID_MIPS_LINKER) 3804688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 3811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Here, we only have to provide a callback to iterate across all the 3821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * loaded libraries. gcc_eh does the rest. */ 3831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectint 384bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesdl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data), 3851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project void *data) 3861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 3871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int rv = 0; 388bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes for (soinfo* si = solist; si != NULL; si = si->next) { 389bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes dl_phdr_info dl_info; 3901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project dl_info.dlpi_addr = si->linkmap.l_addr; 3911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project dl_info.dlpi_name = si->linkmap.l_name; 3921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project dl_info.dlpi_phdr = si->phdr; 3931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project dl_info.dlpi_phnum = si->phnum; 394bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes rv = cb(&dl_info, sizeof(dl_phdr_info), data); 395bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes if (rv != 0) { 3961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 397bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes } 3981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 3991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return rv; 4001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 4014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 4021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 4031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 40416084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerstatic Elf32_Sym *soinfo_elf_lookup(soinfo *si, unsigned hash, const char *name) 4051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 4061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Elf32_Sym *s; 4071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Elf32_Sym *symtab = si->symtab; 4081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const char *strtab = si->strtab; 4091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned n; 4101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 41161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(LOOKUP, "SEARCH %s in %s@0x%08x %08x %d\n", 4121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project name, si->name, si->base, hash, hash % si->nbucket); 4131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project n = hash % si->nbucket; 4141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for(n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]){ 4161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project s = symtab + n; 4171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(strcmp(strtab + s->st_name, name)) continue; 4181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 419e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan /* only concern ourselves with global and weak symbol definitions */ 4201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project switch(ELF32_ST_BIND(s->st_info)){ 4211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case STB_GLOBAL: 422e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case STB_WEAK: 423439fa8ea8c33a7945258dfe84a71ab2665efaa9aRobin Burchell if(s->st_shndx == SHN_UNDEF) 424439fa8ea8c33a7945258dfe84a71ab2665efaa9aRobin Burchell continue; 4251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 42661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(LOOKUP, "FOUND %s in %s (%08x) %d\n", 4271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project name, si->name, s->st_value, s->st_size); 4281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return s; 4291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 4301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 4311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 432943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan return NULL; 4331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 4341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic unsigned elfhash(const char *_name) 4361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 4371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const unsigned char *name = (const unsigned char *) _name; 4381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned h = 0, g; 4391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project while(*name) { 4411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project h = (h << 4) + *name++; 4421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project g = h & 0xf0000000; 4431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project h ^= g; 4441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project h ^= g >> 24; 4451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 4461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return h; 4471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 4481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 4491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic Elf32_Sym * 4505ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvelsoinfo_do_lookup(soinfo *si, const char *name, soinfo **lsi, 4515ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel soinfo *needed[]) 4526ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev{ 453943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan unsigned elf_hash = elfhash(name); 454d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich Elf32_Sym *s = NULL; 4554fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer int i; 4566ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev 457c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (si != NULL && somain != NULL) { 4585ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel 4595ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel /* 460c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * Local scope is executable scope. Just start looking into it right away 461c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * for the shortcut. 4625ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel */ 4635ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel 464c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (si == somain) { 465c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin s = soinfo_elf_lookup(si, elf_hash, name); 4665ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel if (s != NULL) { 467c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin *lsi = si; 4685ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel goto done; 4695ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 470c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin } else { 471c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */ 472c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin 473c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin /* 474c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * If this object was built with symbolic relocations disabled, the 475c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * first place to look to resolve external references is the main 476c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * executable. 477c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin */ 478c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin 479c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (!si->has_DT_SYMBOLIC) { 48061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("%s: looking up %s in executable %s\n", 48161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes si->name, name, somain->name); 482c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin s = soinfo_elf_lookup(somain, elf_hash, name); 483c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (s != NULL) { 484c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin *lsi = somain; 485c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin goto done; 486c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin } 487c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin } 4885ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel 489c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin /* Look for symbols in the local scope (the object who is 490c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * searching). This happens with C++ templates on i386 for some 491c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * reason. 492c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * 493c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * Notes on weak symbols: 494c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * The ELF specs are ambiguous about treatment of weak definitions in 495c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * dynamic linking. Some systems return the first definition found 496c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * and some the first non-weak definition. This is system dependent. 497c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * Here we return the first definition found for simplicity. */ 498c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin 499c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin s = soinfo_elf_lookup(si, elf_hash, name); 500c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (s != NULL) { 501c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin *lsi = si; 502c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin goto done; 503c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin } 504c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin 505c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin /* 506c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * If this object was built with -Bsymbolic and symbol is not found 507c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin * in the local scope, try to find the symbol in the main executable. 508c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin */ 509c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin 510c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (si->has_DT_SYMBOLIC) { 51161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("%s: looking up %s in executable %s after local scope\n", 51261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes si->name, name, somain->name); 513c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin s = soinfo_elf_lookup(somain, elf_hash, name); 514c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin if (s != NULL) { 515c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin *lsi = somain; 516c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin goto done; 517c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin } 518c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin } 5195ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 520d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich } 5216ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev 5224fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer /* Next, look for it in the preloads list */ 5234fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer for(i = 0; preloads[i] != NULL; i++) { 5245ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel s = soinfo_elf_lookup(preloads[i], elf_hash, name); 5255ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel if(s != NULL) { 5265ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel *lsi = preloads[i]; 5274fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer goto done; 5285ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 5294fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer } 5304fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 53112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel for(i = 0; needed[i] != NULL; i++) { 53261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("%s: looking up %s in %s\n", 53361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes si->name, name, needed[i]->name); 5345ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel s = soinfo_elf_lookup(needed[i], elf_hash, name); 5355ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel if (s != NULL) { 5365ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel *lsi = needed[i]; 53712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel goto done; 5385ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 5396ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev } 5406ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev 5416ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevdone: 5426ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev if(s != NULL) { 54361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = 0x%08x, " 544bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner "found in %s, base = 0x%08x, load bias = 0x%08x\n", 54561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes si->name, name, s->st_value, 5465ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel (*lsi)->name, (*lsi)->base, (*lsi)->load_bias); 5476ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev return s; 5486ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev } 5496ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev 550943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan return NULL; 5516ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev} 5526ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev 5536ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev/* This is used by dl_sym(). It performs symbol lookup only within the 5546ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev specified soinfo object and not in any of its dependencies. 5556ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */ 55616084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' TurnerElf32_Sym *soinfo_lookup(soinfo *si, const char *name) 5571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 55816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner return soinfo_elf_lookup(si, elfhash(name), name); 5591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 5601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5616ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev/* This is used by dl_sym(). It performs a global symbol lookup. 5626ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */ 5631698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt FischerElf32_Sym *lookup(const char *name, soinfo **found, soinfo *start) 5641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 565943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan unsigned elf_hash = elfhash(name); 5661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Elf32_Sym *s = NULL; 5671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project soinfo *si; 5681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5691698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer if(start == NULL) { 5701698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer start = solist; 5711698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer } 5721698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer 5731698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer for(si = start; (s == NULL) && (si != NULL); si = si->next) 5741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project { 5756ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev if(si->flags & FLAG_ERROR) 5761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project continue; 57716084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner s = soinfo_elf_lookup(si, elf_hash, name); 5781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (s != NULL) { 5799ea64da6c511e8f9f4edae4c10c20879957631abIliyan Malchev *found = si; 5801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 5811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 5821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 5831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 5846ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev if(s != NULL) { 58561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(LOOKUP, "%s s->st_value = 0x%08x, si->base = 0x%08x\n", 58661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes name, s->st_value, si->base); 5871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return s; 5881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 5891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 590943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan return NULL; 5911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 5921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 593bda5da074eab4bdf374e1f4a19d480c62c72f5ffMathias Agopiansoinfo *find_containing_library(const void *addr) 594e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer{ 595e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer soinfo *si; 596e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer 597e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer for(si = solist; si != NULL; si = si->next) 598e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer { 599e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer if((unsigned)addr >= si->base && (unsigned)addr - si->base < si->size) { 600e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer return si; 601e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer } 602e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer } 603e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer 604e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer return NULL; 605e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer} 606e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer 60716084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' TurnerElf32_Sym *soinfo_find_symbol(soinfo* si, const void *addr) 608e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer{ 609e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer unsigned int i; 610e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer unsigned soaddr = (unsigned)addr - si->base; 611e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer 612e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer /* Search the library's symbol table for any defined symbol which 613e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer * contains this address */ 614e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer for(i=0; i<si->nchain; i++) { 615e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer Elf32_Sym *sym = &si->symtab[i]; 616e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer 617e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer if(sym->st_shndx != SHN_UNDEF && 618e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer soaddr >= sym->st_value && 619e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer soaddr < sym->st_value + sym->st_size) { 620e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer return sym; 621e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer } 622e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer } 623e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer 624e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer return NULL; 625e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer} 626e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer 6271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0 6281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic void dump(soinfo *si) 6291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 6301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Elf32_Sym *s = si->symtab; 6311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned n; 6321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for(n = 0; n < si->nchain; n++) { 63461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("%04d> %08x: %02x %04x %08x %08x %s\n", n, s, 6351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project s->st_info, s->st_shndx, s->st_value, s->st_size, 6361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->strtab + s->st_name); 6371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project s++; 6381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 6391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 6401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 6411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 642124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library_on_path(const char* name, const char* const paths[]) { 643124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes char buf[512]; 644124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes for (size_t i = 0; paths[i] != NULL; ++i) { 645124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes int n = format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name); 646124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (n < 0 || n >= static_cast<int>(sizeof(buf))) { 647124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes WARN("Ignoring very long library path: %s/%s\n", paths[i], name); 648124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes continue; 6491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 650124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC)); 651124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (fd != -1) { 652124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return fd; 653124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 654124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 655124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return -1; 6561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 6571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 658124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library(const char* name) { 65961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ opening %s ]\n", name); 6601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 661124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes // If the name contains a slash, we should attempt to open it directly and not search the paths. 662124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (strchr(name, '/') != NULL) { 6636971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC)); 6646971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes if (fd != -1) { 6656971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes return fd; 6666971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes } 6676971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now. 668124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 6691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 670124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths. 671124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes int fd = open_library_on_path(name, gLdPaths); 672124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (fd == -1) { 673124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes fd = open_library_on_path(name, gSoPaths); 674124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 675124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return fd; 6761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 6771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 6784688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Returns 'true' if the library is prelinked or on failure so we error out 6794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// either way. We no longer support prelinking. 6804688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic bool is_prelinked(int fd, const char* name) 6811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 6824688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes struct prelink_info_t { 6834688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes long mmap_addr; 6844688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes char tag[4]; // "PRE ". 6854688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes }; 6864688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 687bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes off_t sz = lseek(fd, -sizeof(prelink_info_t), SEEK_END); 6881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (sz < 0) { 6894688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("lseek failed: %s", strerror(errno)); 6904688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return true; 6911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 6921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 693bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes prelink_info_t info; 6948dfc073b1487ab25ab483ab346cee1d9e584adb1Elliott Hughes int rc = TEMP_FAILURE_RETRY(read(fd, &info, sizeof(info))); 6958dfc073b1487ab25ab483ab346cee1d9e584adb1Elliott Hughes if (rc != sizeof(info)) { 69661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DL_ERR("could not read prelink_info_t structure for \"%s\": %s", name, strerror(errno)); 6974688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return true; 6981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 6991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (memcmp(info.tag, "PRE ", 4) == 0) { 7014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("prelinked libraries no longer supported: %s", name); 7024688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return true; 7031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 7044688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return false; 7051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 7061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 70716084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner/* verify_elf_header 70816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner * Verifies the content of an ELF header. 7091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 7101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Args: 7111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 7121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns: 7131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * 0 on success 7141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * -1 if no valid ELF object is found @ base. 7151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 7161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectstatic int 71716084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turnerverify_elf_header(const Elf32_Ehdr* hdr) 7181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 7191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (hdr->e_ident[EI_MAG0] != ELFMAG0) return -1; 7201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (hdr->e_ident[EI_MAG1] != ELFMAG1) return -1; 7211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (hdr->e_ident[EI_MAG2] != ELFMAG2) return -1; 7221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (hdr->e_ident[EI_MAG3] != ELFMAG3) return -1; 723d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich if (hdr->e_type != ET_DYN) return -1; 7241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* TODO: Should we verify anything else in the header? */ 726897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#ifdef ANDROID_ARM_LINKER 727897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang if (hdr->e_machine != EM_ARM) return -1; 728897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#elif defined(ANDROID_X86_LINKER) 729897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang if (hdr->e_machine != EM_386) return -1; 730d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_MIPS_LINKER) 731d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if (hdr->e_machine != EM_MIPS) return -1; 732897815a1feff230be3ea42655a77dcbb9a8dcca9Zhenghua Wang#endif 7331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 7341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 7351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct scoped_fd { 7374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes ~scoped_fd() { 7384688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (fd != -1) { 7394688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes close(fd); 7404688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 7414688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 7424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes int fd; 7434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes}; 7441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct soinfo_ptr { 7464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes soinfo_ptr(const char* name) { 7474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes const char* bname = strrchr(name, '/'); 7484688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes ptr = soinfo_alloc(bname ? bname + 1 : name); 7494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 7504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes ~soinfo_ptr() { 7514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes soinfo_free(ptr); 7524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 7534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes soinfo* release() { 7544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes soinfo* result = ptr; 7554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes ptr = NULL; 7564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return result; 7574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 7584688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes soinfo* ptr; 7594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes}; 7601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// TODO: rewrite linker_phdr.h to use a class, then lose this. 7624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstruct phdr_ptr { 7634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes phdr_ptr() : phdr_mmap(NULL) {} 7644688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes ~phdr_ptr() { 7654688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (phdr_mmap != NULL) { 7664688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes phdr_table_unload(phdr_mmap, phdr_size); 7674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 7684688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 7694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes void* phdr_mmap; 7704688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes Elf32_Addr phdr_size; 7714688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes}; 772b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner 773124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic soinfo* load_library(const char* name) { 7744688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes // Open the file. 7754688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes scoped_fd fd; 7764688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes fd.fd = open_library(name); 7774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (fd.fd == -1) { 7784688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("library \"%s\" not found", name); 7791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return NULL; 7802e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin } 7811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7824688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes // Read the ELF header. 7834688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes Elf32_Ehdr header[1]; 7844688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes int ret = TEMP_FAILURE_RETRY(read(fd.fd, (void*)header, sizeof(header))); 78523363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner if (ret < 0) { 7864688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("can't read file \"%s\": %s", name, strerror(errno)); 7874688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 78823363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner } 78923363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner if (ret != (int)sizeof(header)) { 7904688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("too small to be an ELF executable: %s", name); 7914688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 79223363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner } 79323363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner if (verify_elf_header(header) < 0) { 7944688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("not a valid ELF executable: %s", name); 7954688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 7961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 7971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 7984688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes // Read the program header table. 7994688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes const Elf32_Phdr* phdr_table; 8004688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes phdr_ptr phdr_holder; 8014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes ret = phdr_table_load(fd.fd, header->e_phoff, header->e_phnum, 8024688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes &phdr_holder.phdr_mmap, &phdr_holder.phdr_size, &phdr_table); 80323363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner if (ret < 0) { 8044688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("can't load program header table: %s: %s", name, strerror(errno)); 8054688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 8061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 8074688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes size_t phdr_count = header->e_phnum; 8081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8094688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes // Get the load extents. 8104688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes Elf32_Addr ext_sz = phdr_table_get_load_size(phdr_table, phdr_count); 81161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ '%s' wants sz=0x%08x ]\n", name, ext_sz); 81223363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner if (ext_sz == 0) { 8134688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("no loadable segments in file: %s", name); 8144688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 81523363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner } 81623363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner 8174688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes // We no longer support pre-linked libraries. 8184688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (is_prelinked(fd.fd, name)) { 8194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 82023363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner } 82116084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner 8224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes // Reserve address space for all loadable segments. 8234688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes void* load_start = NULL; 8244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes Elf32_Addr load_size = 0; 8254688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes Elf32_Addr load_bias = 0; 826b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner ret = phdr_table_reserve_memory(phdr_table, 827b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner phdr_count, 828b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner &load_start, 829b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner &load_size, 830b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner &load_bias); 831b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (ret < 0) { 8324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("can't reserve %d bytes in address space for \"%s\": %s", 8334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes ext_sz, name, strerror(errno)); 8344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 835b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner } 8361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 83761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ allocated memory for %s @ %p (0x%08x) ]\n", name, load_start, load_size); 838b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner 839b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner /* Map all the segments in our address space with default protections */ 840b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner ret = phdr_table_load_segments(phdr_table, 841b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner phdr_count, 842b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner load_bias, 8434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes fd.fd); 844b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (ret < 0) { 8454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("can't map loadable segments for \"%s\": %s", 8464688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes name, strerror(errno)); 8474688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 848b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner } 8491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes soinfo_ptr si(name); 8514688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (si.ptr == NULL) { 8524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 8531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 8541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 8554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes si.ptr->base = (Elf32_Addr) load_start; 8564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes si.ptr->size = load_size; 8574688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes si.ptr->load_bias = load_bias; 8584688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes si.ptr->flags = 0; 8594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes si.ptr->entry = 0; 8604688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes si.ptr->dynamic = (unsigned *)-1; 8614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes si.ptr->phnum = phdr_count; 8624688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes si.ptr->phdr = phdr_table_get_loaded_phdr(phdr_table, phdr_count, load_bias); 8634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (si.ptr->phdr == NULL) { 8644688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("can't find loaded PHDR for \"%s\"", name); 8654688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return NULL; 86623363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner } 8674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes 8684688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return si.release(); 8691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 8701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 871d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic soinfo* init_library(soinfo* si) { 872d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // At this point we know that whatever is loaded @ base is a valid ELF 873d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // shared library whose segments are properly mapped in. 87461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ init_library base=0x%08x sz=0x%08x name='%s') ]\n", 87561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes si->base, si->size, si->name); 8761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 877d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (!soinfo_link_image(si)) { 878d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes munmap((void *)si->base, si->size); 879d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return NULL; 880d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 8811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 882d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return si; 8831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 8841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 88512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvelstatic soinfo *find_loaded_library(const char *name) 8861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 8871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project soinfo *si; 8886774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner const char *bname; 8896774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner 89012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel // TODO: don't use basename only for determining libraries 89112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel // http://code.google.com/p/android/issues/detail?id=6670 89212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel 89312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel bname = strrchr(name, '/'); 89412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel bname = bname ? bname + 1 : name; 89512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel 89612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel for(si = solist; si != NULL; si = si->next){ 89712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel if(!strcmp(bname, si->name)) { 89812c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel return si; 89912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel } 90012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel } 90112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel return NULL; 90212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel} 90312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel 904d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic soinfo* find_library_internal(const char* name) { 905d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (name == NULL) { 906d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return somain; 907d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 908d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 909d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes soinfo* si = find_loaded_library(name); 910d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si != NULL) { 911d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si->flags & FLAG_ERROR) { 912d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes DL_ERR("\"%s\" failed to load previously", name); 913d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return NULL; 914d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 915d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si->flags & FLAG_LINKED) { 916d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return si; 917d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 918d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes DL_ERR("OOPS: recursive link to \"%s\"", si->name); 919d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return NULL; 920d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 92112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel 92261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ '%s' has not been loaded yet. Locating...]\n", name); 923d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si = load_library(name); 924d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si != NULL) { 925d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si = init_library(si); 926d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 9276774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner 928d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return si; 929d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes} 930d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 931d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic soinfo* find_library(const char* name) { 932d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes soinfo* si = find_library_internal(name); 933d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si != NULL) { 934d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->refcount++; 935d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 936d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return si; 937d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes} 938d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 939d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic int soinfo_unload(soinfo* si) { 940d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si->refcount == 1) { 94161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("unloading '%s'\n", si->name); 942d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->CallDestructors(); 943d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 944d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes for (unsigned* d = si->dynamic; *d; d += 2) { 945d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (d[0] == DT_NEEDED) { 946d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes soinfo* lsi = find_loaded_library(si->strtab + d[1]); 947d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (lsi != NULL) { 94861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("%s needs to unload %s\n", si->name, lsi->name); 949d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes soinfo_unload(lsi); 950d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } else { 951d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // TODO: should we return -1 in this case? 952d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes DL_ERR("\"%s\": could not unload dependent library", si->name); 9531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 954d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 9551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 9561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 957d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes munmap(reinterpret_cast<void*>(si->base), si->size); 958d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes notify_gdb_of_unload(si); 959d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes soinfo_free(si); 960d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->refcount = 0; 961d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } else { 962d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->refcount--; 96361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes PRINT("not unloading '%s', decrementing refcount to %d\n", si->name, si->refcount); 964d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 965d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return 0; 9661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 9671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 968d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughessoinfo* do_dlopen(const char* name) { 969d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes set_soinfo_pool_protection(PROT_READ | PROT_WRITE); 970d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes soinfo* si = find_library(name); 971d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si != NULL) { 972d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->CallConstructors(); 973d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 974d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes set_soinfo_pool_protection(PROT_READ); 975d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return si; 976d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes} 9771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 978d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesint do_dlclose(soinfo* si) { 979d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes set_soinfo_pool_protection(PROT_READ | PROT_WRITE); 980d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes int result = soinfo_unload(si); 981d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes set_soinfo_pool_protection(PROT_READ); 982d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return result; 9831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 9841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* TODO: don't use unsigned for addrs below. It works, but is not 9861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * ideal. They should probably be either uint32_t, Elf32_Addr, or unsigned 9871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * long. 9881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 98912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvelstatic int soinfo_relocate(soinfo *si, Elf32_Rel *rel, unsigned count, 99012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel soinfo *needed[]) 9911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 9921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Elf32_Sym *symtab = si->symtab; 9931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project const char *strtab = si->strtab; 9941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Elf32_Sym *s; 9951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project Elf32_Rel *start = rel; 9965ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel soinfo *lsi; 9971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 9984688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes for (size_t idx = 0; idx < count; ++idx, ++rel) { 9991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned type = ELF32_R_TYPE(rel->r_info); 10001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned sym = ELF32_R_SYM(rel->r_info); 1001bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner unsigned reloc = (unsigned)(rel->r_offset + si->load_bias); 10021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned sym_addr = 0; 10031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char *sym_name = NULL; 10041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 100561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("Processing '%s' relocation at index %d\n", si->name, idx); 1006d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if (type == 0) { // R_*_NONE 1007d7daacb46372132ae3f0121647074936c304b572Raghu Gandham continue; 1008d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 10091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(sym != 0) { 1010d1b40d8c69dfca94185ea9231f6cb16f70c02e39Dima Zavin sym_name = (char *)(strtab + symtab[sym].st_name); 10115ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel s = soinfo_do_lookup(si, sym_name, &lsi, needed); 1012e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan if(s == NULL) { 1013e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan /* We only allow an undefined symbol if this is a weak 1014e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan reference.. */ 1015e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan s = &symtab[sym]; 1016e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan if (ELF32_ST_BIND(s->st_info) != STB_WEAK) { 1017e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name); 1018e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan return -1; 1019e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan } 1020e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1021e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan /* IHI0044C AAELF 4.5.1.1: 1022e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1023e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan Libraries are not searched to resolve weak references. 1024e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan It is not an error for a weak reference to remain 1025e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan unsatisfied. 1026e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1027e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan During linking, the value of an undefined weak reference is: 1028e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan - Zero if the relocation type is absolute 1029e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan - The address of the place if the relocation is pc-relative 1030bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes - The address of nominal base address if the relocation 1031e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan type is base-relative. 1032e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan */ 1033e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1034e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan switch (type) { 1035e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_ARM_LINKER) 1036e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_JUMP_SLOT: 1037e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_GLOB_DAT: 1038e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_ABS32: 1039e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_RELATIVE: /* Don't care. */ 1040e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#elif defined(ANDROID_X86_LINKER) 1041d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case R_386_JMP_SLOT: 1042e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_386_GLOB_DAT: 1043e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_386_32: 1044e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_386_RELATIVE: /* Dont' care. */ 1045e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_*_LINKER */ 1046e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan /* sym_addr was initialized to be zero above or relocation 1047e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan code below does not care about value of sym_addr. 1048e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan No need to do anything. */ 1049e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan break; 1050e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1051e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_X86_LINKER) 1052e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_386_PC32: 1053e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan sym_addr = reloc; 1054e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan break; 1055e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_X86_LINKER */ 1056e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan 1057e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#if defined(ANDROID_ARM_LINKER) 1058e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan case R_ARM_COPY: 1059e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan /* Fall through. Can't really copy if weak symbol is 1060e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan not found in run-time. */ 1061e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan#endif /* ANDROID_ARM_LINKER */ 1062e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan default: 10634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("unknown weak reloc type %d @ %p (%d)", 10644688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes type, rel, (int) (rel - start)); 1065e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan return -1; 1066e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan } 1067e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan } else { 1068e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan /* We got a definition. */ 10691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0 1070d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if((base == 0) && (si->base != 0)){ 1071d7daacb46372132ae3f0121647074936c304b572Raghu Gandham /* linking from libraries to main image is bad */ 10724688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("cannot locate \"%s\"...", 10734688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes strtab + symtab[sym].st_name); 1074d7daacb46372132ae3f0121647074936c304b572Raghu Gandham return -1; 1075d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 10761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 10775ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel sym_addr = (unsigned)(s->st_value + lsi->load_bias); 1078d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1079bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocSymbol); 10801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } else { 1081e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan s = NULL; 10821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 10831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 10841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* TODO: This is ugly. Split up the relocations by arch into 10851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * different files. 10861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 10871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project switch(type){ 10881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_ARM_LINKER) 10891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_ARM_JUMP_SLOT: 1090bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocAbsolute); 10911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 109261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name); 10931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *((unsigned*)reloc) = sym_addr; 10941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 10951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_ARM_GLOB_DAT: 1096bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocAbsolute); 10971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 109861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s\n", reloc, sym_addr, sym_name); 10991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *((unsigned*)reloc) = sym_addr; 11001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 11011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_ARM_ABS32: 1102bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocAbsolute); 11031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 110461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s\n", reloc, sym_addr, sym_name); 11051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *((unsigned*)reloc) += sym_addr; 11061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 110734ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner case R_ARM_REL32: 1108bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocRelative); 110934ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner MARK(rel->r_offset); 111061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s\n", 111134ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner reloc, sym_addr, rel->r_offset, sym_name); 111234ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner *((unsigned*)reloc) += sym_addr - rel->r_offset; 111334ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner break; 11141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#elif defined(ANDROID_X86_LINKER) 1115d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case R_386_JMP_SLOT: 1116bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocAbsolute); 11171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 111861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name); 11191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *((unsigned*)reloc) = sym_addr; 11201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 11211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_386_GLOB_DAT: 1122bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocAbsolute); 11231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 112461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s\n", reloc, sym_addr, sym_name); 11251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *((unsigned*)reloc) = sym_addr; 11261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 1127d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#elif defined(ANDROID_MIPS_LINKER) 1128d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case R_MIPS_JUMP_SLOT: 1129bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocAbsolute); 1130d7daacb46372132ae3f0121647074936c304b572Raghu Gandham MARK(rel->r_offset); 113161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s\n", reloc, sym_addr, sym_name); 1132d7daacb46372132ae3f0121647074936c304b572Raghu Gandham *((unsigned*)reloc) = sym_addr; 1133d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1134d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case R_MIPS_REL32: 1135bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocAbsolute); 1136d7daacb46372132ae3f0121647074936c304b572Raghu Gandham MARK(rel->r_offset); 113761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s\n", 1138d7daacb46372132ae3f0121647074936c304b572Raghu Gandham reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*"); 1139d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if (s) { 1140d7daacb46372132ae3f0121647074936c304b572Raghu Gandham *((unsigned*)reloc) += sym_addr; 1141d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } else { 1142d7daacb46372132ae3f0121647074936c304b572Raghu Gandham *((unsigned*)reloc) += si->base; 1143d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1144d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 11451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_*_LINKER */ 11461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_ARM_LINKER) 11481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_ARM_RELATIVE: 11491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#elif defined(ANDROID_X86_LINKER) 11501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_386_RELATIVE: 11511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_*_LINKER */ 1152bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocRelative); 11531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 11544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (sym) { 115561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DL_ERR("odd RELATIVE form..."); 11561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return -1; 11571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 115861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(RELO, "RELO RELATIVE %08x <- +%08x\n", reloc, si->base); 11591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *((unsigned*)reloc) += si->base; 11601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 11611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if defined(ANDROID_X86_LINKER) 11631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_386_32: 1164bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocRelative); 11651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 11661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 116761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s\n", reloc, sym_addr, sym_name); 11681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *((unsigned *)reloc) += (unsigned)sym_addr; 11691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 11701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_386_PC32: 1172bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocRelative); 11731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 117461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s\n", 117561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes reloc, (sym_addr - reloc), sym_addr, reloc, sym_name); 11761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *((unsigned *)reloc) += (unsigned)(sym_addr - reloc); 11771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 11781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_X86_LINKER */ 11791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 11801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#ifdef ANDROID_ARM_LINKER 11811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case R_ARM_COPY: 1182d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich if ((si->flags & FLAG_EXE) == 0) { 1183d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich /* 1184d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf 1185d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich * 1186d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich * Section 4.7.1.10 "Dynamic relocations" 1187d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich * R_ARM_COPY may only appear in executable objects where e_type is 1188d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich * set to ET_EXEC. 1189d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich * 1190d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables. 1191d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich * We should explicitly disallow ET_DYN executables from having 1192d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich * R_ARM_COPY relocations. 1193d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich */ 1194d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name); 1195d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich return -1; 1196d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich } 1197bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes count_relocation(kRelocCopy); 11981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project MARK(rel->r_offset); 119961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s\n", reloc, s->st_size, sym_addr, sym_name); 1200d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich if (reloc == sym_addr) { 12015ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel Elf32_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed); 12025ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel 12035ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel if (src == NULL) { 12045ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name); 12055ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel return -1; 12065ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 12075ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel if (lsi->has_DT_SYMBOLIC) { 12085ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared " 12095ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel "library %s (built with -Bsymbolic?)", si->name, lsi->name); 12105ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel return -1; 12115ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 12125ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel if (s->st_size < src->st_size) { 12135ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)", 12145ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel si->name, s->st_size, src->st_size); 12155ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel return -1; 12165ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 12175ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size); 12185ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } else { 12195ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name); 1220d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich return -1; 1221d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich } 12221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 12231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif /* ANDROID_ARM_LINKER */ 12241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 12251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project default: 12264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("unknown reloc type %d @ %p (%d)", 12274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes type, rel, (int) (rel - start)); 12281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return -1; 12291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 12301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 12311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return 0; 12321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 12331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1234d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#ifdef ANDROID_MIPS_LINKER 1235bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic int mips_relocate_got(soinfo* si, soinfo* needed[]) { 1236d7daacb46372132ae3f0121647074936c304b572Raghu Gandham unsigned *got; 1237d7daacb46372132ae3f0121647074936c304b572Raghu Gandham unsigned local_gotno, gotsym, symtabno; 1238d7daacb46372132ae3f0121647074936c304b572Raghu Gandham Elf32_Sym *symtab, *sym; 1239d7daacb46372132ae3f0121647074936c304b572Raghu Gandham unsigned g; 1240d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1241d7daacb46372132ae3f0121647074936c304b572Raghu Gandham got = si->plt_got; 1242d7daacb46372132ae3f0121647074936c304b572Raghu Gandham local_gotno = si->mips_local_gotno; 1243d7daacb46372132ae3f0121647074936c304b572Raghu Gandham gotsym = si->mips_gotsym; 1244d7daacb46372132ae3f0121647074936c304b572Raghu Gandham symtabno = si->mips_symtabno; 1245d7daacb46372132ae3f0121647074936c304b572Raghu Gandham symtab = si->symtab; 1246d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1247d7daacb46372132ae3f0121647074936c304b572Raghu Gandham /* 1248d7daacb46372132ae3f0121647074936c304b572Raghu Gandham * got[0] is address of lazy resolver function 1249d7daacb46372132ae3f0121647074936c304b572Raghu Gandham * got[1] may be used for a GNU extension 1250bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes * set it to a recognizable address in case someone calls it 1251d7daacb46372132ae3f0121647074936c304b572Raghu Gandham * (should be _rtld_bind_start) 1252d7daacb46372132ae3f0121647074936c304b572Raghu Gandham * FIXME: maybe this should be in a separate routine 1253d7daacb46372132ae3f0121647074936c304b572Raghu Gandham */ 1254d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1255d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if ((si->flags & FLAG_LINKER) == 0) { 1256d7daacb46372132ae3f0121647074936c304b572Raghu Gandham g = 0; 1257d7daacb46372132ae3f0121647074936c304b572Raghu Gandham got[g++] = 0xdeadbeef; 1258d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if (got[g] & 0x80000000) { 1259d7daacb46372132ae3f0121647074936c304b572Raghu Gandham got[g++] = 0xdeadfeed; 1260d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1261d7daacb46372132ae3f0121647074936c304b572Raghu Gandham /* 1262d7daacb46372132ae3f0121647074936c304b572Raghu Gandham * Relocate the local GOT entries need to be relocated 1263d7daacb46372132ae3f0121647074936c304b572Raghu Gandham */ 1264d7daacb46372132ae3f0121647074936c304b572Raghu Gandham for (; g < local_gotno; g++) { 1265d7daacb46372132ae3f0121647074936c304b572Raghu Gandham got[g] += si->load_bias; 1266d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1267d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1268d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1269d7daacb46372132ae3f0121647074936c304b572Raghu Gandham /* Now for the global GOT entries */ 1270d7daacb46372132ae3f0121647074936c304b572Raghu Gandham sym = symtab + gotsym; 1271d7daacb46372132ae3f0121647074936c304b572Raghu Gandham got = si->plt_got + local_gotno; 1272d7daacb46372132ae3f0121647074936c304b572Raghu Gandham for (g = gotsym; g < symtabno; g++, sym++, got++) { 1273d7daacb46372132ae3f0121647074936c304b572Raghu Gandham const char *sym_name; 1274d7daacb46372132ae3f0121647074936c304b572Raghu Gandham Elf32_Sym *s; 12755ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel soinfo *lsi; 1276d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1277d7daacb46372132ae3f0121647074936c304b572Raghu Gandham /* This is an undefined reference... try to locate it */ 1278d7daacb46372132ae3f0121647074936c304b572Raghu Gandham sym_name = si->strtab + sym->st_name; 12795ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel s = soinfo_do_lookup(si, sym_name, &lsi, needed); 1280d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if (s == NULL) { 1281d7daacb46372132ae3f0121647074936c304b572Raghu Gandham /* We only allow an undefined symbol if this is a weak 1282d7daacb46372132ae3f0121647074936c304b572Raghu Gandham reference.. */ 1283d7daacb46372132ae3f0121647074936c304b572Raghu Gandham s = &symtab[g]; 1284d7daacb46372132ae3f0121647074936c304b572Raghu Gandham if (ELF32_ST_BIND(s->st_info) != STB_WEAK) { 12854688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("cannot locate \"%s\"...", sym_name); 1286d7daacb46372132ae3f0121647074936c304b572Raghu Gandham return -1; 1287d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1288d7daacb46372132ae3f0121647074936c304b572Raghu Gandham *got = 0; 1289d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1290d7daacb46372132ae3f0121647074936c304b572Raghu Gandham else { 1291d7daacb46372132ae3f0121647074936c304b572Raghu Gandham /* FIXME: is this sufficient? 1292d7daacb46372132ae3f0121647074936c304b572Raghu Gandham * For reference see NetBSD link loader 1293d7daacb46372132ae3f0121647074936c304b572Raghu Gandham * 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 1294d7daacb46372132ae3f0121647074936c304b572Raghu Gandham */ 12955ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel *got = lsi->load_bias + s->st_value; 1296d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1297d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1298d7daacb46372132ae3f0121647074936c304b572Raghu Gandham return 0; 1299d7daacb46372132ae3f0121647074936c304b572Raghu Gandham} 1300d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif 1301d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 13028215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner/* Please read the "Initialization and Termination functions" functions. 13038215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * of the linker design note in bionic/linker/README.TXT to understand 13048215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * what the following code is doing. 13058215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * 13068215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * The important things to remember are: 13078215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * 13088215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * DT_PREINIT_ARRAY must be called first for executables, and should 13098215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * not appear in shared libraries. 13108215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * 13118215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * DT_INIT should be called before DT_INIT_ARRAY if both are present 13128215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * 13138215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * DT_FINI should be called after DT_FINI_ARRAY if both are present 13148215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * 13158215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner * DT_FINI_ARRAY must be parsed in reverse order. 13168215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner */ 1317d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallArray(const char* array_name UNUSED, unsigned* array, int count, bool reverse) { 1318d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (array == NULL) { 1319d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return; 1320d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 13218215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner 1322d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes int step = 1; 1323d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (reverse) { 1324d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes array += (count-1); 1325d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes step = -1; 1326d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 13278215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner 132861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ Calling %s @ %p [%d] for '%s' ]\n", array_name, array, count, name); 13298215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner 1330d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes for (int n = count; n > 0; n--) { 133161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ Looking at %s[%d] *%p == 0x%08x ]\n", array_name, n, array, *array); 1332d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes void (*func)() = (void (*)()) *array; 1333d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes array += step; 1334d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (((int) func == 0) || ((int) func == -1)) { 1335d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes continue; 13361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 133761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ Calling func @ %p ]\n", func); 1338d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes func(); 1339d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 1340d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 134161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ Done calling %s for '%s' ]\n", array_name, name); 13421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 13431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1344d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallFunction(const char* function_name UNUSED, void (*function)()) { 1345d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (function == NULL) { 1346d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return; 1347d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 1348d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 134961a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ Calling %s @ %p for '%s' ]\n", function_name, function, name); 1350d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes function(); 135161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ Done calling %s for '%s' ]\n", function_name, name); 13529181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov} 13539181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov 1354d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallPreInitConstructors() { 1355d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false); 1356d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes} 1357e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov 1358d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallConstructors() { 1359d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (constructors_called) { 1360d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return; 1361d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 1362e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov 1363d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // We set constructors_called before actually calling the constructors, otherwise it doesn't 1364d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // protect against recursive constructor calls. One simple example of constructor recursion 1365d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so: 1366d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // 1. The program depends on libc, so libc's constructor is called here. 1367d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so. 1368d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // 3. dlopen() calls the constructors on the newly created 1369d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // soinfo for libc_malloc_debug_leak.so. 1370d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // 4. The debug .so depends on libc, so CallConstructors is 1371d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // called again with the libc soinfo. If it doesn't trigger the early- 1372d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes // out above, the libc constructor will be called again (recursively!). 1373d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes constructors_called = true; 1374d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 1375d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (!(flags & FLAG_EXE) && preinit_array) { 1376d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes DL_ERR("shared library \"%s\" has a preinit_array table @ %p", name, preinit_array); 1377d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes return; 1378d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 13791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1380d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (dynamic) { 1381d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes for (unsigned* d = dynamic; *d; d += 2) { 1382d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (d[0] == DT_NEEDED) { 1383d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes soinfo* lsi = find_loaded_library(strtab + d[1]); 1384d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (lsi == NULL) { 1385d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes DL_ERR("\"%s\": could not initialize dependent library", name); 1386d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } else { 1387d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes lsi->CallConstructors(); 1388d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 1389d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 13901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1391d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes } 13921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1393d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallFunction("DT_INIT", init_func); 1394d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallArray("DT_INIT_ARRAY", init_array, init_array_count, false); 1395e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov} 13968215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner 1397d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallDestructors() { 1398d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true); 1399d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes CallFunction("DT_FINI", fini_func); 14001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 14011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with 14031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /dev/null. */ 14045419b9474753d25dff947c7740532f86d130c0beElliott Hughesstatic int nullify_closed_stdio() { 14051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int dev_null, i, status; 14061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int return_value = 0; 14071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 140816084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR)); 14091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (dev_null < 0) { 14104688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("cannot open /dev/null: %s", strerror(errno)); 14111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return -1; 14121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 141361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ Opened /dev/null file-descriptor=%d]\n", dev_null); 14141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* If any of the stdio file descriptors is valid and not associated 14161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project with /dev/null, dup /dev/null to it. */ 14171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for (i = 0; i < 3; i++) { 14181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* If it is /dev/null already, we are done. */ 14194688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (i == dev_null) { 14201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project continue; 14214688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 14221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 142361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ Nullifying stdio file descriptor %d]\n", i); 14244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL)); 14251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14264688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes /* If file is opened, we are good. */ 14274688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (status != -1) { 14284688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes continue; 14294688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 14301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* The only error we allow is that the file descriptor does not 14321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project exist, in which case we dup /dev/null to it. */ 14331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (errno != EBADF) { 14344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("fcntl failed: %s", strerror(errno)); 14351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return_value = -1; 14361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project continue; 14371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 14381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* Try dupping /dev/null to this stdio file descriptor and 14401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project repeat if there is a signal. Note that any errors in closing 14411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project the stdio descriptor are lost. */ 14424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes status = TEMP_FAILURE_RETRY(dup2(dev_null, i)); 14431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (status < 0) { 14444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("dup2 failed: %s", strerror(errno)); 14451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return_value = -1; 14461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project continue; 14471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 14481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 14491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* If /dev/null is not one of the stdio file descriptors, close it. */ 14511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if (dev_null > 2) { 145261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ Closing /dev/null file-descriptor=%d]\n", dev_null); 14534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes status = TEMP_FAILURE_RETRY(close(dev_null)); 14544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (status == -1) { 14554688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("close failed: %s", strerror(errno)); 14561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return_value = -1; 14571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 14581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 14591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 14601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return return_value; 14611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 14621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1463124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic bool soinfo_link_image(soinfo* si) { 1464124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes si->flags |= FLAG_ERROR; 1465124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes 1466f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee /* "base" might wrap around UINT32_MAX. */ 1467bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner Elf32_Addr base = si->load_bias; 1468b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner const Elf32_Phdr *phdr = si->phdr; 14691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int phnum = si->phnum; 1470b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner int relocating_linker = (si->flags & FLAG_LINKER) != 0; 147112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel soinfo **needed, **pneeded; 14721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1473b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner /* We can't debug anything until the linker is relocated */ 1474b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (!relocating_linker) { 147561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes INFO("[ linking %s ]\n", si->name); 147661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("si->base = 0x%08x si->flags = 0x%08x\n", si->base, si->flags); 1477b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner } 14781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 147963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner /* Extract dynamic section */ 1480124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes size_t dynamic_count; 148112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic, 148212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel &dynamic_count); 148363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner if (si->dynamic == NULL) { 1484b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (!relocating_linker) { 1485124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name); 1486b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner } 1487124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 148863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner } else { 1489b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (!relocating_linker) { 149061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("dynamic = %p\n", si->dynamic); 1491b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner } 149263f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner } 149363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner 149463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#ifdef ANDROID_ARM_LINKER 149563f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner (void) phdr_table_get_arm_exidx(phdr, phnum, base, 149663f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner &si->ARM_exidx, &si->ARM_exidx_count); 149763f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif 149863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner 14991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* extract useful information from dynamic section */ 1500124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes for (unsigned* d = si->dynamic; *d; ++d) { 150161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("d = %p, d[0] = 0x%08x d[1] = 0x%08x\n", d, d[0], d[1]); 15021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project switch(*d++){ 15031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_HASH: 1504f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->nbucket = ((unsigned *) (base + *d))[0]; 1505f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->nchain = ((unsigned *) (base + *d))[1]; 1506f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->bucket = (unsigned *) (base + *d + 8); 1507f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->chain = (unsigned *) (base + *d + 8 + si->nbucket * 4); 15081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_STRTAB: 1510f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->strtab = (const char *) (base + *d); 15111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_SYMTAB: 1513f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->symtab = (Elf32_Sym *) (base + *d); 15141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PLTREL: 15161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(*d != DT_REL) { 1517124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("unsupported DT_RELA in \"%s\"", si->name); 1518124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 15191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 15201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_JMPREL: 1522f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->plt_rel = (Elf32_Rel*) (base + *d); 15231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PLTRELSZ: 15251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->plt_rel_count = *d / 8; 15261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_REL: 1528f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->rel = (Elf32_Rel*) (base + *d); 15291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_RELSZ: 15311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->rel_count = *d / 8; 15321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PLTGOT: 15341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project /* Save this in case we decide to do lazy binding. We don't yet. */ 1535f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->plt_got = (unsigned *)(base + *d); 15361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_DEBUG: 1538d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if !defined(ANDROID_MIPS_LINKER) 1539bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes // Set the DT_DEBUG entry to the address of _r_debug for GDB 15401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *d = (int) &_r_debug; 1541d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif 15421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 1543ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI case DT_RELA: 1544124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("unsupported DT_RELA in \"%s\"", si->name); 1545124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 15461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_INIT: 1547f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->init_func = (void (*)(void))(base + *d); 154861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("%s constructors (init func) found at %p\n", si->name, si->init_func); 15491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_FINI: 1551f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->fini_func = (void (*)(void))(base + *d); 155261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("%s destructors (fini func) found at %p\n", si->name, si->fini_func); 15531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_INIT_ARRAY: 1555f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->init_array = (unsigned *)(base + *d); 155661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("%s constructors (init_array) found at %p\n", si->name, si->init_array); 15571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_INIT_ARRAYSZ: 15591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->init_array_count = ((unsigned)*d) / sizeof(Elf32_Addr); 15601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_FINI_ARRAY: 1562f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->fini_array = (unsigned *)(base + *d); 156361a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("%s destructors (fini_array) found at %p\n", si->name, si->fini_array); 15641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_FINI_ARRAYSZ: 15661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->fini_array_count = ((unsigned)*d) / sizeof(Elf32_Addr); 15671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PREINIT_ARRAY: 1569f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee si->preinit_array = (unsigned *)(base + *d); 157061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("%s constructors (preinit_array) found at %p\n", si->name, si->preinit_array); 15711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_PREINIT_ARRAYSZ: 15731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->preinit_array_count = ((unsigned)*d) / sizeof(Elf32_Addr); 15741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case DT_TEXTREL: 15765135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich si->has_text_relocations = true; 15771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 15785ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel case DT_SYMBOLIC: 15795ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel si->has_DT_SYMBOLIC = true; 15805ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel break; 15815ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel#if defined(DT_FLAGS) 15825ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel case DT_FLAGS: 15835ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel if (*d & DF_TEXTREL) { 15845ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel si->has_text_relocations = true; 15855ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 15865ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel if (*d & DF_SYMBOLIC) { 15875ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel si->has_DT_SYMBOLIC = true; 15885ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel } 15895ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel break; 15905ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel#endif 1591d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if defined(ANDROID_MIPS_LINKER) 1592d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_NEEDED: 1593d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_STRSZ: 1594d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_SYMENT: 1595d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_RELENT: 1596d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1597d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_RLD_MAP: 1598bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB. 1599d7daacb46372132ae3f0121647074936c304b572Raghu Gandham { 1600bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes r_debug** dp = (r_debug**) *d; 1601d7daacb46372132ae3f0121647074936c304b572Raghu Gandham *dp = &_r_debug; 1602d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1603d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1604d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_RLD_VERSION: 1605d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_FLAGS: 1606d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_BASE_ADDRESS: 1607d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_UNREFEXTNO: 1608d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_RWPLT: 1609d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1610d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1611d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_PLTGOT: 1612d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#if 0 1613d7daacb46372132ae3f0121647074936c304b572Raghu Gandham /* not yet... */ 1614d7daacb46372132ae3f0121647074936c304b572Raghu Gandham si->mips_pltgot = (unsigned *)(si->base + *d); 1615d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif 1616d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1617d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1618d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_SYMTABNO: 1619d7daacb46372132ae3f0121647074936c304b572Raghu Gandham si->mips_symtabno = *d; 1620d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1621d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1622d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_LOCAL_GOTNO: 1623d7daacb46372132ae3f0121647074936c304b572Raghu Gandham si->mips_local_gotno = *d; 1624d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1625d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1626d7daacb46372132ae3f0121647074936c304b572Raghu Gandham case DT_MIPS_GOTSYM: 1627d7daacb46372132ae3f0121647074936c304b572Raghu Gandham si->mips_gotsym = *d; 1628d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1629d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 1630d7daacb46372132ae3f0121647074936c304b572Raghu Gandham default: 163161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("Unused DT entry: type 0x%08x arg 0x%08x\n", d[-1], d[0]); 1632d7daacb46372132ae3f0121647074936c304b572Raghu Gandham break; 1633d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif 16341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 16351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 16361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 163761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("si->base = 0x%08x, si->strtab = %p, si->symtab = %p\n", 163861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes si->base, si->strtab, si->symtab); 16391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1640124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes // Sanity checks. 1641124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (si->nbucket == 0) { 1642124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name); 1643124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1644124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 1645124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (si->strtab == 0) { 1646124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name); 1647124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1648124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 1649124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (si->symtab == 0) { 1650124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name); 1651124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 16521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 16531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16544fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer /* if this is the main executable, then load all of the preloads now */ 1655d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (si->flags & FLAG_EXE) { 16564fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer memset(preloads, 0, sizeof(preloads)); 1657d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) { 1658d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes soinfo* lsi = find_library(gLdPreloadNames[i]); 1659d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (lsi == NULL) { 16604fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf)); 16614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s", 1662124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes gLdPreloadNames[i], si->name, tmp_err_buf); 1663124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 16644fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer } 16654fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer preloads[i] = lsi; 16664fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer } 16674fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer } 16684fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 166912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel /* dynamic_count is an upper bound for the number of needed libs */ 167012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel pneeded = needed = (soinfo**) alloca((1 + dynamic_count) * sizeof(soinfo*)); 167112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel 1672124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes for (unsigned* d = si->dynamic; *d; d += 2) { 1673d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (d[0] == DT_NEEDED) { 167461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("%s needs %s\n", si->name, si->strtab + d[1]); 1675d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes soinfo* lsi = find_library(si->strtab + d[1]); 1676d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes if (lsi == NULL) { 16770353195f344666256dba474a15c9ba22cf0cccc9Dima Zavin strlcpy(tmp_err_buf, linker_get_error(), sizeof(tmp_err_buf)); 16784688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s", 16794688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes si->strtab + d[1], si->name, tmp_err_buf); 1680124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 16811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 168212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel *pneeded++ = lsi; 16831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 16841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 168512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel *pneeded = NULL; 16861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 16875135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich if (si->has_text_relocations) { 16885135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich /* Unprotect the segments, i.e. make them writable, to allow 16895135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich * text relocations to work properly. We will later call 16905135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich * phdr_table_protect_segments() after all of them are applied 16915135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich * and all constructors are run. 16925135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich */ 16935135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) { 16945135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich DL_ERR("can't unprotect loadable segments for \"%s\": %s", 16955135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich si->name, strerror(errno)); 1696124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 16975135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich } 16985135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich } 16995135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich 1700124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (si->plt_rel) { 170161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("[ relocating %s plt ]\n", si->name ); 1702124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if(soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) { 1703124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1704124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 17051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 1706124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (si->rel) { 170761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("[ relocating %s ]\n", si->name ); 1708124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if(soinfo_relocate(si, si->rel, si->rel_count, needed)) { 1709124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1710124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes } 17111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1713d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#ifdef ANDROID_MIPS_LINKER 1714124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (mips_relocate_got(si, needed)) { 1715124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 1716d7daacb46372132ae3f0121647074936c304b572Raghu Gandham } 1717d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif 1718d7daacb46372132ae3f0121647074936c304b572Raghu Gandham 17191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->flags |= FLAG_LINKED; 172061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("[ finished linking %s ]\n", si->name); 17211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 17225135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich if (si->has_text_relocations) { 17235135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich /* All relocations are done, we can protect our segments back to 17245135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich * read-only. */ 17255135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) { 17265135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich DL_ERR("can't protect segments for \"%s\": %s", 17275135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich si->name, strerror(errno)); 1728124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 17295135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich } 17301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 17311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1732b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner /* We can also turn on GNU RELRO protection */ 1733b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) { 17344688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes DL_ERR("can't enable GNU RELRO protection for \"%s\": %s", 17354688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes si->name, strerror(errno)); 1736124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return false; 17379ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich } 17389ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich 1739124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes // If this is a setuid/setgid program, close the security hole described in 1740124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc 174118a206c81d9743481e364384affd43306911283dElliott Hughes if (get_AT_SECURE()) { 17424688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes nullify_closed_stdio(); 17434688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 17441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project notify_gdb_of_load(si); 1745124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes si->flags &= ~FLAG_ERROR; 1746124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes return true; 17471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 17481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 17494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void parse_path(const char* path, const char* delimiters, 17504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes const char** array, char* buf, size_t buf_size, size_t max_count) 1751bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley{ 17524688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (path == NULL) { 17534688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes return; 17544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 1755bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley 17564688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes size_t len = strlcpy(buf, path, buf_size); 1757bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley 17584688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes size_t i = 0; 17594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes char* buf_p = buf; 17604688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) { 17614688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (*array[i] != '\0') { 1762bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley ++i; 17634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes } 1764bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley } 1765bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley 17664688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes // Forget the last path if we had to truncate; this occurs if the 2nd to 17674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes // last char isn't '\0' (i.e. wasn't originally a delimiter). 17684688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') { 17694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes array[i - 1] = NULL; 1770bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley } else { 17714688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes array[i] = NULL; 1772bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley } 1773bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley} 1774bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley 17754688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void parse_LD_LIBRARY_PATH(const char* path) { 1776124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes parse_path(path, ":", gLdPaths, 1777124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX); 17784688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes} 17794fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 17804688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void parse_LD_PRELOAD(const char* path) { 17814688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes // We have historically supported ':' as well as ' ' in LD_PRELOAD. 1782124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes parse_path(path, " :", gLdPreloadNames, 1783124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX); 17844fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer} 17854fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 1786468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/* 1787468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and 1788468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs 1789468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point. 1790468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */ 1791bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissellstatic unsigned __linker_init_post_relocation(unsigned **elfdata, unsigned linker_base) 17921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{ 17931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project static soinfo linker_soinfo; 17941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 17951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project int argc = (int) *elfdata; 17961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project char **argv = (char**) (elfdata + 1); 1797bb44055d0a3515410f45417d5b0e047cc60f0dc6Stephen Smalley unsigned *vecs = (unsigned*) (argv + argc + 1); 17981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1799ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner /* NOTE: we store the elfdata pointer on a special location 1800ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner * of the temporary TLS area in order to pass it to 1801ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner * the C Library's runtime initializer. 1802ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner * 1803ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner * The initializer must clear the slot and reset the TLS 1804ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner * to point to a different location to ensure that no other 1805ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner * shared library constructor can access it. 1806ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner */ 18071a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov __libc_init_tls(elfdata); 18081a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov 18091a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING 18101a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov struct timeval t0, t1; 18111a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov gettimeofday(&t0, 0); 18121a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif 18131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 181418a206c81d9743481e364384affd43306911283dElliott Hughes // Initialize environment functions, and get to the ELF aux vectors table. 1815be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner vecs = linker_env_init(vecs); 1816be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner 18171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project debugger_init(); 18181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 181918a206c81d9743481e364384affd43306911283dElliott Hughes // Get a few environment variables. 182061a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes const char* LD_DEBUG = linker_env_get("LD_DEBUG"); 182161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes if (LD_DEBUG != NULL) { 182261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes debug_verbosity = atoi(LD_DEBUG); 182318a206c81d9743481e364384affd43306911283dElliott Hughes } 1824be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner 182518a206c81d9743481e364384affd43306911283dElliott Hughes // Normally, these are cleaned by linker_env_init, but the test 182618a206c81d9743481e364384affd43306911283dElliott Hughes // doesn't cost us anything. 182718a206c81d9743481e364384affd43306911283dElliott Hughes const char* ldpath_env = NULL; 182818a206c81d9743481e364384affd43306911283dElliott Hughes const char* ldpreload_env = NULL; 182918a206c81d9743481e364384affd43306911283dElliott Hughes if (!get_AT_SECURE()) { 183018a206c81d9743481e364384affd43306911283dElliott Hughes ldpath_env = linker_env_get("LD_LIBRARY_PATH"); 183118a206c81d9743481e364384affd43306911283dElliott Hughes ldpreload_env = linker_env_get("LD_PRELOAD"); 18321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 18331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 18341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project INFO("[ android linker & debugger ]\n"); 183561a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes DEBUG("elfdata @ 0x%08x\n", (unsigned)elfdata); 18361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 183718a206c81d9743481e364384affd43306911283dElliott Hughes soinfo* si = soinfo_alloc(argv[0]); 183818a206c81d9743481e364384affd43306911283dElliott Hughes if (si == NULL) { 183918a206c81d9743481e364384affd43306911283dElliott Hughes exit(EXIT_FAILURE); 18401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 18411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1842d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich /* bootstrap the link map, the main exe always needs to be first */ 18431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->flags |= FLAG_EXE; 1844bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes link_map* map = &(si->linkmap); 18451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 18461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_addr = 0; 18471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_name = argv[0]; 18481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_prev = NULL; 18491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project map->l_next = NULL; 18501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 18511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project _r_debug.r_map = map; 18521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project r_debug_tail = map; 18531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1854bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell /* gdb expects the linker to be in the debug shared object list. 1855bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell * Without this, gdb has trouble locating the linker's ".text" 1856bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell * and ".plt" sections. Gdb could also potentially use this to 1857bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell * relocate the offset of our exported 'rtld_db_dlactivity' symbol. 1858bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell * Don't use soinfo_alloc(), because the linker shouldn't 18591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * be on the soinfo list. 18601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */ 1861be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner strlcpy((char*) linker_soinfo.name, "/system/bin/linker", sizeof linker_soinfo.name); 18621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project linker_soinfo.flags = 0; 1863bb5c30a3ee40324effd4daa346505e16faf91728Ryan V. Bissell linker_soinfo.base = linker_base; 186406f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng /* 186506f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng * Set the dynamic field in the link map otherwise gdb will complain with 186606f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng * the following: 186706f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng * warning: .dynamic section for "/system/bin/linker" is not at the 186806f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng * expected address (wrong library or version mismatch?) 186906f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng */ 187006f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_base; 187106f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng Elf32_Phdr *phdr = 187206f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng (Elf32_Phdr *)((unsigned char *) linker_base + elf_hdr->e_phoff); 187312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base, 187412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel &linker_soinfo.dynamic, NULL); 18751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project insert_soinfo_into_debug_map(&linker_soinfo); 18761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1877d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich /* extract information passed from the kernel */ 187818a206c81d9743481e364384affd43306911283dElliott Hughes while (vecs[0] != 0){ 18791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project switch(vecs[0]){ 18801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case AT_PHDR: 18811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->phdr = (Elf32_Phdr*) vecs[1]; 18821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case AT_PHNUM: 18841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->phnum = (int) vecs[1]; 18851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project case AT_ENTRY: 18871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->entry = vecs[1]; 18881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project break; 18891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 18901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project vecs += 2; 18911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 18921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 18938180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner /* Compute the value of si->base. We can't rely on the fact that 18948180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner * the first entry is the PHDR because this will not be true 18958180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner * for certain executables (e.g. some in the NDK unit test suite) 18968180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner */ 18978180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner int nn; 18988180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner si->base = 0; 1899b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner si->size = phdr_table_get_load_size(si->phdr, si->phnum); 1900bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner si->load_bias = 0; 19018180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner for ( nn = 0; nn < si->phnum; nn++ ) { 19028180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner if (si->phdr[nn].p_type == PT_PHDR) { 1903bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner si->load_bias = (Elf32_Addr)si->phdr - si->phdr[nn].p_vaddr; 1904bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner si->base = (Elf32_Addr) si->phdr - si->phdr[nn].p_offset; 19058180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner break; 19068180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner } 19078180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner } 19081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project si->dynamic = (unsigned *)-1; 19096774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner si->refcount = 1; 19101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 19114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid). 19124688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes parse_LD_LIBRARY_PATH(ldpath_env); 19134688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes parse_LD_PRELOAD(ldpreload_env); 19144fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer 19155ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel somain = si; 19165ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel 1917124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (!soinfo_link_image(si)) { 19182e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin char errmsg[] = "CANNOT LINK EXECUTABLE\n"; 19192e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf)); 19202e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin write(2, errmsg, sizeof(errmsg)); 192118a206c81d9743481e364384affd43306911283dElliott Hughes exit(EXIT_FAILURE); 19221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 19231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 1924d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->CallPreInitConstructors(); 19259181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov 192618a206c81d9743481e364384affd43306911283dElliott Hughes for (size_t i = 0; preloads[i] != NULL; ++i) { 1927d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes preloads[i]->CallConstructors(); 1928326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng } 1929326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng 19309c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin /*After the link_image, the si->base is initialized. 19319c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin *For so lib, the map->l_addr will be updated in notify_gdb_of_load. 19329c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin *We need to update this value for so exe here. So Unwind_Backtrace 19339c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin *for some arch like x86 could work correctly within so exe. 19349c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin */ 19359c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin map->l_addr = si->base; 1936d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes si->CallConstructors(); 1937e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov 19381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING 19391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project gettimeofday(&t1,NULL); 19401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project PRINT("LINKER TIME: %s: %d microseconds\n", argv[0], (int) ( 19411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) - 19421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec) 19431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project )); 19441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 19451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS 19461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol\n", argv[0], 1947bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes linker_stats.count[kRelocAbsolute], 1948bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes linker_stats.count[kRelocRelative], 1949bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes linker_stats.count[kRelocCopy], 1950bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes linker_stats.count[kRelocSymbol]); 19511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 19521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES 19531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project { 19541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned n; 19551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned i; 19561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned count = 0; 19571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for(n = 0; n < 4096; n++){ 19581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(bitmask[n]){ 19591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project unsigned x = bitmask[n]; 19601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project for(i = 0; i < 8; i++){ 19611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project if(x & 1) count++; 19621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project x >>= 1; 19631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 19641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 19651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 19661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project PRINT("PAGES MODIFIED: %s: %d (%dKB)\n", argv[0], count, count * 4); 19671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project } 19681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 19691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 19701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES 19711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project fflush(stdout); 19721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif 19731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project 197461a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes TRACE("[ Ready to execute '%s' @ 0x%08x ]\n", si->name, si->entry); 19751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project return si->entry; 19761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project} 1977468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich 1978468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/* 1979468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Find the value of AT_BASE passed to us by the kernel. This is the load 1980468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * location of the linker. 1981468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */ 1982468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevichstatic unsigned find_linker_base(unsigned **elfdata) { 1983468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich int argc = (int) *elfdata; 1984468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich char **argv = (char**) (elfdata + 1); 1985468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich unsigned *vecs = (unsigned*) (argv + argc + 1); 1986468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich while (vecs[0] != 0) { 1987468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich vecs++; 1988468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich } 1989468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich 1990468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich /* The end of the environment block is marked by two NULL pointers */ 1991468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich vecs++; 1992468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich 1993468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich while(vecs[0]) { 1994468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich if (vecs[0] == AT_BASE) { 1995468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich return vecs[1]; 1996468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich } 1997468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich vecs += 2; 1998468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich } 1999468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich 2000468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich return 0; // should never happen 2001468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich} 2002468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich 2003bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only 2004bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library 2005bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself. 2006bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * 2007bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input: 2008bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * elf -> address of ELF header, assumed to be at the start of the file. 2009bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return: 2010bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * load bias, i.e. add the value of any p_vaddr in the file to get 2011bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * the corresponding address in memory. 2012bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */ 2013bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turnerstatic Elf32_Addr 2014bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turnerget_elf_exec_load_bias(const Elf32_Ehdr* elf) 2015bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner{ 2016bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner Elf32_Addr offset = elf->e_phoff; 2017bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner const Elf32_Phdr* phdr_table = (const Elf32_Phdr*)((char*)elf + offset); 2018bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner const Elf32_Phdr* phdr_end = phdr_table + elf->e_phnum; 2019bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner const Elf32_Phdr* phdr; 2020bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner 2021bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner for (phdr = phdr_table; phdr < phdr_end; phdr++) { 2022bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner if (phdr->p_type == PT_LOAD) { 2023bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner return (Elf32_Addr)elf + phdr->p_offset - phdr->p_vaddr; 2024bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner } 2025bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner } 2026bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner return 0; 2027bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner} 2028bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner 2029468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/* 2030468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This 2031468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and 2032468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation(). 2033468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * 2034468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own 2035468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern 2036468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault. 2037468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */ 20384688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesextern "C" unsigned __linker_init(unsigned **elfdata) { 2039468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich unsigned linker_addr = find_linker_base(elfdata); 2040468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich Elf32_Ehdr *elf_hdr = (Elf32_Ehdr *) linker_addr; 2041468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich Elf32_Phdr *phdr = 2042468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich (Elf32_Phdr *)((unsigned char *) linker_addr + elf_hdr->e_phoff); 2043468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich 2044468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich soinfo linker_so; 2045468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich memset(&linker_so, 0, sizeof(soinfo)); 2046468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich 2047468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich linker_so.base = linker_addr; 2048b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum); 2049bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner linker_so.load_bias = get_elf_exec_load_bias(elf_hdr); 2050468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich linker_so.dynamic = (unsigned *) -1; 2051468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich linker_so.phdr = phdr; 2052468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich linker_so.phnum = elf_hdr->e_phnum; 2053468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich linker_so.flags |= FLAG_LINKER; 2054468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich 2055124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes if (!soinfo_link_image(&linker_so)) { 2056468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich // It would be nice to print an error message, but if the linker 2057468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich // can't link itself, there's no guarantee that we'll be able to 2058468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich // call write() (because it involves a GOT reference). 2059468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich // 2060468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich // This situation should never occur unless the linker itself 2061468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich // is corrupt. 206218a206c81d9743481e364384affd43306911283dElliott Hughes exit(EXIT_FAILURE); 2063468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich } 2064468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich 2065468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich // We have successfully fixed our own relocations. It's safe to run 2066468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich // the main part of the linker now. 20675419b9474753d25dff947c7740532f86d130c0beElliott Hughes unsigned start_address = __linker_init_post_relocation(elfdata, linker_addr); 20685419b9474753d25dff947c7740532f86d130c0beElliott Hughes 2069d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes set_soinfo_pool_protection(PROT_READ); 2070d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes 20715419b9474753d25dff947c7740532f86d130c0beElliott Hughes // Return the address that the calling assembly stub should jump to. 20725419b9474753d25dff947c7740532f86d130c0beElliott Hughes return start_address; 2073468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich} 2074