linker.cpp revision ebd506c69e12b6dcaf5be94cc8ed1b53af299f9f
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.
43eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/bionic_tls.h"
44eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/KernelArgumentBlock.h"
45eb847bc8666842a3cfc9c06e8458ad1abebebaf0Elliott Hughes#include "private/ScopedPthreadMutexLocker.h"
461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker.h"
481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#include "linker_debug.h"
49be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner#include "linker_environ.h"
5023363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner#include "linker_phdr.h"
511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
52bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley/* Assume average path length of 64 and max 8 paths */
53bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley#define LDPATH_BUFSIZE 512
54bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley#define LDPATH_MAX 8
55bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
564fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer#define LDPRELOAD_BUFSIZE 512
574fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer#define LDPRELOAD_MAX 8
584fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* >>> IMPORTANT NOTE - READ ME BEFORE MODIFYING <<<
601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Do NOT use malloc() and friends or pthread_*() code here.
621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Don't use printf() either; it's caused mysterious memory
631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * corruption in the past.
641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * The linker runs before we bring up libc and it's easiest
651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * to make sure it does not depend on any complex libc features
661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * open issues / todo:
681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - are we doing everything we should for ARM_COPY relocations?
701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - cleaner error reporting
711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * - after linking, set as much stuff as possible to READONLY
721dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *   and NOEXEC
734688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes */
741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
75124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic bool soinfo_link_image(soinfo* si);
76ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikovstatic Elf_Addr get_elf_exec_load_bias(const Elf_Ehdr* elf);
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
89d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* solist = &libdl_info;
90d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* sonext = &libdl_info;
91d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* somain; /* main process, always the one after libdl_info */
921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
93124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* const gSoPaths[] = {
944eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__LP64__)
95011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes  "/vendor/lib64",
96011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes  "/system/lib64",
97011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes#else
98124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  "/vendor/lib",
99124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  "/system/lib",
100011bc0ba45a8b7766a205cb21269dbafb32294b6Elliott Hughes#endif
101124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  NULL
102124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes};
103124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes
104124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic char gLdPathsBuffer[LDPATH_BUFSIZE];
105124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* gLdPaths[LDPATH_MAX + 1];
106bc3a5c26f1b9cf29da6abfc3e197258ef4c03362David Bartley
107124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic char gLdPreloadsBuffer[LDPRELOAD_BUFSIZE];
108124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic const char* gLdPreloadNames[LDPRELOAD_MAX + 1];
1094fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
110d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic soinfo* gLdPreloads[LDPRELOAD_MAX + 1];
1114fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
112650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes__LIBC_HIDDEN__ int gLdDebugVerbosity;
1131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1140d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes__LIBC_HIDDEN__ abort_msg_t* gAbortMessage = NULL; // For debuggerd.
1150d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes
116bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesenum RelocationKind {
117bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocAbsolute = 0,
118bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocRelative,
119bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocCopy,
120bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocSymbol,
121bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    kRelocMax
122bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
123be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
1241dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
125bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstruct linker_stats_t {
126bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    int count[kRelocMax];
127bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes};
128bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
129bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic linker_stats_t linker_stats;
130bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes
131bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind kind) {
132bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    ++linker_stats.count[kind];
133bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
134bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
135bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void count_relocation(RelocationKind) {
136bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes}
1371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
140bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic unsigned bitmask[4096];
141bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(offset) \
142bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    do { \
143bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        bitmask[((offset) >> 12) >> 3] |= (1 << (((offset) >> 12) & 7)); \
144bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    } while(0)
145bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#else
146bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes#define MARK(x) do {} while (0)
1471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
1481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1494688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// You shouldn't try to call memory-allocating functions in the dynamic linker.
1504688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes// Guard against the most obvious ones.
1518f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes#define DISALLOW_ALLOCATION(return_type, name, ...) \
1528f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    return_type name __VA_ARGS__ \
1538f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes    { \
1544688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        const char* msg = "ERROR: " #name " called from the dynamic linker!\n"; \
1558f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        __libc_format_log(ANDROID_LOG_FATAL, "linker", "%s", msg); \
1568f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        write(2, msg, strlen(msg)); \
1578f2a5a0b40fc82126c691d5c30131d908772aab7Elliott Hughes        abort(); \
1584688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
1594688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes#define UNUSED __attribute__((unused))
1604688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, malloc, (size_t u UNUSED));
1614688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void, free, (void* u UNUSED));
1624688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, realloc, (void* u1 UNUSED, size_t u2 UNUSED));
1634688279db5dcc4004941e7f133c4a1c3617d842cElliott HughesDISALLOW_ALLOCATION(void*, calloc, (size_t u1 UNUSED, size_t u2 UNUSED));
1642e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
1650353195f344666256dba474a15c9ba22cf0cccc9Dima Zavinstatic char tmp_err_buf[768];
1662e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavinstatic char __linker_dl_err_buf[768];
1672e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
168650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hugheschar* linker_get_error_buffer() {
1695419b9474753d25dff947c7740532f86d130c0beElliott Hughes  return &__linker_dl_err_buf[0];
1702e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin}
1712e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin
172650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughessize_t linker_get_error_buffer_size() {
173650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes  return sizeof(__linker_dl_err_buf);
174650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes}
175650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes
1761dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/*
1771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * This function is an empty stub where GDB locates a breakpoint to get notified
1781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * about linker activity.
1791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
1805419b9474753d25dff947c7740532f86d130c0beElliott Hughesextern "C" void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
1811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1820d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughesstatic r_debug _r_debug = {1, NULL, &rtld_db_dlactivity, RT_CONSISTENT, 0};
183ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughesstatic link_map_t* r_debug_tail = 0;
1841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1853b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughesstatic pthread_mutex_t gDebugMutex = PTHREAD_MUTEX_INITIALIZER;
1861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
187bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void insert_soinfo_into_debug_map(soinfo * info) {
188bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    // Copy the necessary fields into the debug structure.
189ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    link_map_t* map = &(info->link_map);
190ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov    map->l_addr = info->load_bias;
1911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_name = (char*) info->name;
1925cf640c926e7d7b79934c97226433a3dc448e1ebThinker K.F Li    map->l_ld = (uintptr_t)info->dynamic;
1931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* Stick the new library at the end of the list.
1951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * gdb tends to care more about libc than it does
1961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * about leaf libraries, and ordering it this way
1971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     * reduces the back-and-forth over the wire.
1981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project     */
1991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (r_debug_tail) {
2001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        r_debug_tail->l_next = map;
2011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = r_debug_tail;
2021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    } else {
2041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        _r_debug.r_map = map;
2051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_prev = 0;
2061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        map->l_next = 0;
2071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
2091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
211bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void remove_soinfo_from_debug_map(soinfo* info) {
212ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    link_map_t* map = &(info->link_map);
2135e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
214bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (r_debug_tail == map) {
2155e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        r_debug_tail = map->l_prev;
216bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2175e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
218bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_prev) {
219bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_prev->l_next = map->l_next;
220bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
221bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    if (map->l_next) {
222bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        map->l_next->l_prev = map->l_prev;
223bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    }
2245e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2255e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
226bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_load(soinfo* info) {
2271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (info->flags & FLAG_EXE) {
2281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        // GDB already knows about the main executable
2291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
2301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
2311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2323b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes    ScopedPthreadMutexLocker locker(&gDebugMutex);
2331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_ADD;
2351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    insert_soinfo_into_debug_map(info);
2381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
2391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_CONSISTENT;
2401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2415e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev}
2425e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
243bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesstatic void notify_gdb_of_unload(soinfo* info) {
2445e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    if (info->flags & FLAG_EXE) {
2455e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        // GDB already knows about the main executable
2465e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev        return;
2475e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    }
2485e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2493b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes    ScopedPthreadMutexLocker locker(&gDebugMutex);
2505e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2515e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    _r_debug.r_state = RT_DELETE;
2525e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2535e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2545e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    remove_soinfo_from_debug_map(info);
2555e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev
2565e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    _r_debug.r_state = RT_CONSISTENT;
2575e12d7ec70472659623d2e4552a670d8877f2423Iliyan Malchev    rtld_db_dlactivity();
2581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
26018a206c81d9743481e364384affd43306911283dElliott Hughesvoid notify_gdb_of_libraries() {
2611dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_ADD;
2621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_state = RT_CONSISTENT;
2641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    rtld_db_dlactivity();
2651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
2661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
267ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic bool ensure_free_list_non_empty() {
268ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (gSoInfoFreeList != NULL) {
269ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return true;
270ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
271ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
272ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Allocate a new pool.
273ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo_pool_t* pool = reinterpret_cast<soinfo_pool_t*>(mmap(NULL, sizeof(*pool),
274ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn                                                              PROT_READ|PROT_WRITE,
275ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn                                                              MAP_PRIVATE|MAP_ANONYMOUS, 0, 0));
276ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (pool == MAP_FAILED) {
277ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return false;
278ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
279ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
280ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Add the pool to our list of pools.
281ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  pool->next = gSoInfoPools;
282ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  gSoInfoPools = pool;
283ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
284ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Chain the entries in the new pool onto the free list.
285ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  gSoInfoFreeList = &pool->info[0];
286ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo* next = NULL;
287ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  for (int i = SOINFO_PER_POOL - 1; i >= 0; --i) {
288ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    pool->info[i].next = next;
289ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    next = &pool->info[i];
290ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
291ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
292ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  return true;
293ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn}
2941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
295d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic void set_soinfo_pool_protection(int protection) {
296d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  for (soinfo_pool_t* p = gSoInfoPools; p != NULL; p = p->next) {
297d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (mprotect(p, sizeof(*p), protection) == -1) {
298d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      abort(); // Can't happen.
299d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    }
300d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
301d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
302d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
303ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmbornstatic soinfo* soinfo_alloc(const char* name) {
304ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (strlen(name) >= SOINFO_NAME_LEN) {
305ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    DL_ERR("library name \"%s\" too long", name);
306ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return NULL;
307ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
308ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn
309ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  if (!ensure_free_list_non_empty()) {
310ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    DL_ERR("out of memory when loading \"%s\"", name);
311ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    return NULL;
312ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  }
3131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
314ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Take the head element off the free list.
315ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  soinfo* si = gSoInfoFreeList;
316ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  gSoInfoFreeList = gSoInfoFreeList->next;
3171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
318ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  // Initialize the new element.
319ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  memset(si, 0, sizeof(soinfo));
320ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  strlcpy(si->name, name, sizeof(si->name));
321ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext->next = si;
322ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  sonext = si;
3231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
324ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("name %s: allocated soinfo @ %p", name, si);
325ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn  return si;
3261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
3284688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughesstatic void soinfo_free(soinfo* si)
3291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
3304688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    if (si == NULL) {
3314688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return;
3324688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    }
3334688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *prev = NULL, *trav;
3351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
336ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("name %s: freeing soinfo @ %p", si->name, si);
3371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
338d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (trav = solist; trav != NULL; trav = trav->next) {
3391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (trav == si)
3401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
3411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        prev = trav;
3421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (trav == NULL) {
344d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        /* si was not in solist */
3454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("name \"%s\" is not in solist!", si->name);
3461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return;
3471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
3481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
349be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner    /* prev will never be NULL, because the first entry in solist is
3501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       always the static libdl_info.
3511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    */
3521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    prev->next = si->next;
353d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si == sonext) {
354d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        sonext = prev;
355d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
356ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    si->next = gSoInfoFreeList;
357ba98d9237b0eabc1d8caf2600fd787b988645249Magnus Malmborn    gSoInfoFreeList = si;
3581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
3591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
360cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
361cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_path(const char* path, const char* delimiters,
362cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes                       const char** array, char* buf, size_t buf_size, size_t max_count) {
363cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (path == NULL) {
364cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return;
365cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
366cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
367cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t len = strlcpy(buf, path, buf_size);
368cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
369cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  size_t i = 0;
370cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  char* buf_p = buf;
371cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  while (i < max_count && (array[i] = strsep(&buf_p, delimiters))) {
372cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (*array[i] != '\0') {
373cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      ++i;
374cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    }
375cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
376cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
377cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // Forget the last path if we had to truncate; this occurs if the 2nd to
378cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // last char isn't '\0' (i.e. wasn't originally a delimiter).
379cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (i > 0 && len >= buf_size && buf[buf_size - 2] != '\0') {
380cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    array[i - 1] = NULL;
381cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  } else {
382cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    array[i] = NULL;
383cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
384cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
385cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
386cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_LIBRARY_PATH(const char* path) {
387cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  parse_path(path, ":", gLdPaths,
388cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes             gLdPathsBuffer, sizeof(gLdPathsBuffer), LDPATH_MAX);
389cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
390cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
391cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesstatic void parse_LD_PRELOAD(const char* path) {
392cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // We have historically supported ':' as well as ' ' in LD_PRELOAD.
393cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  parse_path(path, " :", gLdPreloadNames,
394cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes             gLdPreloadsBuffer, sizeof(gLdPreloadsBuffer), LDPRELOAD_MAX);
395cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
396cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
3974eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
3984688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
3991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* For a given PC, find the .so that it belongs to.
4001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Returns the base address of the .ARM.exidx section
4011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * for that .so, and the number of 8-byte entries
4021dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * in that section (via *pcount).
4031dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * Intended to be called by libc's __gnu_Unwind_Find_exidx().
4051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project *
4063b297c40794b23d50cb5240f9b03f6ef25fd98dbElliott Hughes * This function is exposed via dlfcn.cpp and libdl.so.
4071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project */
4081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int *pcount)
4091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
4111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned addr = (unsigned)pc;
4121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
413c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    for (si = solist; si != 0; si = si->next) {
414468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich        if ((addr >= si->base) && (addr < (si->base + si->size))) {
415468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich            *pcount = si->ARM_exidx_count;
416f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee            return (_Unwind_Ptr)si->ARM_exidx;
4171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
4181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   *pcount = 0;
4201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return NULL;
4211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4224688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
42324053a461e7a20f34002262c1bb122023134989dChristopher Ferris#endif
4244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
4251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Here, we only have to provide a callback to iterate across all the
4261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project * loaded libraries. gcc_eh does the rest. */
4271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Projectint
428bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughesdl_iterate_phdr(int (*cb)(dl_phdr_info *info, size_t size, void *data),
4291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                void *data)
4301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
4311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int rv = 0;
432bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes    for (soinfo* si = solist; si != NULL; si = si->next) {
433bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        dl_phdr_info dl_info;
434ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        dl_info.dlpi_addr = si->link_map.l_addr;
435ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        dl_info.dlpi_name = si->link_map.l_name;
4361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phdr = si->phdr;
4371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        dl_info.dlpi_phnum = si->phnum;
438bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        rv = cb(&dl_info, sizeof(dl_phdr_info), data);
439bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        if (rv != 0) {
4401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
441bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes        }
4421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return rv;
4441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4454688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes
446c620059479c47a78d57086d73726c9adc2f337adElliott Hughesstatic Elf_Sym* soinfo_elf_lookup(soinfo* si, unsigned hash, const char* name) {
447c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    Elf_Sym* symtab = si->symtab;
448d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    const char* strtab = si->strtab;
4491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
450c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p %x %zd",
451c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes               name, si->name, reinterpret_cast<void*>(si->base), hash, hash % si->nbucket);
4521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4536bec5b792a5f034089a942cb28cab26d36408235Christopher Ferris    for (unsigned n = si->bucket[hash % si->nbucket]; n != 0; n = si->chain[n]) {
454c620059479c47a78d57086d73726c9adc2f337adElliott Hughes        Elf_Sym* s = symtab + n;
455d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (strcmp(strtab + s->st_name, name)) continue;
4561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
457e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            /* only concern ourselves with global and weak symbol definitions */
458c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (ELF_ST_BIND(s->st_info)) {
4591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case STB_GLOBAL:
460e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan        case STB_WEAK:
461d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (s->st_shndx == SHN_UNDEF) {
462439fa8ea8c33a7945258dfe84a71ab2665efaa9aRobin Burchell                continue;
463d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            }
4641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
465c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
466c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                       name, si->name, reinterpret_cast<void*>(s->st_value),
467c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                       static_cast<size_t>(s->st_size));
4681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return s;
4691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
4701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
472943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
4731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
475d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic unsigned elfhash(const char* _name) {
476d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    const unsigned char* name = (const unsigned char*) _name;
4771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    unsigned h = 0, g;
4781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
4791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    while(*name) {
4801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h = (h << 4) + *name++;
4811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        g = h & 0xf0000000;
4821dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g;
4831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        h ^= g >> 24;
4841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
4851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return h;
4861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
4871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
488c620059479c47a78d57086d73726c9adc2f337adElliott Hughesstatic Elf_Sym* soinfo_do_lookup(soinfo* si, const char* name, soinfo** lsi, soinfo* needed[]) {
489943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    unsigned elf_hash = elfhash(name);
490c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    Elf_Sym* s = NULL;
4916ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
492c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin    if (si != NULL && somain != NULL) {
4935ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
4945ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        /*
495c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * Local scope is executable scope. Just start looking into it right away
496c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin         * for the shortcut.
4975ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel         */
4985ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
499c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        if (si == somain) {
500c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
5015ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            if (s != NULL) {
502c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
5035ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                goto done;
5045ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
505c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin        } else {
506c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Order of symbol lookup is controlled by DT_SYMBOLIC flag */
507c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
508c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
509c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with symbolic relocations disabled, the
510c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * first place to look to resolve external references is the main
511c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * executable.
512c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
513c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
514c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (!si->has_DT_SYMBOLIC) {
515ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s",
51661a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
517c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
518c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
519c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
520c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
521c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
522c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5235ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
524c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /* Look for symbols in the local scope (the object who is
5254eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes             * searching). This happens with C++ templates on x86 for some
526c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * reason.
527c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             *
528c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Notes on weak symbols:
529c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * The ELF specs are ambiguous about treatment of weak definitions in
530c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * dynamic linking.  Some systems return the first definition found
531c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * and some the first non-weak definition.   This is system dependent.
532c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * Here we return the first definition found for simplicity.  */
533c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
534c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            s = soinfo_elf_lookup(si, elf_hash, name);
535c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (s != NULL) {
536c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                *lsi = si;
537c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                goto done;
538c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
539c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
540c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            /*
541c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * If this object was built with -Bsymbolic and symbol is not found
542c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             * in the local scope, try to find the symbol in the main executable.
543c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin             */
544c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin
545c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            if (si->has_DT_SYMBOLIC) {
546ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes                DEBUG("%s: looking up %s in executable %s after local scope",
54761a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes                      si->name, name, somain->name);
548c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                s = soinfo_elf_lookup(somain, elf_hash, name);
549c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                if (s != NULL) {
550c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    *lsi = somain;
551c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                    goto done;
552c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin                }
553c77c434149959e135ba21d1dd8a78a408fef2489Pavel Chupin            }
5545ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
555d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    }
5566ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5574fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    /* Next, look for it in the preloads list */
558d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (int i = 0; gLdPreloads[i] != NULL; i++) {
559d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        s = soinfo_elf_lookup(gLdPreloads[i], elf_hash, name);
560d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (s != NULL) {
561d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            *lsi = gLdPreloads[i];
5624fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            goto done;
5635ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5644fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
5654fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
566d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (int i = 0; needed[i] != NULL; i++) {
567ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        DEBUG("%s: looking up %s in %s",
56861a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes              si->name, name, needed[i]->name);
5695ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_elf_lookup(needed[i], elf_hash, name);
5705ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        if (s != NULL) {
5715ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            *lsi = needed[i];
57212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            goto done;
5735ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        }
5746ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5756ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
5766ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchevdone:
577d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (s != NULL) {
578c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
579c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   "found in %s, base = %p, load bias = %p",
580c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   si->name, name, reinterpret_cast<void*>(s->st_value),
581c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   (*lsi)->name, reinterpret_cast<void*>((*lsi)->base),
582c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                   reinterpret_cast<void*>((*lsi)->load_bias));
5836ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev        return s;
5846ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev    }
5856ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
586943043583a8f3a8de34970b550a3e8e8a6fb0fb8Doug Kwan    return NULL;
5876ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev}
5886ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev
589d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3).  It performs symbol lookup only within the
5906ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev   specified soinfo object and not in any of its dependencies.
591d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom
592d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   TODO: Only looking in the specified soinfo seems wrong. dlsym(3) says
593d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   that it should do a breadth first search through the dependency
594d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   tree. This agrees with the ELF spec (aka System V Application
595d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   Binary Interface) where in Chapter 5 it discuss resolving "Shared
596d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   Object Dependencies" in breadth first search order.
5976ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
598c620059479c47a78d57086d73726c9adc2f337adElliott HughesElf_Sym* dlsym_handle_lookup(soinfo* si, const char* name) {
59916084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    return soinfo_elf_lookup(si, elfhash(name), name);
6001dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6011dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
602d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom/* This is used by dlsym(3) to performs a global symbol lookup. If the
603d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   start value is null (for RTLD_DEFAULT), the search starts at the
604d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   beginning of the global solist. Otherwise the search starts at the
605d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom   specified soinfo (for RTLD_NEXT).
6066ed80c8814db9eed1fb687be22322e38dc46a2fbIliyan Malchev */
607c620059479c47a78d57086d73726c9adc2f337adElliott HughesElf_Sym* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start) {
608cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  unsigned elf_hash = elfhash(name);
6091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
610cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (start == NULL) {
611cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    start = solist;
612cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6131698d9ebfc7e27271852a1fdf305a2ac37b3ebe4Matt Fischer
614c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  Elf_Sym* s = NULL;
615cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  for (soinfo* si = start; (s == NULL) && (si != NULL); si = si->next) {
616cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    s = soinfo_elf_lookup(si, elf_hash, name);
617cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    if (s != NULL) {
618cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      *found = si;
619cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes      break;
6201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
621cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
623cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (s != NULL) {
624c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
625c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes               name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
626cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
6271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
628cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  return s;
6291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
631fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Chengsoinfo* find_containing_library(const void* p) {
632c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  Elf_Addr address = reinterpret_cast<Elf_Addr>(p);
633fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (soinfo* si = solist; si != NULL; si = si->next) {
634fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (address >= si->base && address - si->base < si->size) {
635fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return si;
636e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
637fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
638fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return NULL;
639e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
640e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
641c620059479c47a78d57086d73726c9adc2f337adElliott HughesElf_Sym* dladdr_find_symbol(soinfo* si, const void* addr) {
642c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  Elf_Addr soaddr = reinterpret_cast<Elf_Addr>(addr) - si->base;
643fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
644fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // Search the library's symbol table for any defined symbol which
645fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  // contains this address.
646fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  for (size_t i = 0; i < si->nchain; ++i) {
647c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    Elf_Sym* sym = &si->symtab[i];
648fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (sym->st_shndx != SHN_UNDEF &&
649fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr >= sym->st_value &&
650fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng        soaddr < sym->st_value + sym->st_size) {
651fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng      return sym;
652e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer    }
653fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
654e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
655fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return NULL;
656e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer}
657e2a8b1fd19fb3a8ead2ba28ddba27be19fa978b5Matt Fischer
6581dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if 0
659d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstromstatic void dump(soinfo* si)
6601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
661c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    Elf_Sym* s = si->symtab;
662d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (unsigned n = 0; n < si->nchain; n++) {
663ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("%04d> %08x: %02x %04x %08x %08x %s", n, s,
6641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               s->st_info, s->st_shndx, s->st_value, s->st_size,
6651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               si->strtab + s->st_name);
6661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        s++;
6671dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
6681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
6701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
671124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library_on_path(const char* name, const char* const paths[]) {
672124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  char buf[512];
673124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  for (size_t i = 0; paths[i] != NULL; ++i) {
6741e980b6bc8315d00a07312b25486531247abd98cElliott Hughes    int n = __libc_format_buffer(buf, sizeof(buf), "%s/%s", paths[i], name);
675124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (n < 0 || n >= static_cast<int>(sizeof(buf))) {
676ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes      PRINT("Warning: ignoring very long library path: %s/%s", paths[i], name);
677124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      continue;
6781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
679124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
680124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (fd != -1) {
681124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes      return fd;
682124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
683124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
684124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return -1;
6851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
6861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
687124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic int open_library(const char* name) {
688ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ opening %s ]", name);
6891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
690124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // If the name contains a slash, we should attempt to open it directly and not search the paths.
691124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (strchr(name, '/') != NULL) {
6926971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
6936971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    if (fd != -1) {
6946971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes      return fd;
6956971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    }
6966971fe4ca52ebdaa85ba676a044412b01d2ef1bfElliott Hughes    // ...but nvidia binary blobs (at least) rely on this behavior, so fall through for now.
697124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
6981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
699124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  // Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
700124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  int fd = open_library_on_path(name, gLdPaths);
701124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  if (fd == -1) {
702124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    fd = open_library_on_path(name, gSoPaths);
703124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  }
704124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes  return fd;
7051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
707124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic soinfo* load_library(const char* name) {
7084688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Open the file.
709650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    int fd = open_library(name);
710650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    if (fd == -1) {
7114688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("library \"%s\" not found", name);
7121dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return NULL;
7132e85579c34047c305caf15fb0ebe02bf3d001d0eDima Zavin    }
7141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
715650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    // Read the ELF header and load the segments.
716650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    ElfReader elf_reader(name, fd);
717650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    if (!elf_reader.Load()) {
7184688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
71923363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
72023363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner
721650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    const char* bname = strrchr(name, '/');
722650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    soinfo* si = soinfo_alloc(bname ? bname + 1 : name);
723650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    if (si == NULL) {
7244688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        return NULL;
72523363ed7503c25ef4024ce0d517f7415c096645dDavid 'Digit' Turner    }
726650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->base = elf_reader.load_start();
727650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->size = elf_reader.load_size();
728650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->load_bias = elf_reader.load_bias();
729650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->flags = 0;
730650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->entry = 0;
731650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->dynamic = NULL;
732650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phnum = elf_reader.phdr_count();
733650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    si->phdr = elf_reader.loaded_phdr();
734650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes    return si;
7351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
7361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
73712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvelstatic soinfo *find_loaded_library(const char *name)
7381dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
7391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    soinfo *si;
7406774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner    const char *bname;
7416774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
74212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // TODO: don't use basename only for determining libraries
74312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    // http://code.google.com/p/android/issues/detail?id=6670
74412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
74512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    bname = strrchr(name, '/');
74612c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    bname = bname ? bname + 1 : name;
74712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
748d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (si = solist; si != NULL; si = si->next) {
749d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (!strcmp(bname, si->name)) {
75012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            return si;
75112c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel        }
75212c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    }
75312c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    return NULL;
75412c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel}
75512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
756d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic soinfo* find_library_internal(const char* name) {
757d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (name == NULL) {
758d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return somain;
759d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
760d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
761d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_loaded_library(name);
762d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
763d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_LINKED) {
764d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      return si;
765d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    }
766d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    DL_ERR("OOPS: recursive link to \"%s\"", si->name);
767d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return NULL;
768d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
76912c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
770ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ '%s' has not been loaded yet.  Locating...]", name);
771d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  si = load_library(name);
772cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (si == NULL) {
773cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return NULL;
774cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
775cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
776cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // At this point we know that whatever is loaded @ base is a valid ELF
777cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  // shared library whose segments are properly mapped in.
778c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  TRACE("[ init_library base=%p sz=0x%08x name='%s' ]",
779c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        reinterpret_cast<void*>(si->base), si->size, si->name);
780cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
781cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (!soinfo_link_image(si)) {
782cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    munmap(reinterpret_cast<void*>(si->base), si->size);
783cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    soinfo_free(si);
784cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    return NULL;
785d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
7866774809b6249d9d4efd982b6e3ca377c84482d9aDavid 'Digit' Turner
787d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
788d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
789d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
790d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic soinfo* find_library(const char* name) {
791d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_library_internal(name);
792d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
793ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count++;
794d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
795d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
796d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
797d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
798d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesstatic int soinfo_unload(soinfo* si) {
799ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  if (si->ref_count == 1) {
800ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("unloading '%s'", si->name);
801d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallDestructors();
802d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
803c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
804d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom      if (d->d_tag == DT_NEEDED) {
805d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        const char* library_name = si->strtab + d->d_un.d_val;
8068147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes        TRACE("%s needs to unload %s", si->name, library_name);
8078147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes        soinfo_unload(find_loaded_library(library_name));
808d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
8091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
8101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
811d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    munmap(reinterpret_cast<void*>(si->base), si->size);
812d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    notify_gdb_of_unload(si);
813d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    soinfo_free(si);
814ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 0;
815d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  } else {
816ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count--;
817c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    TRACE("not unloading '%s', decrementing ref_count to %zd", si->name, si->ref_count);
818d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
819d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return 0;
8201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
822cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughesvoid do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
823cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  if (!get_AT_SECURE()) {
824cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes    parse_LD_LIBRARY_PATH(ld_library_path);
825cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes  }
826cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes}
827cade4c36e7c9c62db3f476a0f9cfc329bac9acb7Elliott Hughes
828e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughessoinfo* do_dlopen(const char* name, int flags) {
829e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes  if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL)) != 0) {
830e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    DL_ERR("invalid flags to dlopen: %x", flags);
831e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes    return NULL;
832e66190d2a97a713ae4b4786e60ca3d67ab8aa192Elliott Hughes  }
833d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
834d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  soinfo* si = find_library(name);
835d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (si != NULL) {
836d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
837d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
838d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ);
839d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return si;
840d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
8411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
842d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesint do_dlclose(soinfo* si) {
843d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
844d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  int result = soinfo_unload(si);
845d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  set_soinfo_pool_protection(PROT_READ);
846d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  return result;
8471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
8481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
8494eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
850c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughesstatic int soinfo_relocate_a(soinfo* si, Elf_Rela* rela, unsigned count, soinfo* needed[]) {
851c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  Elf_Sym* symtab = si->symtab;
852c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  const char* strtab = si->strtab;
853c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  Elf_Sym* s;
854c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  Elf_Rela* start = rela;
855c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  soinfo* lsi;
856c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
857c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  for (size_t idx = 0; idx < count; ++idx, ++rela) {
858c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    unsigned type = ELF_R_TYPE(rela->r_info);
859c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    unsigned sym = ELF_R_SYM(rela->r_info);
860c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    Elf_Addr reloc = static_cast<Elf_Addr>(rela->r_offset + si->load_bias);
861c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    Elf_Addr sym_addr = 0;
862c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    char* sym_name = NULL;
863c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
864c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
865c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (type == 0) { // R_*_NONE
866c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      continue;
867c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
868c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (sym != 0) {
869c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      sym_name = (char *)(strtab + symtab[sym].st_name);
870c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      s = soinfo_do_lookup(si, sym_name, &lsi, needed);
871c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      if (s == NULL) {
872c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        // We only allow an undefined symbol if this is a weak reference...
873c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        s = &symtab[sym];
874c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
875c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
876c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          return -1;
877c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
878c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
879c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        /* IHI0044C AAELF 4.5.1.1:
880c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
881c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           Libraries are not searched to resolve weak references.
882c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           It is not an error for a weak reference to remain unsatisfied.
883c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
884c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           During linking, the value of an undefined weak reference is:
885c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - Zero if the relocation type is absolute
886c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - The address of the place if the relocation is pc-relative
887c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes           - The address of nominal base address if the relocation
888c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes             type is base-relative.
889c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         */
890c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
891c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (type) {
8924eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__x86_64__)
893c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_JUMP_SLOT:
894c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_GLOB_DAT:
895c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_32:
896c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_RELATIVE:
897c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          // No need to do anything.
898c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          break;
899c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case R_X86_64_PC32:
900c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          sym_addr = reloc;
901c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          break;
9024eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
903c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
904c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        default:
905c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          DL_ERR("unknown weak reloc type %d @ %p (%d)", type, rela, (int) (rela - start));
906c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          return -1;
907c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
908c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      } else {
909c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        // We got a definition.
910c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        sym_addr = static_cast<Elf_Addr>(s->st_value + lsi->load_bias);
911c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      }
912c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocSymbol);
913c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    } else {
914c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      s = NULL;
915c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
916c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes
917c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    switch (type) {
9184eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__x86_64__)
919c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_JUMP_SLOT:
920c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocAbsolute);
921c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
922c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO JMP_SLOT %08zx <- %08zx %s", static_cast<size_t>(reloc),
923c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
924c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
925c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
926c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_GLOB_DAT:
927c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocAbsolute);
928c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
929c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO GLOB_DAT %08zx <- %08zx %s", static_cast<size_t>(reloc),
930c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr + rela->r_addend), sym_name);
931c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
932c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
933c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_RELATIVE:
934c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
935c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
936c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      if (sym) {
937c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DL_ERR("odd RELATIVE form...");
938c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        return -1;
939c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      }
940c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO RELATIVE %08zx <- +%08zx", static_cast<size_t>(reloc),
941c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(si->base));
942c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      *reinterpret_cast<Elf_Addr*>(reloc) = si->base + rela->r_addend;
943c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
944c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_32:
945c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
946c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
947c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
948c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr), sym_name);
949c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
950c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
951c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin    case R_X86_64_64:
952c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      count_relocation(kRelocRelative);
953c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      MARK(rela->r_offset);
954c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
955c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin                 static_cast<size_t>(sym_addr), sym_name);
956c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend;
957c075c18537d18ccb6760ba1f5727ce2c0890285aPavel Chupin      break;
958c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    case R_X86_64_PC32:
959c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      count_relocation(kRelocRelative);
960c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      MARK(rela->r_offset);
961c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
962c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
963c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                 static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
964c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr + rela->r_addend - reloc;
965c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      break;
9664eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
967c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    default:
968c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      DL_ERR("unknown reloc type %d @ %p (%d)", type, rela, (int) (rela - start));
969c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes      return -1;
970c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
971c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  }
972c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes  return 0;
973c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes}
974c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
975c620059479c47a78d57086d73726c9adc2f337adElliott Hughesstatic int soinfo_relocate(soinfo* si, Elf_Rel* rel, unsigned count,
976d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                           soinfo* needed[])
9771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project{
978c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    Elf_Sym* symtab = si->symtab;
979d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    const char* strtab = si->strtab;
980c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    Elf_Sym* s;
981c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    Elf_Rel* start = rel;
982d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo* lsi;
9831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
9844688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    for (size_t idx = 0; idx < count; ++idx, ++rel) {
985c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        unsigned type = ELF_R_TYPE(rel->r_info);
986c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        // TODO: don't use unsigned for 'sym'. Use uint32_t or Elf_Addr instead.
987c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        unsigned sym = ELF_R_SYM(rel->r_info);
988c620059479c47a78d57086d73726c9adc2f337adElliott Hughes        Elf_Addr reloc = static_cast<Elf_Addr>(rel->r_offset + si->load_bias);
989c620059479c47a78d57086d73726c9adc2f337adElliott Hughes        Elf_Addr sym_addr = 0;
990d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        char* sym_name = NULL;
9911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
992c620059479c47a78d57086d73726c9adc2f337adElliott Hughes        DEBUG("Processing '%s' relocation at index %zd", si->name, idx);
993d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (type == 0) { // R_*_NONE
994d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            continue;
995d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
996d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (sym != 0) {
997d1b40d8c69dfca94185ea9231f6cb16f70c02e39Dima Zavin            sym_name = (char *)(strtab + symtab[sym].st_name);
9985ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            s = soinfo_do_lookup(si, sym_name, &lsi, needed);
999d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (s == NULL) {
10004eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                // We only allow an undefined symbol if this is a weak reference...
1001e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                s = &symtab[sym];
1002c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
1003e9b6fc6f82276188404a2839e31a2f4c19f6c336Elliott Hughes                    DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, si->name);
1004e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1005e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1006e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1007e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                /* IHI0044C AAELF 4.5.1.1:
1008e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1009e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   Libraries are not searched to resolve weak references.
1010e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   It is not an error for a weak reference to remain
1011e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   unsatisfied.
1012e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1013e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   During linking, the value of an undefined weak reference is:
1014e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - Zero if the relocation type is absolute
1015e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                   - The address of the place if the relocation is pc-relative
1016bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes                   - The address of nominal base address if the relocation
1017e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                     type is base-relative.
1018e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                  */
1019e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
1020e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                switch (type) {
10214eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
1022e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_JUMP_SLOT:
1023e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_GLOB_DAT:
1024e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_ABS32:
1025e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_RELATIVE:    /* Don't care. */
10264eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // sym_addr was initialized to be zero above or relocation
10274eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // code below does not care about value of sym_addr.
10284eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // No need to do anything.
10294eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    break;
10304eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
1031d7daacb46372132ae3f0121647074936c304b572Raghu Gandham                case R_386_JMP_SLOT:
1032e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_GLOB_DAT:
1033e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_32:
1034c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes                case R_386_RELATIVE:    /* Don't care. */
10354eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // sym_addr was initialized to be zero above or relocation
10364eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // code below does not care about value of sym_addr.
10374eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // No need to do anything.
1038e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
1039e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_386_PC32:
1040e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    sym_addr = reloc;
1041e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    break;
10424eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1043e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan
10444eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
1045e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                case R_ARM_COPY:
10464eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    // Fall through. Can't really copy if weak symbol is not found at run-time.
10474eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1048e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                default:
10494eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                    DL_ERR("unknown weak reloc type %d @ %p (%d)", type, rel, (int) (rel - start));
1050e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                    return -1;
1051e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan                }
1052e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            } else {
10534eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                // We got a definition.
1054c620059479c47a78d57086d73726c9adc2f337adElliott Hughes                sym_addr = static_cast<Elf_Addr>(s->st_value + lsi->load_bias);
1055d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1056bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocSymbol);
10571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        } else {
1058e823807dc2fb5f2d44cd00d75c10ddd9f5e93beaDoug Kwan            s = NULL;
10591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
10601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1061c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (type) {
10624eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
10631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_JUMP_SLOT:
1064bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
10651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1066ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
1067c620059479c47a78d57086d73726c9adc2f337adElliott Hughes            *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
10681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
10691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_GLOB_DAT:
1070bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
10711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1072ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
1073c620059479c47a78d57086d73726c9adc2f337adElliott Hughes            *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
10741dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
10751dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_ABS32:
1076bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocAbsolute);
10771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1078ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
1079c620059479c47a78d57086d73726c9adc2f337adElliott Hughes            *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
10801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
108134ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner        case R_ARM_REL32:
1082bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocRelative);
108334ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            MARK(rel->r_offset);
1084ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
108534ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner                       reloc, sym_addr, rel->r_offset, sym_name);
1086c620059479c47a78d57086d73726c9adc2f337adElliott Hughes            *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr - rel->r_offset;
108734ea5117dbeba41ae5dfdfb2c2ec8cae23388b66David 'Digit' Turner            break;
10881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case R_ARM_COPY:
1089d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            if ((si->flags & FLAG_EXE) == 0) {
1090d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                /*
1091d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
1092d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 *
1093d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * Section 4.7.1.10 "Dynamic relocations"
1094d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * R_ARM_COPY may only appear in executable objects where e_type is
1095d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * set to ET_EXEC.
1096d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 *
1097d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * TODO: FLAG_EXE is set for both ET_DYN and ET_EXEC executables.
1098d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * We should explicitly disallow ET_DYN executables from having
1099d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 * R_ARM_COPY relocations.
1100d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                 */
1101d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                DL_ERR("%s R_ARM_COPY relocations only supported for ET_EXEC", si->name);
1102d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                return -1;
1103d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            }
1104bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            count_relocation(kRelocCopy);
11051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            MARK(rel->r_offset);
1106ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            TRACE_TYPE(RELO, "RELO %08x <- %d @ %08x %s", reloc, s->st_size, sym_addr, sym_name);
1107d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            if (reloc == sym_addr) {
1108c620059479c47a78d57086d73726c9adc2f337adElliott Hughes                Elf_Sym *src = soinfo_do_lookup(NULL, sym_name, &lsi, needed);
11095ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
11105ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (src == NULL) {
11115ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s R_ARM_COPY relocation source cannot be resolved", si->name);
11125ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
11135ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
11145ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (lsi->has_DT_SYMBOLIC) {
11155ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s invalid R_ARM_COPY relocation against DT_SYMBOLIC shared "
11165ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                           "library %s (built with -Bsymbolic?)", si->name, lsi->name);
11175ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
11185ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
11195ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                if (s->st_size < src->st_size) {
11205ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    DL_ERR("%s R_ARM_COPY relocation size mismatch (%d < %d)",
11215ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                           si->name, s->st_size, src->st_size);
11225ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                    return -1;
11235ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                }
11245ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                memcpy((void*)reloc, (void*)(src->st_value + lsi->load_bias), src->st_size);
11255ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            } else {
11265ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                DL_ERR("%s R_ARM_COPY relocation target cannot be resolved", si->name);
1127d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich                return -1;
1128d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich            }
11291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
11304eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
11314eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_JMP_SLOT:
11324eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
11334eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
11344eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO JMP_SLOT %08x <- %08x %s", reloc, sym_addr, sym_name);
11354eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
11364eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
11374eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_GLOB_DAT:
11384eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
11394eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
11404eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO GLOB_DAT %08x <- %08x %s", reloc, sym_addr, sym_name);
11414eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            *reinterpret_cast<Elf_Addr*>(reloc) = sym_addr;
11424eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
11434eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_32:
11444eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
11454eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
11464eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
11474eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
11484eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
11494eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_PC32:
11504eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
11514eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
11524eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
11534eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                       reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
11544eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            *reinterpret_cast<Elf_Addr*>(reloc) += (sym_addr - reloc);
11554eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
11564eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__mips__)
11574eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_MIPS_REL32:
11584eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocAbsolute);
11594eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
11604eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x %s",
11614eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                       reloc, sym_addr, (sym_name) ? sym_name : "*SECTIONHDR*");
11624eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            if (s) {
11634eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                *reinterpret_cast<Elf_Addr*>(reloc) += sym_addr;
11644eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            } else {
11654eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                *reinterpret_cast<Elf_Addr*>(reloc) += si->base;
11664eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            }
11674eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
11684eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
11694eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes
11704eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
11714eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_ARM_RELATIVE:
11724eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#elif defined(__i386__)
11734eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes        case R_386_RELATIVE:
11744eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
11754eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            count_relocation(kRelocRelative);
11764eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            MARK(rel->r_offset);
11774eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            if (sym) {
11784eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                DL_ERR("odd RELATIVE form...");
11794eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                return -1;
11804eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            }
11814eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            TRACE_TYPE(RELO, "RELO RELATIVE %p <- +%p",
11824eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                       reinterpret_cast<void*>(reloc), reinterpret_cast<void*>(si->base));
11834eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            *reinterpret_cast<Elf_Addr*>(reloc) += si->base;
11844eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            break;
11851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        default:
11874eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            DL_ERR("unknown reloc type %d @ %p (%d)", type, rel, (int) (rel - start));
11881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return -1;
11891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
11901dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
11911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return 0;
11921dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
1193c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
11941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
11954eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
119687c358524e479235aa6241736d2ce325f89daafcBrian Carlstromstatic bool mips_relocate_got(soinfo* si, soinfo* needed[]) {
119787c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned* got = si->plt_got;
119887c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    if (got == NULL) {
119987c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom        return true;
120087c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    }
120187c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned local_gotno = si->mips_local_gotno;
120287c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned gotsym = si->mips_gotsym;
120387c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    unsigned symtabno = si->mips_symtabno;
1204c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    Elf_Sym* symtab = si->symtab;
1205d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1206d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    /*
1207d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * got[0] is address of lazy resolver function
1208d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * got[1] may be used for a GNU extension
1209bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes     * set it to a recognizable address in case someone calls it
1210d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * (should be _rtld_bind_start)
1211d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     * FIXME: maybe this should be in a separate routine
1212d7daacb46372132ae3f0121647074936c304b572Raghu Gandham     */
1213d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1214d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    if ((si->flags & FLAG_LINKER) == 0) {
121587c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom        size_t g = 0;
1216d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        got[g++] = 0xdeadbeef;
1217d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (got[g] & 0x80000000) {
1218d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            got[g++] = 0xdeadfeed;
1219d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1220d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        /*
1221d7daacb46372132ae3f0121647074936c304b572Raghu Gandham         * Relocate the local GOT entries need to be relocated
1222d7daacb46372132ae3f0121647074936c304b572Raghu Gandham         */
1223d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        for (; g < local_gotno; g++) {
1224d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            got[g] += si->load_bias;
1225d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1226d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1227d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1228d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    /* Now for the global GOT entries */
1229c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    Elf_Sym* sym = symtab + gotsym;
1230d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    got = si->plt_got + local_gotno;
123187c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    for (size_t g = gotsym; g < symtabno; g++, sym++, got++) {
1232d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        const char* sym_name;
1233c620059479c47a78d57086d73726c9adc2f337adElliott Hughes        Elf_Sym* s;
1234d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        soinfo* lsi;
1235d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1236d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        /* This is an undefined reference... try to locate it */
1237d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        sym_name = si->strtab + sym->st_name;
12385ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        s = soinfo_do_lookup(si, sym_name, &lsi, needed);
1239d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        if (s == NULL) {
1240d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* We only allow an undefined symbol if this is a weak
1241d7daacb46372132ae3f0121647074936c304b572Raghu Gandham               reference..   */
1242d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            s = &symtab[g];
1243c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
12444688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("cannot locate \"%s\"...", sym_name);
124587c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom                return false;
1246d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1247d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            *got = 0;
1248d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1249d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        else {
1250d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            /* FIXME: is this sufficient?
1251d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             * For reference see NetBSD link loader
1252d7daacb46372132ae3f0121647074936c304b572Raghu 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
1253d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             */
12545ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel             *got = lsi->load_bias + s->st_value;
1255d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        }
1256d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
125787c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    return true;
1258d7daacb46372132ae3f0121647074936c304b572Raghu Gandham}
1259d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1260d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1261ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughesvoid soinfo::CallArray(const char* array_name UNUSED, linker_function_t* functions, size_t count, bool reverse) {
1262ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  if (functions == NULL) {
1263d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1264d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
12658215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1266c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, name);
12678215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1268ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int begin = reverse ? (count - 1) : 0;
1269ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int end = reverse ? -1 : count;
1270ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  int step = reverse ? -1 : 1;
12718215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1272ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  for (int i = begin; i != end; i += step) {
1273ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
1274ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    CallFunction("function", functions[i]);
1275d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1276d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1277ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s for '%s' ]", array_name, name);
12781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
12791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1280ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughesvoid soinfo::CallFunction(const char* function_name UNUSED, linker_function_t function) {
1281db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  if (function == NULL || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
1282d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1283d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1284d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
1285ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, name);
1286d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  function();
1287ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes  TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, name);
1288db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes
1289db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // The function may have called dlopen(3) or dlclose(3), so we need to ensure our data structures
1290db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  // are still writable. This happens with our debug malloc (see http://b/7941716).
1291db492b3ca753c4ef688d0daf648294de0c89145eElliott Hughes  set_soinfo_pool_protection(PROT_READ | PROT_WRITE);
12929181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov}
12939181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
1294d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallPreInitConstructors() {
12958147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_PREINIT_ARRAY functions are called before any other constructors for executables,
12968147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // but ignored in a shared library.
1297d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_PREINIT_ARRAY", preinit_array, preinit_array_count, false);
1298d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes}
1299e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1300d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallConstructors() {
1301d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  if (constructors_called) {
1302d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    return;
1303d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
1304e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
1305d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // We set constructors_called before actually calling the constructors, otherwise it doesn't
1306d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // protect against recursive constructor calls. One simple example of constructor recursion
1307d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
1308d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 1. The program depends on libc, so libc's constructor is called here.
1309d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
1310d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 3. dlopen() calls the constructors on the newly created
1311d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    soinfo for libc_malloc_debug_leak.so.
1312d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  // 4. The debug .so depends on libc, so CallConstructors is
1313d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    called again with the libc soinfo. If it doesn't trigger the early-
1314d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  //    out above, the libc constructor will be called again (recursively!).
1315d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  constructors_called = true;
1316d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
13178147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  if ((flags & FLAG_EXE) == 0 && preinit_array != NULL) {
13188147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes    // The GNU dynamic linker silently ignores these, but we warn the developer.
1319c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
13208147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes          name, preinit_array_count);
1321d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
13221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1323d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom  if (dynamic != NULL) {
1324c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    for (Elf_Dyn* d = dynamic; d->d_tag != DT_NULL; ++d) {
1325d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom      if (d->d_tag == DT_NEEDED) {
1326d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        const char* library_name = strtab + d->d_un.d_val;
13278147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes        TRACE("\"%s\": calling constructors in DT_NEEDED \"%s\"", name, library_name);
13288147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes        find_loaded_library(library_name)->CallConstructors();
1329d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes      }
13301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1331d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  }
13321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13338147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  TRACE("\"%s\": calling constructors", name);
13348147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
13358147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_INIT should be called before DT_INIT_ARRAY if both are present.
1336d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_INIT", init_func);
1337d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_INIT_ARRAY", init_array, init_array_count, false);
1338e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov}
13398215679b355efe3829bab571bd566dc818ea4cccDavid 'Digit' Turner
1340d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughesvoid soinfo::CallDestructors() {
13418147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  TRACE("\"%s\": calling destructors", name);
13428147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
13438147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_FINI_ARRAY must be parsed in reverse order.
1344d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallArray("DT_FINI_ARRAY", fini_array, fini_array_count, true);
13458147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes
13468147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes  // DT_FINI should be called after DT_FINI_ARRAY if both are present.
1347d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes  CallFunction("DT_FINI", fini_func);
13481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
13491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project/* Force any of the closed stdin, stdout and stderr to be associated with
13511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project   /dev/null. */
13525419b9474753d25dff947c7740532f86d130c0beElliott Hughesstatic int nullify_closed_stdio() {
13531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int dev_null, i, status;
13541dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int return_value = 0;
13551dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
135616084168111cd7d2ac8a6b92e6fa6df5696928a5David 'Digit' Turner    dev_null = TEMP_FAILURE_RETRY(open("/dev/null", O_RDWR));
13571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null < 0) {
13584688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("cannot open /dev/null: %s", strerror(errno));
13591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        return -1;
13601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1361ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    TRACE("[ Opened /dev/null file-descriptor=%d]", dev_null);
13621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If any of the stdio file descriptors is valid and not associated
13641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project       with /dev/null, dup /dev/null to it.  */
13651dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    for (i = 0; i < 3; i++) {
13661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* If it is /dev/null already, we are done. */
13674688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (i == dev_null) {
13681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
13694688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
13701dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1371ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Nullifying stdio file descriptor %d]", i);
13724688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(fcntl(i, F_GETFL));
13731dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13744688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        /* If file is opened, we are good. */
13754688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status != -1) {
13764688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            continue;
13774688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        }
13781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13791dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* The only error we allow is that the file descriptor does not
13801dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           exist, in which case we dup /dev/null to it. */
13811dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (errno != EBADF) {
13824688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("fcntl failed: %s", strerror(errno));
13831dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
13841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
13851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
13861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13871dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        /* Try dupping /dev/null to this stdio file descriptor and
13881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           repeat if there is a signal.  Note that any errors in closing
13891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project           the stdio descriptor are lost.  */
13904688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(dup2(dev_null, i));
13911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        if (status < 0) {
13924688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("dup2 failed: %s", strerror(errno));
13931dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
13941dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            continue;
13951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
13961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
13971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
13981dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    /* If /dev/null is not one of the stdio file descriptors, close it. */
13991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    if (dev_null > 2) {
1400ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        TRACE("[ Closing /dev/null file-descriptor=%d]", dev_null);
14014688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        status = TEMP_FAILURE_RETRY(close(dev_null));
14024688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        if (status == -1) {
14034688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes            DL_ERR("close failed: %s", strerror(errno));
14041dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            return_value = -1;
14051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
14061dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
14071dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
14081dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return return_value;
14091dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
14101dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1411124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughesstatic bool soinfo_link_image(soinfo* si) {
1412f186a1819a4489c8d73485042670a3002742b683Ji-Hwan Lee    /* "base" might wrap around UINT32_MAX. */
1413c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    Elf_Addr base = si->load_bias;
1414c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    const Elf_Phdr *phdr = si->phdr;
14151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    int phnum = si->phnum;
1416d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    bool relocating_linker = (si->flags & FLAG_LINKER) != 0;
14171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1418b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can't debug anything until the linker is relocated */
1419b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (!relocating_linker) {
1420ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        INFO("[ linking %s ]", si->name);
1421c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(si->base), si->flags);
1422b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    }
14231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
142463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    /* Extract dynamic section */
1425124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    size_t dynamic_count;
1426c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    Elf_Word dynamic_flags;
142712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    phdr_table_get_dynamic_section(phdr, phnum, base, &si->dynamic,
1428cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman                                   &dynamic_count, &dynamic_flags);
142963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    if (si->dynamic == NULL) {
1430b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1431124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("missing PT_DYNAMIC in \"%s\"", si->name);
1432b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
1433124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
143463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    } else {
1435b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        if (!relocating_linker) {
1436ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("dynamic = %p", si->dynamic);
1437b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner        }
143863f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    }
143963f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
14404eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__arm__)
144163f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner    (void) phdr_table_get_arm_exidx(phdr, phnum, base,
144263f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner                                    &si->ARM_exidx, &si->ARM_exidx_count);
144363f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner#endif
144463f99f4a4e05353de2e8ba3d7bd4d882d716167aDavid 'Digit' Turner
14458147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes    // Extract useful information from dynamic section.
1446d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    uint32_t needed_count = 0;
1447c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1448c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
1449c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes              d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1450c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        switch (d->d_tag) {
14511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_HASH:
1452d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->nbucket = ((unsigned *) (base + d->d_un.d_ptr))[0];
1453d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->nchain = ((unsigned *) (base + d->d_un.d_ptr))[1];
1454d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->bucket = (unsigned *) (base + d->d_un.d_ptr + 8);
1455d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->chain = (unsigned *) (base + d->d_un.d_ptr + 8 + si->nbucket * 4);
14561dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_STRTAB:
1458d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->strtab = (const char *) (base + d->d_un.d_ptr);
14591dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14601dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_SYMTAB:
1461c620059479c47a78d57086d73726c9adc2f337adElliott Hughes            si->symtab = (Elf_Sym *) (base + d->d_un.d_ptr);
14621dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14634eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if !defined(__LP64__)
14641dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTREL:
1465d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val != DT_REL) {
1466124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1467124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
14681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
14691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1470c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
14711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_JMPREL:
14724eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1473c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            si->plt_rela = (Elf_Rela*) (base + d->d_un.d_ptr);
1474c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1475c620059479c47a78d57086d73726c9adc2f337adElliott Hughes            si->plt_rel = (Elf_Rel*) (base + d->d_un.d_ptr);
1476c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
14771dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14781dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTRELSZ:
14794eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1480c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            si->plt_rela_count = d->d_un.d_val / sizeof(Elf_Rela);
1481c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1482c620059479c47a78d57086d73726c9adc2f337adElliott Hughes            si->plt_rel_count = d->d_un.d_val / sizeof(Elf_Rel);
1483c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
14841dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14854eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if !defined(__LP64__)
14861dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PLTGOT:
14874eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            // Used by 32-bit MIPS.
1488d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->plt_got = (unsigned *)(base + d->d_un.d_ptr);
14891dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1490c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
14911dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_DEBUG:
1492bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_DEBUG entry to the address of _r_debug for GDB
1493cf23905a4bcc7bfdd109be5b6d69ad06877aa217Chris Dearman            // if the dynamic table is writable
149499c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            if ((dynamic_flags & PF_W) != 0) {
1495c620059479c47a78d57086d73726c9adc2f337adElliott Hughes                d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
149699c32055cb50443f722c55914869c1f6bb941959Elliott Hughes            }
14971dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
14984eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1499c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         case DT_RELA:
1500c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            si->rela = (Elf_Rela*) (base + d->d_un.d_ptr);
1501c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1502c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes         case DT_RELASZ:
1503c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            si->rela_count = d->d_un.d_val / sizeof(Elf_Rela);
1504c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1505c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_REL:
1506c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            DL_ERR("unsupported DT_REL in \"%s\"", si->name);
1507c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1508c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_RELSZ:
1509c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            DL_ERR("unsupported DT_RELSZ in \"%s\"", si->name);
1510c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1511c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1512c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_REL:
1513c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            si->rel = (Elf_Rel*) (base + d->d_un.d_ptr);
1514c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1515c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        case DT_RELSZ:
1516c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            si->rel_count = d->d_un.d_val / sizeof(Elf_Rel);
1517c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            break;
1518ad13c57298e57d33c130fb03a2c6494da573408cShin-ichiro KAWASAKI         case DT_RELA:
1519124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            DL_ERR("unsupported DT_RELA in \"%s\"", si->name);
1520124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1521c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
15221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT:
1523ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->init_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
15248147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_INIT) found at %p", si->name, si->init_func);
15251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI:
1527ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->fini_func = reinterpret_cast<linker_function_t>(base + d->d_un.d_ptr);
15288147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s destructors (DT_FINI) found at %p", si->name, si->fini_func);
15291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAY:
1531ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->init_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
15328147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", si->name, si->init_array);
15331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_INIT_ARRAYSZ:
1535c620059479c47a78d57086d73726c9adc2f337adElliott Hughes            si->init_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
15361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15371dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAY:
1538ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->fini_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
15398147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", si->name, si->fini_array);
15401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_FINI_ARRAYSZ:
1542c620059479c47a78d57086d73726c9adc2f337adElliott Hughes            si->fini_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
15431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAY:
1545ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            si->preinit_array = reinterpret_cast<linker_function_t*>(base + d->d_un.d_ptr);
15468147d3c284932896ab6095232b355979b9eb33d3Elliott Hughes            DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", si->name, si->preinit_array);
15471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_PREINIT_ARRAYSZ:
1549c620059479c47a78d57086d73726c9adc2f337adElliott Hughes            si->preinit_array_count = ((unsigned)d->d_un.d_val) / sizeof(Elf_Addr);
15501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
15511dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        case DT_TEXTREL:
1552e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__)
1553e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes            DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1554e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes            return false;
1555e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else
15565135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            si->has_text_relocations = true;
15571dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            break;
1558e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
15595ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_SYMBOLIC:
15605ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            si->has_DT_SYMBOLIC = true;
15615ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
1562d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        case DT_NEEDED:
1563d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            ++needed_count;
1564d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            break;
15655ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel        case DT_FLAGS:
1566d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_TEXTREL) {
1567e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if defined(__LP64__)
1568e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes                DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", si->name);
1569e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes                return false;
1570e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#else
15715ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_text_relocations = true;
1572e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
15735ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
1574d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (d->d_un.d_val & DF_SYMBOLIC) {
15755ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel                si->has_DT_SYMBOLIC = true;
15765ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            }
15775ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel            break;
15784eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
1579d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_STRSZ:
1580d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_SYMENT:
1581d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_RELENT:
1582d7daacb46372132ae3f0121647074936c304b572Raghu Gandham             break;
1583d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_MAP:
1584bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes            // Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
1585d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            {
1586d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom              r_debug** dp = (r_debug**) d->d_un.d_ptr;
1587d7daacb46372132ae3f0121647074936c304b572Raghu Gandham              *dp = &_r_debug;
1588d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            }
1589d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1590d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_RLD_VERSION:
1591d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_FLAGS:
1592d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_BASE_ADDRESS:
1593d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_UNREFEXTNO:
1594d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1595d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1596d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_SYMTABNO:
1597d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_symtabno = d->d_un.d_val;
1598d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1599d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1600d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_LOCAL_GOTNO:
1601d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_local_gotno = d->d_un.d_val;
1602d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
1603d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1604d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        case DT_MIPS_GOTSYM:
1605d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            si->mips_gotsym = d->d_un.d_val;
1606d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
16074eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#endif
1608d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
1609d7daacb46372132ae3f0121647074936c304b572Raghu Gandham        default:
16104eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes            DEBUG("Unused DT entry: type %p arg %p",
16114eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes                  reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
1612d7daacb46372132ae3f0121647074936c304b572Raghu Gandham            break;
16131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
16141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16151dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1616c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
1617c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes          reinterpret_cast<void*>(si->base), si->strtab, si->symtab);
16181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1619124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    // Sanity checks.
1620d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (relocating_linker && needed_count != 0) {
1621d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
1622d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        return false;
1623d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
1624124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->nbucket == 0) {
1625124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_HASH in \"%s\" (built with --hash-style=gnu?)", si->name);
1626124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1627124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1628124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->strtab == 0) {
1629124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_STRTAB in \"%s\"", si->name);
1630124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1631124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    }
1632124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (si->symtab == 0) {
1633124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        DL_ERR("empty/missing DT_SYMTAB in \"%s\"", si->name);
1634124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
16351dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
16361dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
16377e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes    // If this is the main executable, then load all of the libraries from LD_PRELOAD now.
1638d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    if (si->flags & FLAG_EXE) {
1639d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        memset(gLdPreloads, 0, sizeof(gLdPreloads));
16407e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes        size_t preload_count = 0;
1641d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes        for (size_t i = 0; gLdPreloadNames[i] != NULL; i++) {
1642d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            soinfo* lsi = find_library(gLdPreloadNames[i]);
16437e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes            if (lsi != NULL) {
16447e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                gLdPreloads[preload_count++] = lsi;
16457e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes            } else {
16467e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                // As with glibc, failure to load an LD_PRELOAD library is just a warning.
16477e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                DL_WARN("could not load library \"%s\" from LD_PRELOAD for \"%s\"; caused by %s",
16487e5a8cc5230dcc027686813e51a1b001cee4c602Elliott Hughes                        gLdPreloadNames[i], si->name, linker_get_error_buffer());
16494fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer            }
16504fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer        }
16514fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer    }
16524fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
1653d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo** needed = (soinfo**) alloca((1 + needed_count) * sizeof(soinfo*));
1654d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    soinfo** pneeded = needed;
165512c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel
1656c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    for (Elf_Dyn* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
1657d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (d->d_tag == DT_NEEDED) {
1658d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            const char* library_name = si->strtab + d->d_un.d_val;
1659ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes            DEBUG("%s needs %s", si->name, library_name);
1660d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            soinfo* lsi = find_library(library_name);
1661d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes            if (lsi == NULL) {
1662650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes                strlcpy(tmp_err_buf, linker_get_error_buffer(), sizeof(tmp_err_buf));
16634688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes                DL_ERR("could not load library \"%s\" needed by \"%s\"; caused by %s",
1664d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                       library_name, si->name, tmp_err_buf);
1665124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes                return false;
16661dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
166712c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel            *pneeded++ = lsi;
16681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
16691dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
167012c78bbded8ec03f821dfa09174464c04836e4eaArd Biesheuvel    *pneeded = NULL;
16711dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1672e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__)
16735135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
1674e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // Make segments writable to allow text relocations to work properly. We will later call
1675e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // phdr_table_protect_segments() after all of them are applied and all constructors are run.
16763d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich        DL_WARN("%s has text relocations. This is wasting memory and prevents "
16773d4470c14a57b0b9ae74060370be53469e95d6a1Nick Kralevich                "security hardening. Please fix.", si->name);
16785135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_unprotect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
16795135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't unprotect loadable segments for \"%s\": %s",
16805135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1681124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
16825135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
16835135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    }
1684e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
16855135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich
16864eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(USE_RELA)
1687c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (si->plt_rela != NULL) {
1688c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DEBUG("[ relocating %s plt ]\n", si->name );
1689c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        if (soinfo_relocate_a(si, si->plt_rela, si->plt_rela_count, needed)) {
1690c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1691c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
1692c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1693c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    if (si->rela != NULL) {
1694c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        DEBUG("[ relocating %s ]\n", si->name );
1695c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        if (soinfo_relocate_a(si, si->rela, si->rela_count, needed)) {
1696c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes            return false;
1697c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes        }
1698c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    }
1699c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#else
1700d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si->plt_rel != NULL) {
1701ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        DEBUG("[ relocating %s plt ]", si->name );
1702d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (soinfo_relocate(si, si->plt_rel, si->plt_rel_count, needed)) {
1703124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1704124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
17051dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1706d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    if (si->rel != NULL) {
1707ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        DEBUG("[ relocating %s ]", si->name );
1708d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        if (soinfo_relocate(si, si->rel, si->rel_count, needed)) {
1709124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
1710124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        }
17111dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1712c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes#endif
17131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
17144eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__mips__)
171587c358524e479235aa6241736d2ce325f89daafcBrian Carlstrom    if (!mips_relocate_got(si, needed)) {
1716124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
1717d7daacb46372132ae3f0121647074936c304b572Raghu Gandham    }
1718d7daacb46372132ae3f0121647074936c304b572Raghu Gandham#endif
1719d7daacb46372132ae3f0121647074936c304b572Raghu Gandham
17201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_LINKED;
1721ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    DEBUG("[ finished linking %s ]", si->name);
17221dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1723e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#if !defined(__LP64__)
17245135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich    if (si->has_text_relocations) {
1725e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes        // All relocations are done, we can protect our segments back to read-only.
17265135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        if (phdr_table_protect_segments(si->phdr, si->phnum, si->load_bias) < 0) {
17275135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich            DL_ERR("can't protect segments for \"%s\": %s",
17285135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich                   si->name, strerror(errno));
1729124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes            return false;
17305135b3ae6ebc460418f7917bd36b368340e48d5aNick Kralevich        }
17311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
1732e4d792adb8d6f9228b9ac9dc1ad7f43b271f085fElliott Hughes#endif
17331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1734b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    /* We can also turn on GNU RELRO protection */
1735b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    if (phdr_table_protect_gnu_relro(si->phdr, si->phnum, si->load_bias) < 0) {
17364688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes        DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
17374688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes               si->name, strerror(errno));
1738124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes        return false;
17399ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich    }
17409ec0f03a0d0b17bbb94ac0b9fef6add28a133c3aNick Kralevich
17411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    notify_gdb_of_load(si);
1742124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    return true;
17431dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
17441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1745468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1746c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * This function add vdso to internal dso list.
1747c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * It helps to stack unwinding through signal handlers.
1748c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov * Also, it makes bionic more like glibc.
1749c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov */
1750c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikovstatic void add_vdso(KernelArgumentBlock& args UNUSED) {
17514eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(AT_SYSINFO_EHDR)
1752c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    Elf_Ehdr* ehdr_vdso = reinterpret_cast<Elf_Ehdr*>(args.getauxval(AT_SYSINFO_EHDR));
1753c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
1754c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov    soinfo* si = soinfo_alloc("[vdso]");
1755ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov
1756c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    si->phdr = reinterpret_cast<Elf_Phdr*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
1757c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov    si->phnum = ehdr_vdso->e_phnum;
1758ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov    si->base = reinterpret_cast<Elf_Addr>(ehdr_vdso);
1759ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov    si->size = phdr_table_get_load_size(si->phdr, si->phnum);
1760ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov    si->flags = 0;
1761ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov    si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
1762ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov
1763ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov    soinfo_link_image(si);
1764ebd506c69e12b6dcaf5be94cc8ed1b53af299f9fSergey Melnikov    insert_soinfo_into_debug_map(si);
1765c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov#endif
1766c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov}
1767c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
1768c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov/*
1769468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This code is called after the linker has linked itself and
1770468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * fixed it's own GOT. It is safe to make references to externs
1771468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * and other non-local data at this point.
1772468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
1773c620059479c47a78d57086d73726c9adc2f337adElliott Hughesstatic Elf_Addr __linker_init_post_relocation(KernelArgumentBlock& args, Elf_Addr linker_base) {
177442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    /* NOTE: we store the args pointer on a special location
1775ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       of the temporary TLS area in order to pass it to
1776ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       the C Library's runtime initializer.
1777ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *
1778ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       The initializer must clear the slot and reset the TLS
1779ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       to point to a different location to ensure that no other
1780ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     *       shared library constructor can access it.
1781ef0bd1857041ffde069cf52138aaf22c1af7130eDavid 'Digit' Turner     */
1782d3920b3a996b358e48232f417aa0a1e44a60f155Elliott Hughes  __libc_init_tls(args);
17831a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov
17841a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#if TIMING
17851a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    struct timeval t0, t1;
17861a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov    gettimeofday(&t0, 0);
17871a78fbb5c8228e4aea2a516818828b76044310f2Evgeniy Stepanov#endif
17881dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
178918a206c81d9743481e364384affd43306911283dElliott Hughes    // Initialize environment functions, and get to the ELF aux vectors table.
179042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    linker_env_init(args);
1791be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
17928d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // If this is a setuid/setgid program, close the security hole described in
17938d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    // ftp://ftp.freebsd.org/pub/FreeBSD/CERT/advisories/FreeBSD-SA-02:23.stdio.asc
17948d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    if (get_AT_SECURE()) {
17958d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich        nullify_closed_stdio();
17968d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich    }
17978d3e91d4f842911366155845afb3cfbdad0b4cadNick Kralevich
1798d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    debuggerd_init();
17991dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
180018a206c81d9743481e364384affd43306911283dElliott Hughes    // Get a few environment variables.
180161a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    const char* LD_DEBUG = linker_env_get("LD_DEBUG");
180261a9ccb41eba8c35cae6e21318aca7160a402c5bElliott Hughes    if (LD_DEBUG != NULL) {
1803650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes      gLdDebugVerbosity = atoi(LD_DEBUG);
180418a206c81d9743481e364384affd43306911283dElliott Hughes    }
1805be5755969d70668bbab0e0c0ed75ebd867189723David 'Digit' Turner
180618a206c81d9743481e364384affd43306911283dElliott Hughes    // Normally, these are cleaned by linker_env_init, but the test
180718a206c81d9743481e364384affd43306911283dElliott Hughes    // doesn't cost us anything.
180818a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpath_env = NULL;
180918a206c81d9743481e364384affd43306911283dElliott Hughes    const char* ldpreload_env = NULL;
181018a206c81d9743481e364384affd43306911283dElliott Hughes    if (!get_AT_SECURE()) {
181118a206c81d9743481e364384affd43306911283dElliott Hughes      ldpath_env = linker_env_get("LD_LIBRARY_PATH");
181218a206c81d9743481e364384affd43306911283dElliott Hughes      ldpreload_env = linker_env_get("LD_PRELOAD");
18131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1815ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    INFO("[ android linker & debugger ]");
18161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
181742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    soinfo* si = soinfo_alloc(args.argv[0]);
181818a206c81d9743481e364384affd43306911283dElliott Hughes    if (si == NULL) {
181918a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
18201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18211dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1822d39c3abd5ad8600fb1d79a0b95a58197197087e0Nick Kralevich    /* bootstrap the link map, the main exe always needs to be first */
18231dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    si->flags |= FLAG_EXE;
1824ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    link_map_t* map = &(si->link_map);
18251dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_addr = 0;
182742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    map->l_name = args.argv[0];
18281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_prev = NULL;
18291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    map->l_next = NULL;
18301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    _r_debug.r_map = map;
18321dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    r_debug_tail = map;
18331dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1834d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    /* gdb expects the linker to be in the debug shared object list.
1835d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * Without this, gdb has trouble locating the linker's ".text"
1836d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * and ".plt" sections. Gdb could also potentially use this to
1837d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * relocate the offset of our exported 'rtld_db_dlactivity' symbol.
1838d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * Don't use soinfo_alloc(), because the linker shouldn't
1839d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * be on the soinfo list.
184006f0e74a2b957db93fed3fdd49fe939ab2fe1595Ben Cheng     */
1841d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    {
1842d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        static soinfo linker_soinfo;
18434eeb1f12a8b63afc0d0ad4d466b16fbffb21cd5aElliott Hughes#if defined(__LP64__)
18441a57f9f75c5752d990a2749b000ec4ceace94b54Pavel Chupin        strlcpy(linker_soinfo.name, "/system/bin/linker64", sizeof(linker_soinfo.name));
18451a57f9f75c5752d990a2749b000ec4ceace94b54Pavel Chupin#else
1846d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        strlcpy(linker_soinfo.name, "/system/bin/linker", sizeof(linker_soinfo.name));
18471a57f9f75c5752d990a2749b000ec4ceace94b54Pavel Chupin#endif
1848d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        linker_soinfo.flags = 0;
1849d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        linker_soinfo.base = linker_base;
1850d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom
1851d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        /*
1852d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         * Set the dynamic field in the link map otherwise gdb will complain with
1853d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         * the following:
1854d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         *   warning: .dynamic section for "/system/bin/linker" is not at the
1855d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         *   expected address (wrong library or version mismatch?)
1856d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom         */
1857c620059479c47a78d57086d73726c9adc2f337adElliott Hughes        Elf_Ehdr *elf_hdr = (Elf_Ehdr *) linker_base;
1858c620059479c47a78d57086d73726c9adc2f337adElliott Hughes        Elf_Phdr *phdr = (Elf_Phdr*)((unsigned char*) linker_base + elf_hdr->e_phoff);
1859d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
1860d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                                       &linker_soinfo.dynamic, NULL, NULL);
1861d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        insert_soinfo_into_debug_map(&linker_soinfo);
1862d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    }
18631dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
186442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // Extract information passed from the kernel.
1865c620059479c47a78d57086d73726c9adc2f337adElliott Hughes    si->phdr = reinterpret_cast<Elf_Phdr*>(args.getauxval(AT_PHDR));
186642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->phnum = args.getauxval(AT_PHNUM);
186742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    si->entry = args.getauxval(AT_ENTRY);
18681dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18698180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    /* Compute the value of si->base. We can't rely on the fact that
18708180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * the first entry is the PHDR because this will not be true
18718180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     * for certain executables (e.g. some in the NDK unit test suite)
18728180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner     */
18738180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    si->base = 0;
1874b52e4385c403d18a68309e568ac729c787d900c4David 'Digit' Turner    si->size = phdr_table_get_load_size(si->phdr, si->phnum);
1875bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    si->load_bias = 0;
1876ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    for (size_t i = 0; i < si->phnum; ++i) {
187742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      if (si->phdr[i].p_type == PT_PHDR) {
1878c620059479c47a78d57086d73726c9adc2f337adElliott Hughes        si->load_bias = reinterpret_cast<Elf_Addr>(si->phdr) - si->phdr[i].p_vaddr;
1879c620059479c47a78d57086d73726c9adc2f337adElliott Hughes        si->base = reinterpret_cast<Elf_Addr>(si->phdr) - si->phdr[i].p_offset;
188042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes        break;
188142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes      }
18828180b08fb2f27052f9df2ae4787bb5bf409f13e0David 'Digit' Turner    }
1883d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    si->dynamic = NULL;
1884ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    si->ref_count = 1;
18851dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
18864688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    // Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
18874688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_LIBRARY_PATH(ldpath_env);
18884688279db5dcc4004941e7f133c4a1c3617d842cElliott Hughes    parse_LD_PRELOAD(ldpreload_env);
18894fd42c1dc002fa19349fa0d4ef97757eb1815032Matt Fischer
18905ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel    somain = si;
18915ae44f302b7d1d19f25c4c6f125e32dc369961d9Ard Biesheuvel
1892124fae9eabd7a25d80dfa8c3b56bed0f0fba16f1Elliott Hughes    if (!soinfo_link_image(si)) {
1893650be4e584eeab3591b9e273bfd6d169eea60853Elliott Hughes        __libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
189418a206c81d9743481e364384affd43306911283dElliott Hughes        exit(EXIT_FAILURE);
18951dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
18961dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1897c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov    add_vdso(args);
1898c45087bffa528c0809f0df2e0a3708eba7018b33Sergey Melnikov
1899d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallPreInitConstructors();
19009181a5dcfe69199415c7aebf93524cc3dd6f8a6fEvgeniy Stepanov
1901d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    for (size_t i = 0; gLdPreloads[i] != NULL; ++i) {
1902d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        gLdPreloads[i]->CallConstructors();
1903326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng    }
1904326e85eca6916eb904649f7bff65244a40088ba7Kito Cheng
1905d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom    /* After the link_image, the si->load_bias is initialized.
1906d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * For so lib, the map->l_addr will be updated in notify_gdb_of_load.
1907d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * We need to update this value for so exe here. So Unwind_Backtrace
1908d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom     * for some arch like x86 could work correctly within so exe.
19099c3449ecd9d5e9cdb1e995e35e53334a4a288a5eXiaokang Qin     */
1910c5db969aa4a24a76fc0964f2ff89533e832b47a6Chao-Ying Fu    map->l_addr = si->load_bias;
1911d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes    si->CallConstructors();
1912e83c56dfbb6a9a61f0f18031620322af97e80162Evgeniy Stepanov
19131dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING
19141dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    gettimeofday(&t1,NULL);
1915ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
19161dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
19171dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               (((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)
19181dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project               ));
19191dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19201dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if STATS
1921ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes    PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
1922bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocAbsolute],
1923bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocRelative],
1924bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocCopy],
1925bedfe38b8ba512dd6236c00e8b4a9b01c2bd1281Elliott Hughes           linker_stats.count[kRelocSymbol]);
19261dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19271dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if COUNT_PAGES
19281dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    {
19291dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned n;
19301dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned i;
19311dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        unsigned count = 0;
1932d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom        for (n = 0; n < 4096; n++) {
1933d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom            if (bitmask[n]) {
19341dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                unsigned x = bitmask[n];
1935d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                for (i = 0; i < 8; i++) {
1936d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    if (x & 1) {
1937d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                        count++;
1938d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom                    }
19391dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                    x >>= 1;
19401dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project                }
19411dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project            }
19421dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project        }
1943ca0c11bd823f37f03cc8067cb182876827d5275aElliott Hughes        PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
19441dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    }
19451dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19461dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
19471dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#if TIMING || STATS || COUNT_PAGES
19481dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    fflush(stdout);
19491dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project#endif
19501dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project
1951c00f2cb587630d5e954c7f548749f1e3170b3cb1Elliott Hughes    TRACE("[ Ready to execute '%s' @ %p ]", si->name, reinterpret_cast<void*>(si->entry));
19521dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project    return si->entry;
19531dc9e472e19acfe6dc7f41e429236e7eef7ceda1The Android Open Source Project}
1954468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
1955bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner/* Compute the load-bias of an existing executable. This shall only
1956bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * be used to compute the load bias of an executable or shared library
1957bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * that was loaded by the kernel itself.
1958bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *
1959bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Input:
1960bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    elf    -> address of ELF header, assumed to be at the start of the file.
1961bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner * Return:
1962bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    load bias, i.e. add the value of any p_vaddr in the file to get
1963bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner *    the corresponding address in memory.
1964bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner */
1965c620059479c47a78d57086d73726c9adc2f337adElliott Hughesstatic Elf_Addr get_elf_exec_load_bias(const Elf_Ehdr* elf) {
1966c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  Elf_Addr offset = elf->e_phoff;
1967c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  const Elf_Phdr* phdr_table = (const Elf_Phdr*)((char*)elf + offset);
1968c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  const Elf_Phdr* phdr_end = phdr_table + elf->e_phnum;
1969fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng
1970c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  for (const Elf_Phdr* phdr = phdr_table; phdr < phdr_end; phdr++) {
1971fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng    if (phdr->p_type == PT_LOAD) {
1972c620059479c47a78d57086d73726c9adc2f337adElliott Hughes      return reinterpret_cast<Elf_Addr>(elf) + phdr->p_offset - phdr->p_vaddr;
1973bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner    }
1974fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  }
1975fa8c05dc00bb41ae8fe5cb5e4f82816e30f7f7b2Kito Cheng  return 0;
1976bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner}
1977bea23e59f7145537fd4e600ae67fa92a798872cfDavid 'Digit' Turner
1978468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich/*
1979468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * This is the entry point for the linker, called from begin.S. This
1980468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * method is responsible for fixing the linker's own relocations, and
1981468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * then calling __linker_init_post_relocation().
1982468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich *
1983468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * Because this method is called before the linker has fixed it's own
1984468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * relocations, any attempt to reference an extern variable, extern
1985468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich * function, or other GOT reference will generate a segfault.
1986468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich */
1987c620059479c47a78d57086d73726c9adc2f337adElliott Hughesextern "C" Elf_Addr __linker_init(void* raw_args) {
198842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  KernelArgumentBlock args(raw_args);
198942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
1990c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  Elf_Addr linker_addr = args.getauxval(AT_BASE);
199142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
1992c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  Elf_Ehdr* elf_hdr = reinterpret_cast<Elf_Ehdr*>(linker_addr);
1993c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  Elf_Phdr* phdr = (Elf_Phdr*)((unsigned char*) linker_addr + elf_hdr->e_phoff);
199442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
199542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  soinfo linker_so;
199642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  memset(&linker_so, 0, sizeof(soinfo));
199742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
199842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.base = linker_addr;
199942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
200042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
2001d4ee82dfa3ba01baa10e3ca48fcb31a27b8a4e81Brian Carlstrom  linker_so.dynamic = NULL;
200242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phdr = phdr;
200342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.phnum = elf_hdr->e_phnum;
200442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  linker_so.flags |= FLAG_LINKER;
200542b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes
200642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  if (!soinfo_link_image(&linker_so)) {
200742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // It would be nice to print an error message, but if the linker
200842b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // can't link itself, there's no guarantee that we'll be able to
200942b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // call write() (because it involves a GOT reference).
201042b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    //
201142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // This situation should never occur unless the linker itself
201242b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    // is corrupt.
201342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes    exit(EXIT_FAILURE);
201442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  }
2015468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich
201642b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // We have successfully fixed our own relocations. It's safe to run
201742b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // the main part of the linker now.
20180d787c1fa18c6a1f29ef9840e28a68cf077be1deElliott Hughes  args.abort_message_ptr = &gAbortMessage;
2019c620059479c47a78d57086d73726c9adc2f337adElliott Hughes  Elf_Addr start_address = __linker_init_post_relocation(args, linker_addr);
20205419b9474753d25dff947c7740532f86d130c0beElliott Hughes
202142b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  set_soinfo_pool_protection(PROT_READ);
2022d23736e4f228e46304b7cbc674a1d0094d73e0f5Elliott Hughes
202342b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  // Return the address that the calling assembly stub should jump to.
202442b2c6a5eed5e4ef35315b8cd32d1355f12a69b6Elliott Hughes  return start_address;
2025468319ce4f3f7383d788b76c09cda2a405311f36Nick Kralevich}
2026