2009-03-04 04:28:35 +01:00
|
|
|
/*
|
2015-04-09 22:42:33 +02:00
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
2009-03-04 04:28:35 +01:00
|
|
|
* All rights reserved.
|
|
|
|
*
|
|
|
|
* Redistribution and use in source and binary forms, with or without
|
|
|
|
* modification, are permitted provided that the following conditions
|
|
|
|
* are met:
|
|
|
|
* * Redistributions of source code must retain the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer.
|
|
|
|
* * Redistributions in binary form must reproduce the above copyright
|
|
|
|
* notice, this list of conditions and the following disclaimer in
|
|
|
|
* the documentation and/or other materials provided with the
|
|
|
|
* distribution.
|
|
|
|
*
|
|
|
|
* THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
|
|
|
|
* "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
|
|
|
|
* LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
|
|
|
|
* FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
|
|
|
|
* COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
|
|
|
|
* INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
|
|
|
|
* BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
|
|
|
|
* OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
|
|
|
|
* AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
|
|
|
|
* OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
|
|
|
|
* OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
|
|
|
|
* SUCH DAMAGE.
|
|
|
|
*/
|
|
|
|
|
2015-06-03 02:36:54 +02:00
|
|
|
#include <android/api-level.h>
|
2012-08-04 01:49:39 +02:00
|
|
|
#include <dlfcn.h>
|
|
|
|
#include <errno.h>
|
|
|
|
#include <fcntl.h>
|
2014-02-11 02:46:57 +01:00
|
|
|
#include <inttypes.h>
|
2012-08-04 01:49:39 +02:00
|
|
|
#include <pthread.h>
|
2009-03-04 04:28:35 +01:00
|
|
|
#include <stdio.h>
|
|
|
|
#include <stdlib.h>
|
|
|
|
#include <string.h>
|
2012-08-04 01:49:39 +02:00
|
|
|
#include <sys/mman.h>
|
2014-11-10 04:27:20 +01:00
|
|
|
#include <sys/param.h>
|
2015-10-14 20:15:45 +02:00
|
|
|
#include <sys/prctl.h>
|
2012-08-04 01:49:39 +02:00
|
|
|
#include <unistd.h>
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-08-22 21:25:04 +02:00
|
|
|
#include <new>
|
2015-03-24 02:43:02 +01:00
|
|
|
#include <string>
|
2015-09-29 01:38:31 +02:00
|
|
|
#include <unordered_map>
|
2015-03-24 02:43:02 +01:00
|
|
|
#include <vector>
|
2014-08-22 21:25:04 +02:00
|
|
|
|
2012-08-04 01:49:39 +02:00
|
|
|
// Private C library headers.
|
2013-10-10 00:50:50 +02:00
|
|
|
#include "private/bionic_tls.h"
|
|
|
|
#include "private/KernelArgumentBlock.h"
|
|
|
|
#include "private/ScopedPthreadMutexLocker.h"
|
2014-09-06 01:42:53 +02:00
|
|
|
#include "private/ScopeGuard.h"
|
|
|
|
#include "private/UniquePtr.h"
|
2009-03-04 04:28:35 +01:00
|
|
|
|
|
|
|
#include "linker.h"
|
2015-03-10 23:30:26 +01:00
|
|
|
#include "linker_block_allocator.h"
|
2009-03-04 04:28:35 +01:00
|
|
|
#include "linker_debug.h"
|
2015-04-22 22:10:04 +02:00
|
|
|
#include "linker_sleb128.h"
|
2012-06-18 18:13:49 +02:00
|
|
|
#include "linker_phdr.h"
|
2015-01-09 08:30:15 +01:00
|
|
|
#include "linker_relocs.h"
|
2015-02-04 01:06:47 +01:00
|
|
|
#include "linker_reloc_iterators.h"
|
2015-10-02 03:41:57 +02:00
|
|
|
#include "linker_utils.h"
|
2015-07-29 12:00:22 +02:00
|
|
|
|
|
|
|
#include "base/strings.h"
|
2015-01-16 14:22:54 +01:00
|
|
|
#include "ziparchive/zip_archive.h"
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-10-06 20:08:13 +02:00
|
|
|
extern void __libc_init_globals(KernelArgumentBlock&);
|
2015-06-09 03:04:00 +02:00
|
|
|
extern void __libc_init_AT_SECURE(KernelArgumentBlock&);
|
|
|
|
|
|
|
|
// Override macros to use C++ style casts.
|
2015-01-23 01:04:25 +01:00
|
|
|
#undef ELF_ST_TYPE
|
|
|
|
#define ELF_ST_TYPE(x) (static_cast<uint32_t>(x) & 0xf)
|
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-03-10 23:43:50 +01:00
|
|
|
static LinkerTypeAllocator<soinfo> g_soinfo_allocator;
|
|
|
|
static LinkerTypeAllocator<LinkedListEntry<soinfo>> g_soinfo_links_allocator;
|
2012-09-12 13:00:55 +02:00
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
static soinfo* solist;
|
|
|
|
static soinfo* sonext;
|
2014-09-12 18:43:13 +02:00
|
|
|
static soinfo* somain; // main process, always the one after libdl_info
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-05-14 19:02:03 +02:00
|
|
|
static const char* const kDefaultLdPaths[] = {
|
2013-10-26 02:38:02 +02:00
|
|
|
#if defined(__LP64__)
|
2013-10-08 23:27:10 +02:00
|
|
|
"/vendor/lib64",
|
|
|
|
"/system/lib64",
|
|
|
|
#else
|
2012-10-31 22:20:03 +01:00
|
|
|
"/vendor/lib",
|
|
|
|
"/system/lib",
|
2013-10-08 23:27:10 +02:00
|
|
|
#endif
|
2014-08-29 21:02:36 +02:00
|
|
|
nullptr
|
2012-10-31 22:20:03 +01:00
|
|
|
};
|
|
|
|
|
2015-07-11 02:54:01 +02:00
|
|
|
static const char* const kAsanDefaultLdPaths[] = {
|
|
|
|
#if defined(__LP64__)
|
|
|
|
"/data/vendor/lib64",
|
|
|
|
"/vendor/lib64",
|
|
|
|
"/data/lib64",
|
|
|
|
"/system/lib64",
|
|
|
|
#else
|
|
|
|
"/data/vendor/lib",
|
|
|
|
"/vendor/lib",
|
|
|
|
"/data/lib",
|
|
|
|
"/system/lib",
|
|
|
|
#endif
|
|
|
|
nullptr
|
|
|
|
};
|
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
static const ElfW(Versym) kVersymNotNeeded = 0;
|
|
|
|
static const ElfW(Versym) kVersymGlobal = 1;
|
2015-07-16 13:52:06 +02:00
|
|
|
static const char* const kZipFileSeparator = "!/";
|
2015-04-09 22:42:33 +02:00
|
|
|
|
2015-07-11 02:54:01 +02:00
|
|
|
static const char* const* g_default_ld_paths;
|
2015-03-24 02:43:02 +01:00
|
|
|
static std::vector<std::string> g_ld_library_paths;
|
|
|
|
static std::vector<std::string> g_ld_preload_names;
|
2014-01-14 01:37:47 +01:00
|
|
|
|
2015-03-24 02:43:02 +01:00
|
|
|
static std::vector<soinfo*> g_ld_preloads;
|
2009-12-31 19:09:10 +01:00
|
|
|
|
2014-05-14 19:02:03 +02:00
|
|
|
__LIBC_HIDDEN__ int g_ld_debug_verbosity;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-08-29 21:02:36 +02:00
|
|
|
__LIBC_HIDDEN__ abort_msg_t* g_abort_message = nullptr; // For debuggerd.
|
2013-04-04 22:46:46 +02:00
|
|
|
|
2015-06-10 22:38:39 +02:00
|
|
|
static std::string dirname(const char *path) {
|
|
|
|
const char* last_slash = strrchr(path, '/');
|
|
|
|
if (last_slash == path) return "/";
|
|
|
|
else if (last_slash == nullptr) return ".";
|
|
|
|
else
|
|
|
|
return std::string(path, last_slash - path);
|
|
|
|
}
|
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
#if STATS
|
2012-08-14 23:07:59 +02:00
|
|
|
struct linker_stats_t {
|
2014-09-12 18:43:13 +02:00
|
|
|
int count[kRelocMax];
|
2012-08-14 23:07:59 +02:00
|
|
|
};
|
|
|
|
|
|
|
|
static linker_stats_t linker_stats;
|
|
|
|
|
2015-01-14 20:36:38 +01:00
|
|
|
void count_relocation(RelocationKind kind) {
|
2014-09-12 18:43:13 +02:00
|
|
|
++linker_stats.count[kind];
|
2012-08-14 23:07:59 +02:00
|
|
|
}
|
|
|
|
#else
|
2015-01-14 20:36:38 +01:00
|
|
|
void count_relocation(RelocationKind) {
|
2012-08-14 23:07:59 +02:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if COUNT_PAGES
|
2015-01-14 20:36:38 +01:00
|
|
|
uint32_t bitmask[4096];
|
2009-03-04 04:28:35 +01:00
|
|
|
#endif
|
|
|
|
|
2009-05-21 03:28:09 +02:00
|
|
|
static char __linker_dl_err_buf[768];
|
|
|
|
|
2013-03-06 03:47:58 +01:00
|
|
|
char* linker_get_error_buffer() {
|
2012-10-17 00:54:46 +02:00
|
|
|
return &__linker_dl_err_buf[0];
|
2009-05-21 03:28:09 +02:00
|
|
|
}
|
|
|
|
|
2013-03-06 03:47:58 +01:00
|
|
|
size_t linker_get_error_buffer_size() {
|
|
|
|
return sizeof(__linker_dl_err_buf);
|
|
|
|
}
|
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
// This function is an empty stub where GDB locates a breakpoint to get notified
|
|
|
|
// about linker activity.
|
2015-03-31 03:43:38 +02:00
|
|
|
extern "C"
|
|
|
|
void __attribute__((noinline)) __attribute__((visibility("default"))) rtld_db_dlactivity();
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-05-14 19:02:03 +02:00
|
|
|
static pthread_mutex_t g__r_debug_mutex = PTHREAD_MUTEX_INITIALIZER;
|
2015-03-31 03:43:38 +02:00
|
|
|
static r_debug _r_debug =
|
|
|
|
{1, nullptr, reinterpret_cast<uintptr_t>(&rtld_db_dlactivity), r_debug::RT_CONSISTENT, 0};
|
|
|
|
|
2014-02-10 22:31:13 +01:00
|
|
|
static link_map* r_debug_tail = 0;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-02-10 22:31:13 +01:00
|
|
|
static void insert_soinfo_into_debug_map(soinfo* info) {
|
2014-09-12 18:43:13 +02:00
|
|
|
// Copy the necessary fields into the debug structure.
|
|
|
|
link_map* map = &(info->link_map_head);
|
|
|
|
map->l_addr = info->load_bias;
|
2015-03-31 20:14:03 +02:00
|
|
|
// link_map l_name field is not const.
|
|
|
|
map->l_name = const_cast<char*>(info->get_realpath());
|
2014-09-12 18:43:13 +02:00
|
|
|
map->l_ld = info->dynamic;
|
|
|
|
|
|
|
|
// Stick the new library at the end of the list.
|
|
|
|
// gdb tends to care more about libc than it does
|
|
|
|
// about leaf libraries, and ordering it this way
|
|
|
|
// reduces the back-and-forth over the wire.
|
|
|
|
if (r_debug_tail) {
|
|
|
|
r_debug_tail->l_next = map;
|
|
|
|
map->l_prev = r_debug_tail;
|
|
|
|
map->l_next = 0;
|
|
|
|
} else {
|
|
|
|
_r_debug.r_map = map;
|
|
|
|
map->l_prev = 0;
|
|
|
|
map->l_next = 0;
|
|
|
|
}
|
|
|
|
r_debug_tail = map;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2012-08-14 23:07:59 +02:00
|
|
|
static void remove_soinfo_from_debug_map(soinfo* info) {
|
2014-09-12 18:43:13 +02:00
|
|
|
link_map* map = &(info->link_map_head);
|
2009-03-25 03:02:00 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
if (r_debug_tail == map) {
|
|
|
|
r_debug_tail = map->l_prev;
|
|
|
|
}
|
2009-03-25 03:02:00 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
if (map->l_prev) {
|
|
|
|
map->l_prev->l_next = map->l_next;
|
|
|
|
}
|
|
|
|
if (map->l_next) {
|
|
|
|
map->l_next->l_prev = map->l_prev;
|
|
|
|
}
|
2009-03-25 03:02:00 +01:00
|
|
|
}
|
|
|
|
|
2012-08-14 23:07:59 +02:00
|
|
|
static void notify_gdb_of_load(soinfo* info) {
|
2014-11-29 22:57:41 +01:00
|
|
|
if (info->is_main_executable()) {
|
2014-09-12 18:43:13 +02:00
|
|
|
// GDB already knows about the main executable
|
|
|
|
return;
|
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
_r_debug.r_state = r_debug::RT_ADD;
|
|
|
|
rtld_db_dlactivity();
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
insert_soinfo_into_debug_map(info);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
_r_debug.r_state = r_debug::RT_CONSISTENT;
|
|
|
|
rtld_db_dlactivity();
|
2009-03-25 03:02:00 +01:00
|
|
|
}
|
|
|
|
|
2012-08-14 23:07:59 +02:00
|
|
|
static void notify_gdb_of_unload(soinfo* info) {
|
2014-11-29 22:57:41 +01:00
|
|
|
if (info->is_main_executable()) {
|
2014-09-12 18:43:13 +02:00
|
|
|
// GDB already knows about the main executable
|
|
|
|
return;
|
|
|
|
}
|
2009-03-25 03:02:00 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
ScopedPthreadMutexLocker locker(&g__r_debug_mutex);
|
2009-03-25 03:02:00 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
_r_debug.r_state = r_debug::RT_DELETE;
|
|
|
|
rtld_db_dlactivity();
|
2009-03-25 03:02:00 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
remove_soinfo_from_debug_map(info);
|
2009-03-25 03:02:00 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
_r_debug.r_state = r_debug::RT_CONSISTENT;
|
|
|
|
rtld_db_dlactivity();
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2012-10-30 01:37:13 +01:00
|
|
|
void notify_gdb_of_libraries() {
|
2014-02-10 22:31:13 +01:00
|
|
|
_r_debug.r_state = r_debug::RT_ADD;
|
|
|
|
rtld_db_dlactivity();
|
|
|
|
_r_debug.r_state = r_debug::RT_CONSISTENT;
|
|
|
|
rtld_db_dlactivity();
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
LinkedListEntry<soinfo>* SoinfoListAllocator::alloc() {
|
|
|
|
return g_soinfo_links_allocator.alloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
void SoinfoListAllocator::free(LinkedListEntry<soinfo>* entry) {
|
|
|
|
g_soinfo_links_allocator.free(entry);
|
|
|
|
}
|
|
|
|
|
2015-03-31 03:43:38 +02:00
|
|
|
static soinfo* soinfo_alloc(const char* name, struct stat* file_stat,
|
|
|
|
off64_t file_offset, uint32_t rtld_flags) {
|
2015-03-31 20:14:03 +02:00
|
|
|
if (strlen(name) >= PATH_MAX) {
|
2012-09-12 13:00:55 +02:00
|
|
|
DL_ERR("library name \"%s\" too long", name);
|
2014-08-29 21:02:36 +02:00
|
|
|
return nullptr;
|
2012-09-12 13:00:55 +02:00
|
|
|
}
|
|
|
|
|
2014-10-04 02:52:44 +02:00
|
|
|
soinfo* si = new (g_soinfo_allocator.alloc()) soinfo(name, file_stat, file_offset, rtld_flags);
|
2014-05-09 18:10:14 +02:00
|
|
|
|
2012-09-12 13:00:55 +02:00
|
|
|
sonext->next = si;
|
|
|
|
sonext = si;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2013-03-12 18:40:45 +01:00
|
|
|
TRACE("name %s: allocated soinfo @ %p", name, si);
|
2012-09-12 13:00:55 +02:00
|
|
|
return si;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2014-02-12 01:59:37 +01:00
|
|
|
static void soinfo_free(soinfo* si) {
|
2014-09-12 18:43:13 +02:00
|
|
|
if (si == nullptr) {
|
|
|
|
return;
|
|
|
|
}
|
2012-08-04 01:49:39 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
if (si->base != 0 && si->size != 0) {
|
|
|
|
munmap(reinterpret_cast<void*>(si->base), si->size);
|
|
|
|
}
|
2014-05-09 18:10:14 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
soinfo *prev = nullptr, *trav;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-05-07 19:48:00 +02:00
|
|
|
TRACE("name %s: freeing soinfo @ %p", si->get_realpath(), si);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
for (trav = solist; trav != nullptr; trav = trav->next) {
|
|
|
|
if (trav == si) {
|
|
|
|
break;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
prev = trav;
|
|
|
|
}
|
2014-11-10 04:27:20 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
if (trav == nullptr) {
|
|
|
|
// si was not in solist
|
2015-05-07 19:48:00 +02:00
|
|
|
DL_ERR("name \"%s\"@%p is not in solist!", si->get_realpath(), si);
|
2014-09-12 18:43:13 +02:00
|
|
|
return;
|
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
// clear links to/from si
|
|
|
|
si->remove_all_links();
|
2014-05-09 18:10:14 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
// prev will never be null, because the first entry in solist is
|
|
|
|
// always the static libdl_info.
|
|
|
|
prev->next = si->next;
|
|
|
|
if (si == sonext) {
|
|
|
|
sonext = prev;
|
|
|
|
}
|
2014-05-06 01:49:04 +02:00
|
|
|
|
2015-07-09 00:26:46 +02:00
|
|
|
si->~soinfo();
|
2014-09-12 18:43:13 +02:00
|
|
|
g_soinfo_allocator.free(si);
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2012-12-20 23:42:14 +01:00
|
|
|
static void parse_path(const char* path, const char* delimiters,
|
2015-03-24 02:43:02 +01:00
|
|
|
std::vector<std::string>* paths) {
|
|
|
|
paths->clear();
|
2015-07-29 12:00:22 +02:00
|
|
|
if (path != nullptr) {
|
|
|
|
*paths = android::base::Split(path, delimiters);
|
2012-12-20 23:42:14 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static void parse_LD_LIBRARY_PATH(const char* path) {
|
2015-03-24 02:43:02 +01:00
|
|
|
parse_path(path, ":", &g_ld_library_paths);
|
2012-12-20 23:42:14 +01:00
|
|
|
}
|
|
|
|
|
2015-06-10 22:38:39 +02:00
|
|
|
void soinfo::set_dt_runpath(const char* path) {
|
|
|
|
if (!has_min_version(2)) return;
|
|
|
|
parse_path(path, ":", &dt_runpath_);
|
|
|
|
|
|
|
|
std::string origin = dirname(get_realpath());
|
|
|
|
// FIXME: add $LIB and $PLATFORM.
|
|
|
|
std::pair<std::string, std::string> substs[] = {{"ORIGIN", origin}};
|
2015-10-13 21:14:16 +02:00
|
|
|
for (auto&& s : dt_runpath_) {
|
2015-06-10 22:38:39 +02:00
|
|
|
size_t pos = 0;
|
|
|
|
while (pos < s.size()) {
|
|
|
|
pos = s.find("$", pos);
|
|
|
|
if (pos == std::string::npos) break;
|
|
|
|
for (const auto& subst : substs) {
|
|
|
|
const std::string& token = subst.first;
|
|
|
|
const std::string& replacement = subst.second;
|
|
|
|
if (s.substr(pos + 1, token.size()) == token) {
|
|
|
|
s.replace(pos, token.size() + 1, replacement);
|
|
|
|
// -1 to compensate for the ++pos below.
|
|
|
|
pos += replacement.size() - 1;
|
|
|
|
break;
|
|
|
|
} else if (s.substr(pos + 1, token.size() + 2) == "{" + token + "}") {
|
|
|
|
s.replace(pos, token.size() + 3, replacement);
|
|
|
|
pos += replacement.size() - 1;
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
// Skip $ in case it did not match any of the known substitutions.
|
|
|
|
++pos;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-12-20 23:42:14 +01:00
|
|
|
static void parse_LD_PRELOAD(const char* path) {
|
|
|
|
// We have historically supported ':' as well as ' ' in LD_PRELOAD.
|
2015-03-24 02:43:02 +01:00
|
|
|
parse_path(path, " :", &g_ld_preload_names);
|
2012-12-20 23:42:14 +01:00
|
|
|
}
|
|
|
|
|
2015-03-31 20:14:03 +02:00
|
|
|
static bool realpath_fd(int fd, std::string* realpath) {
|
|
|
|
std::vector<char> buf(PATH_MAX), proc_self_fd(PATH_MAX);
|
2015-10-02 03:41:57 +02:00
|
|
|
__libc_format_buffer(&proc_self_fd[0], proc_self_fd.size(), "/proc/self/fd/%d", fd);
|
2015-10-14 20:15:45 +02:00
|
|
|
// set DUMPABLE to 1 to access /proc/self/fd
|
|
|
|
int dumpable = prctl(PR_GET_DUMPABLE, 0, 0, 0, 0);
|
|
|
|
prctl(PR_SET_DUMPABLE, 1, 0, 0, 0);
|
|
|
|
auto guard = make_scope_guard([&]() {
|
|
|
|
// restore dumpable
|
|
|
|
prctl(PR_SET_DUMPABLE, dumpable, 0, 0, 0);
|
|
|
|
});
|
2015-03-31 20:14:03 +02:00
|
|
|
if (readlink(&proc_self_fd[0], &buf[0], buf.size()) == -1) {
|
2015-05-28 20:44:31 +02:00
|
|
|
PRINT("readlink('%s') failed: %s [fd=%d]", &proc_self_fd[0], strerror(errno), fd);
|
2015-03-31 20:14:03 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-10-02 03:41:57 +02:00
|
|
|
*realpath = &buf[0];
|
2015-03-31 20:14:03 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2013-10-26 02:38:02 +02:00
|
|
|
#if defined(__arm__)
|
2012-08-04 01:49:39 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
// For a given PC, find the .so that it belongs to.
|
|
|
|
// Returns the base address of the .ARM.exidx section
|
|
|
|
// for that .so, and the number of 8-byte entries
|
|
|
|
// in that section (via *pcount).
|
|
|
|
//
|
|
|
|
// Intended to be called by libc's __gnu_Unwind_Find_exidx().
|
|
|
|
//
|
|
|
|
// This function is exposed via dlfcn.cpp and libdl.so.
|
2014-02-12 01:59:37 +01:00
|
|
|
_Unwind_Ptr dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount) {
|
2015-01-23 01:04:25 +01:00
|
|
|
uintptr_t addr = reinterpret_cast<uintptr_t>(pc);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
for (soinfo* si = solist; si != 0; si = si->next) {
|
|
|
|
if ((addr >= si->base) && (addr < (si->base + si->size))) {
|
|
|
|
*pcount = si->ARM_exidx_count;
|
2015-01-23 01:04:25 +01:00
|
|
|
return reinterpret_cast<_Unwind_Ptr>(si->ARM_exidx);
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
|
|
|
*pcount = 0;
|
|
|
|
return nullptr;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2012-08-04 01:49:39 +02:00
|
|
|
|
2013-08-20 02:45:09 +02:00
|
|
|
#endif
|
2012-08-04 01:49:39 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
// Here, we only have to provide a callback to iterate across all the
|
|
|
|
// loaded libraries. gcc_eh does the rest.
|
2015-06-29 23:48:25 +02:00
|
|
|
int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data) {
|
2014-09-12 18:43:13 +02:00
|
|
|
int rv = 0;
|
|
|
|
for (soinfo* si = solist; si != nullptr; si = si->next) {
|
|
|
|
dl_phdr_info dl_info;
|
|
|
|
dl_info.dlpi_addr = si->link_map_head.l_addr;
|
|
|
|
dl_info.dlpi_name = si->link_map_head.l_name;
|
|
|
|
dl_info.dlpi_phdr = si->phdr;
|
|
|
|
dl_info.dlpi_phnum = si->phnum;
|
|
|
|
rv = cb(&dl_info, sizeof(dl_phdr_info), data);
|
|
|
|
if (rv != 0) {
|
|
|
|
break;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
|
|
|
return rv;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2012-08-04 01:49:39 +02:00
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
const ElfW(Versym)* soinfo::get_versym(size_t n) const {
|
|
|
|
if (has_min_version(2) && versym_ != nullptr) {
|
|
|
|
return versym_ + n;
|
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ElfW(Addr) soinfo::get_verneed_ptr() const {
|
|
|
|
if (has_min_version(2)) {
|
|
|
|
return verneed_ptr_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t soinfo::get_verneed_cnt() const {
|
|
|
|
if (has_min_version(2)) {
|
|
|
|
return verneed_cnt_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
ElfW(Addr) soinfo::get_verdef_ptr() const {
|
|
|
|
if (has_min_version(2)) {
|
|
|
|
return verdef_ptr_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t soinfo::get_verdef_cnt() const {
|
|
|
|
if (has_min_version(2)) {
|
|
|
|
return verdef_cnt_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
template<typename F>
|
|
|
|
static bool for_each_verdef(const soinfo* si, F functor) {
|
|
|
|
if (!si->has_min_version(2)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
uintptr_t verdef_ptr = si->get_verdef_ptr();
|
|
|
|
if (verdef_ptr == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t offset = 0;
|
|
|
|
|
|
|
|
size_t verdef_cnt = si->get_verdef_cnt();
|
|
|
|
for (size_t i = 0; i<verdef_cnt; ++i) {
|
|
|
|
const ElfW(Verdef)* verdef = reinterpret_cast<ElfW(Verdef)*>(verdef_ptr + offset);
|
|
|
|
size_t verdaux_offset = offset + verdef->vd_aux;
|
|
|
|
offset += verdef->vd_next;
|
|
|
|
|
|
|
|
if (verdef->vd_version != 1) {
|
2015-04-21 02:40:39 +02:00
|
|
|
DL_ERR("unsupported verdef[%zd] vd_version: %d (expected 1) library: %s",
|
2015-05-07 19:48:00 +02:00
|
|
|
i, verdef->vd_version, si->get_realpath());
|
2015-04-09 22:42:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if ((verdef->vd_flags & VER_FLG_BASE) != 0) {
|
|
|
|
// "this is the version of the file itself. It must not be used for
|
|
|
|
// matching a symbol. It can be used to match references."
|
|
|
|
//
|
|
|
|
// http://www.akkadia.org/drepper/symbol-versioning
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (verdef->vd_cnt == 0) {
|
|
|
|
DL_ERR("invalid verdef[%zd] vd_cnt == 0 (version without a name)", i);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const ElfW(Verdaux)* verdaux = reinterpret_cast<ElfW(Verdaux)*>(verdef_ptr + verdaux_offset);
|
|
|
|
|
|
|
|
if (functor(i, verdef, verdaux) == true) {
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool soinfo::find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const {
|
|
|
|
if (vi == nullptr) {
|
|
|
|
*versym = kVersymNotNeeded;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
*versym = kVersymGlobal;
|
|
|
|
|
|
|
|
return for_each_verdef(this,
|
|
|
|
[&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
|
|
|
|
if (verdef->vd_hash == vi->elf_hash &&
|
|
|
|
strcmp(vi->name, get_string(verdaux->vda_name)) == 0) {
|
|
|
|
*versym = verdef->vd_ndx;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool soinfo::find_symbol_by_name(SymbolName& symbol_name,
|
|
|
|
const version_info* vi,
|
|
|
|
const ElfW(Sym)** symbol) const {
|
|
|
|
uint32_t symbol_index;
|
|
|
|
bool success =
|
|
|
|
is_gnu_hash() ?
|
|
|
|
gnu_lookup(symbol_name, vi, &symbol_index) :
|
|
|
|
elf_lookup(symbol_name, vi, &symbol_index);
|
|
|
|
|
|
|
|
if (success) {
|
|
|
|
*symbol = symbol_index == 0 ? nullptr : symtab_ + symbol_index;
|
|
|
|
}
|
|
|
|
|
|
|
|
return success;
|
2014-11-10 04:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
static bool is_symbol_global_and_defined(const soinfo* si, const ElfW(Sym)* s) {
|
|
|
|
if (ELF_ST_BIND(s->st_info) == STB_GLOBAL ||
|
|
|
|
ELF_ST_BIND(s->st_info) == STB_WEAK) {
|
|
|
|
return s->st_shndx != SHN_UNDEF;
|
|
|
|
} else if (ELF_ST_BIND(s->st_info) != STB_LOCAL) {
|
|
|
|
DL_WARN("unexpected ST_BIND value: %d for '%s' in '%s'",
|
2015-05-07 19:48:00 +02:00
|
|
|
ELF_ST_BIND(s->st_info), si->get_string(s->st_name), si->get_realpath());
|
2014-11-10 04:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
static const ElfW(Versym) kVersymHiddenBit = 0x8000;
|
|
|
|
|
|
|
|
static inline bool is_versym_hidden(const ElfW(Versym)* versym) {
|
|
|
|
// the symbol is hidden if bit 15 of versym is set.
|
|
|
|
return versym != nullptr && (*versym & kVersymHiddenBit) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
static inline bool check_symbol_version(const ElfW(Versym) verneed,
|
|
|
|
const ElfW(Versym)* verdef) {
|
|
|
|
return verneed == kVersymNotNeeded ||
|
|
|
|
verdef == nullptr ||
|
|
|
|
verneed == (*verdef & ~kVersymHiddenBit);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool soinfo::gnu_lookup(SymbolName& symbol_name,
|
|
|
|
const version_info* vi,
|
|
|
|
uint32_t* symbol_index) const {
|
2014-11-10 04:27:20 +01:00
|
|
|
uint32_t hash = symbol_name.gnu_hash();
|
2014-11-13 18:39:20 +01:00
|
|
|
uint32_t h2 = hash >> gnu_shift2_;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-11-10 04:27:20 +01:00
|
|
|
uint32_t bloom_mask_bits = sizeof(ElfW(Addr))*8;
|
2014-11-13 18:39:20 +01:00
|
|
|
uint32_t word_num = (hash / bloom_mask_bits) & gnu_maskwords_;
|
|
|
|
ElfW(Addr) bloom_word = gnu_bloom_filter_[word_num];
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
*symbol_index = 0;
|
|
|
|
|
2015-03-09 20:02:02 +01:00
|
|
|
TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p (gnu)",
|
2015-05-07 19:48:00 +02:00
|
|
|
symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
|
2015-03-09 20:02:02 +01:00
|
|
|
|
2014-11-10 04:27:20 +01:00
|
|
|
// test against bloom filter
|
|
|
|
if ((1 & (bloom_word >> (hash % bloom_mask_bits)) & (bloom_word >> (h2 % bloom_mask_bits))) == 0) {
|
2015-03-09 20:02:02 +01:00
|
|
|
TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
|
2015-05-07 19:48:00 +02:00
|
|
|
symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
|
2015-03-09 20:02:02 +01:00
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
return true;
|
2014-11-10 04:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// bloom test says "probably yes"...
|
2015-03-09 20:02:02 +01:00
|
|
|
uint32_t n = gnu_bucket_[hash % gnu_nbucket_];
|
2014-11-10 04:27:20 +01:00
|
|
|
|
|
|
|
if (n == 0) {
|
2015-03-09 20:02:02 +01:00
|
|
|
TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
|
2015-05-07 19:48:00 +02:00
|
|
|
symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
|
2015-03-09 20:02:02 +01:00
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
// lookup versym for the version definition in this library
|
|
|
|
// note the difference between "version is not requested" (vi == nullptr)
|
|
|
|
// and "version not found". In the first case verneed is kVersymNotNeeded
|
|
|
|
// which implies that the default version can be accepted; the second case results in
|
|
|
|
// verneed = 1 (kVersymGlobal) and implies that we should ignore versioned symbols
|
|
|
|
// for this library and consider only *global* ones.
|
|
|
|
ElfW(Versym) verneed = 0;
|
|
|
|
if (!find_verdef_version_index(vi, &verneed)) {
|
|
|
|
return false;
|
2014-11-10 04:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2014-11-13 18:39:20 +01:00
|
|
|
ElfW(Sym)* s = symtab_ + n;
|
2015-04-09 22:42:33 +02:00
|
|
|
const ElfW(Versym)* verdef = get_versym(n);
|
|
|
|
// skip hidden versions when verneed == kVersymNotNeeded (0)
|
|
|
|
if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
|
|
|
|
continue;
|
|
|
|
}
|
2015-03-09 20:02:02 +01:00
|
|
|
if (((gnu_chain_[n] ^ hash) >> 1) == 0 &&
|
2015-04-09 22:42:33 +02:00
|
|
|
check_symbol_version(verneed, verdef) &&
|
2014-11-10 04:27:20 +01:00
|
|
|
strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
|
|
|
|
is_symbol_global_and_defined(this, s)) {
|
2015-03-09 20:02:02 +01:00
|
|
|
TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
|
2015-05-07 19:48:00 +02:00
|
|
|
symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(s->st_value),
|
2015-03-09 20:02:02 +01:00
|
|
|
static_cast<size_t>(s->st_size));
|
2015-04-09 22:42:33 +02:00
|
|
|
*symbol_index = n;
|
|
|
|
return true;
|
2014-11-10 04:27:20 +01:00
|
|
|
}
|
2015-03-09 20:02:02 +01:00
|
|
|
} while ((gnu_chain_[n++] & 1) == 0);
|
|
|
|
|
|
|
|
TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p",
|
2015-05-07 19:48:00 +02:00
|
|
|
symbol_name.get_name(), get_realpath(), reinterpret_cast<void*>(base));
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
return true;
|
2014-11-10 04:27:20 +01:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
bool soinfo::elf_lookup(SymbolName& symbol_name,
|
|
|
|
const version_info* vi,
|
|
|
|
uint32_t* symbol_index) const {
|
2014-11-10 04:27:20 +01:00
|
|
|
uint32_t hash = symbol_name.elf_hash();
|
|
|
|
|
|
|
|
TRACE_TYPE(LOOKUP, "SEARCH %s in %s@%p h=%x(elf) %zd",
|
2015-05-07 19:48:00 +02:00
|
|
|
symbol_name.get_name(), get_realpath(),
|
2015-03-31 20:14:03 +02:00
|
|
|
reinterpret_cast<void*>(base), hash, hash % nbucket_);
|
2014-11-10 04:27:20 +01:00
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
ElfW(Versym) verneed = 0;
|
|
|
|
if (!find_verdef_version_index(vi, &verneed)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
for (uint32_t n = bucket_[hash % nbucket_]; n != 0; n = chain_[n]) {
|
|
|
|
ElfW(Sym)* s = symtab_ + n;
|
2015-04-09 22:42:33 +02:00
|
|
|
const ElfW(Versym)* verdef = get_versym(n);
|
|
|
|
|
|
|
|
// skip hidden versions when verneed == 0
|
|
|
|
if (verneed == kVersymNotNeeded && is_versym_hidden(verdef)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (check_symbol_version(verneed, verdef) &&
|
|
|
|
strcmp(get_string(s->st_name), symbol_name.get_name()) == 0 &&
|
2015-03-31 03:43:38 +02:00
|
|
|
is_symbol_global_and_defined(this, s)) {
|
2014-11-10 04:27:20 +01:00
|
|
|
TRACE_TYPE(LOOKUP, "FOUND %s in %s (%p) %zd",
|
2015-05-07 19:48:00 +02:00
|
|
|
symbol_name.get_name(), get_realpath(),
|
2015-03-31 20:14:03 +02:00
|
|
|
reinterpret_cast<void*>(s->st_value),
|
|
|
|
static_cast<size_t>(s->st_size));
|
2015-04-09 22:42:33 +02:00
|
|
|
*symbol_index = n;
|
|
|
|
return true;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-02-11 02:46:57 +01:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-07-29 02:32:20 +02:00
|
|
|
TRACE_TYPE(LOOKUP, "NOT FOUND %s in %s@%p %x %zd",
|
2015-05-07 19:48:00 +02:00
|
|
|
symbol_name.get_name(), get_realpath(),
|
2015-03-31 20:14:03 +02:00
|
|
|
reinterpret_cast<void*>(base), hash, hash % nbucket_);
|
2014-07-29 02:32:20 +02:00
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
*symbol_index = 0;
|
|
|
|
return true;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2015-03-31 20:14:03 +02:00
|
|
|
soinfo::soinfo(const char* realpath, const struct stat* file_stat,
|
2015-03-31 03:43:38 +02:00
|
|
|
off64_t file_offset, int rtld_flags) {
|
2014-08-22 21:25:04 +02:00
|
|
|
memset(this, 0, sizeof(*this));
|
|
|
|
|
2015-03-31 20:14:03 +02:00
|
|
|
if (realpath != nullptr) {
|
|
|
|
realpath_ = realpath;
|
|
|
|
}
|
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
flags_ = FLAG_NEW_SOINFO;
|
2014-11-13 18:39:20 +01:00
|
|
|
version_ = SOINFO_VERSION;
|
2014-08-22 21:25:04 +02:00
|
|
|
|
2014-08-29 21:02:36 +02:00
|
|
|
if (file_stat != nullptr) {
|
2014-11-13 18:39:20 +01:00
|
|
|
this->st_dev_ = file_stat->st_dev;
|
|
|
|
this->st_ino_ = file_stat->st_ino;
|
|
|
|
this->file_offset_ = file_offset;
|
2014-08-22 21:25:04 +02:00
|
|
|
}
|
2014-09-16 02:00:10 +02:00
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
this->rtld_flags_ = rtld_flags;
|
2014-08-22 21:25:04 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
|
2014-11-10 04:27:20 +01:00
|
|
|
uint32_t SymbolName::elf_hash() {
|
|
|
|
if (!has_elf_hash_) {
|
2015-03-31 20:14:03 +02:00
|
|
|
const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
|
2014-11-10 04:27:20 +01:00
|
|
|
uint32_t h = 0, g;
|
|
|
|
|
|
|
|
while (*name) {
|
|
|
|
h = (h << 4) + *name++;
|
|
|
|
g = h & 0xf0000000;
|
|
|
|
h ^= g;
|
|
|
|
h ^= g >> 24;
|
|
|
|
}
|
|
|
|
|
|
|
|
elf_hash_ = h;
|
|
|
|
has_elf_hash_ = true;
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
2014-11-10 04:27:20 +01:00
|
|
|
|
|
|
|
return elf_hash_;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t SymbolName::gnu_hash() {
|
|
|
|
if (!has_gnu_hash_) {
|
|
|
|
uint32_t h = 5381;
|
2015-03-31 20:14:03 +02:00
|
|
|
const uint8_t* name = reinterpret_cast<const uint8_t*>(name_);
|
2014-11-10 04:27:20 +01:00
|
|
|
while (*name != 0) {
|
|
|
|
h += (h << 5) + *name++; // h*33 + c = h + h * 32 + c = h + h << 5 + c
|
|
|
|
}
|
|
|
|
|
|
|
|
gnu_hash_ = h;
|
|
|
|
has_gnu_hash_ = true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return gnu_hash_;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
|
|
|
|
soinfo** si_found_in, const soinfo::soinfo_list_t& global_group,
|
|
|
|
const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol) {
|
2014-11-10 04:27:20 +01:00
|
|
|
SymbolName symbol_name(name);
|
2015-04-09 22:42:33 +02:00
|
|
|
const ElfW(Sym)* s = nullptr;
|
2009-09-29 04:38:04 +02:00
|
|
|
|
2014-09-29 21:10:36 +02:00
|
|
|
/* "This element's presence in a shared object library alters the dynamic linker's
|
|
|
|
* symbol resolution algorithm for references within the library. Instead of starting
|
|
|
|
* a symbol search with the executable file, the dynamic linker starts from the shared
|
|
|
|
* object itself. If the shared object fails to supply the referenced symbol, the
|
|
|
|
* dynamic linker then searches the executable file and other shared objects as usual."
|
|
|
|
*
|
|
|
|
* http://www.sco.com/developers/gabi/2012-12-31/ch5.dynamic.html
|
|
|
|
*
|
|
|
|
* Note that this is unlikely since static linker avoids generating
|
|
|
|
* relocations for -Bsymbolic linked dynamic executables.
|
|
|
|
*/
|
2014-08-28 23:12:12 +02:00
|
|
|
if (si_from->has_DT_SYMBOLIC) {
|
2015-05-07 19:48:00 +02:00
|
|
|
DEBUG("%s: looking up %s in local scope (DT_SYMBOLIC)", si_from->get_realpath(), name);
|
2015-04-09 22:42:33 +02:00
|
|
|
if (!si_from->find_symbol_by_name(symbol_name, vi, &s)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-16 23:31:06 +02:00
|
|
|
if (s != nullptr) {
|
2014-08-28 23:12:12 +02:00
|
|
|
*si_found_in = si_from;
|
2014-09-29 21:10:36 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
// 1. Look for it in global_group
|
|
|
|
if (s == nullptr) {
|
2015-04-09 22:42:33 +02:00
|
|
|
bool error = false;
|
2014-08-28 23:12:12 +02:00
|
|
|
global_group.visit([&](soinfo* global_si) {
|
2015-03-31 20:14:03 +02:00
|
|
|
DEBUG("%s: looking up %s in %s (from global group)",
|
2015-05-07 19:48:00 +02:00
|
|
|
si_from->get_realpath(), name, global_si->get_realpath());
|
2015-04-09 22:42:33 +02:00
|
|
|
if (!global_si->find_symbol_by_name(symbol_name, vi, &s)) {
|
|
|
|
error = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-09-29 21:10:36 +02:00
|
|
|
if (s != nullptr) {
|
2014-08-28 23:12:12 +02:00
|
|
|
*si_found_in = global_si;
|
|
|
|
return false;
|
2014-09-29 21:10:36 +02:00
|
|
|
}
|
2012-10-31 10:55:51 +01:00
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
return true;
|
|
|
|
});
|
2015-04-09 22:42:33 +02:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-09-29 21:10:36 +02:00
|
|
|
}
|
2012-10-31 10:55:51 +01:00
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
// 2. Look for it in the local group
|
2014-10-21 18:23:18 +02:00
|
|
|
if (s == nullptr) {
|
2015-04-09 22:42:33 +02:00
|
|
|
bool error = false;
|
2014-10-21 18:23:18 +02:00
|
|
|
local_group.visit([&](soinfo* local_si) {
|
2014-08-28 23:12:12 +02:00
|
|
|
if (local_si == si_from && si_from->has_DT_SYMBOLIC) {
|
2014-10-23 23:19:07 +02:00
|
|
|
// we already did this - skip
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-03-31 20:14:03 +02:00
|
|
|
DEBUG("%s: looking up %s in %s (from local group)",
|
2015-05-07 19:48:00 +02:00
|
|
|
si_from->get_realpath(), name, local_si->get_realpath());
|
2015-04-09 22:42:33 +02:00
|
|
|
if (!local_si->find_symbol_by_name(symbol_name, vi, &s)) {
|
|
|
|
error = true;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
if (s != nullptr) {
|
2014-08-28 23:12:12 +02:00
|
|
|
*si_found_in = local_si;
|
2014-10-21 18:23:18 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
return true;
|
|
|
|
});
|
2015-04-09 22:42:33 +02:00
|
|
|
|
|
|
|
if (error) {
|
|
|
|
return false;
|
|
|
|
}
|
2014-10-21 18:23:18 +02:00
|
|
|
}
|
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
if (s != nullptr) {
|
|
|
|
TRACE_TYPE(LOOKUP, "si %s sym %s s->st_value = %p, "
|
|
|
|
"found in %s, base = %p, load bias = %p",
|
2015-05-07 19:48:00 +02:00
|
|
|
si_from->get_realpath(), name, reinterpret_cast<void*>(s->st_value),
|
|
|
|
(*si_found_in)->get_realpath(), reinterpret_cast<void*>((*si_found_in)->base),
|
2014-08-28 23:12:12 +02:00
|
|
|
reinterpret_cast<void*>((*si_found_in)->load_bias));
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
*symbol = s;
|
|
|
|
return true;
|
2009-09-29 04:38:04 +02:00
|
|
|
}
|
|
|
|
|
2015-01-23 21:03:53 +01:00
|
|
|
class ProtectedDataGuard {
|
|
|
|
public:
|
|
|
|
ProtectedDataGuard() {
|
|
|
|
if (ref_count_++ == 0) {
|
|
|
|
protect_data(PROT_READ | PROT_WRITE);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
~ProtectedDataGuard() {
|
|
|
|
if (ref_count_ == 0) { // overflow
|
|
|
|
__libc_fatal("Too many nested calls to dlopen()");
|
|
|
|
}
|
|
|
|
|
|
|
|
if (--ref_count_ == 0) {
|
|
|
|
protect_data(PROT_READ);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
void protect_data(int protection) {
|
|
|
|
g_soinfo_allocator.protect_all(protection);
|
|
|
|
g_soinfo_links_allocator.protect_all(protection);
|
|
|
|
}
|
|
|
|
|
|
|
|
static size_t ref_count_;
|
|
|
|
};
|
|
|
|
|
|
|
|
size_t ProtectedDataGuard::ref_count_ = 0;
|
|
|
|
|
2014-09-02 01:15:52 +02:00
|
|
|
// Each size has it's own allocator.
|
|
|
|
template<size_t size>
|
|
|
|
class SizeBasedAllocator {
|
|
|
|
public:
|
|
|
|
static void* alloc() {
|
|
|
|
return allocator_.alloc();
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free(void* ptr) {
|
|
|
|
allocator_.free(ptr);
|
|
|
|
}
|
2014-08-29 23:01:48 +02:00
|
|
|
|
2014-09-02 01:15:52 +02:00
|
|
|
private:
|
|
|
|
static LinkerBlockAllocator allocator_;
|
|
|
|
};
|
|
|
|
|
|
|
|
template<size_t size>
|
|
|
|
LinkerBlockAllocator SizeBasedAllocator<size>::allocator_(size);
|
|
|
|
|
|
|
|
template<typename T>
|
|
|
|
class TypeBasedAllocator {
|
|
|
|
public:
|
|
|
|
static T* alloc() {
|
|
|
|
return reinterpret_cast<T*>(SizeBasedAllocator<sizeof(T)>::alloc());
|
|
|
|
}
|
|
|
|
|
|
|
|
static void free(T* ptr) {
|
|
|
|
SizeBasedAllocator<sizeof(T)>::free(ptr);
|
|
|
|
}
|
|
|
|
};
|
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
class LoadTask {
|
|
|
|
public:
|
|
|
|
struct deleter_t {
|
|
|
|
void operator()(LoadTask* t) {
|
|
|
|
TypeBasedAllocator<LoadTask>::free(t);
|
|
|
|
}
|
|
|
|
};
|
2014-07-29 23:21:45 +02:00
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
typedef UniquePtr<LoadTask, deleter_t> unique_ptr;
|
2013-03-01 00:58:45 +01:00
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
static deleter_t deleter;
|
|
|
|
|
|
|
|
static LoadTask* create(const char* name, soinfo* needed_by) {
|
|
|
|
LoadTask* ptr = TypeBasedAllocator<LoadTask>::alloc();
|
|
|
|
return new (ptr) LoadTask(name, needed_by);
|
2014-07-29 02:32:20 +02:00
|
|
|
}
|
2014-07-29 23:21:45 +02:00
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
const char* get_name() const {
|
|
|
|
return name_;
|
2014-07-29 23:21:45 +02:00
|
|
|
}
|
2014-09-06 01:42:53 +02:00
|
|
|
|
|
|
|
soinfo* get_needed_by() const {
|
|
|
|
return needed_by_;
|
|
|
|
}
|
|
|
|
private:
|
|
|
|
LoadTask(const char* name, soinfo* needed_by)
|
|
|
|
: name_(name), needed_by_(needed_by) {}
|
|
|
|
|
|
|
|
const char* name_;
|
|
|
|
soinfo* needed_by_;
|
|
|
|
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(LoadTask);
|
2014-07-29 02:32:20 +02:00
|
|
|
};
|
|
|
|
|
2014-09-16 09:22:10 +02:00
|
|
|
LoadTask::deleter_t LoadTask::deleter;
|
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
template <typename T>
|
|
|
|
using linked_list_t = LinkedList<T, TypeBasedAllocator<LinkedListEntry<T>>>;
|
|
|
|
|
|
|
|
typedef linked_list_t<soinfo> SoinfoLinkedList;
|
|
|
|
typedef linked_list_t<const char> StringLinkedList;
|
|
|
|
typedef linked_list_t<LoadTask> LoadTaskList;
|
|
|
|
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
// This function walks down the tree of soinfo dependencies
|
|
|
|
// in breadth-first order and
|
|
|
|
// * calls action(soinfo* si) for each node, and
|
|
|
|
// * terminates walk if action returns false.
|
|
|
|
//
|
|
|
|
// walk_dependencies_tree returns false if walk was terminated
|
|
|
|
// by the action and true otherwise.
|
|
|
|
template<typename F>
|
|
|
|
static bool walk_dependencies_tree(soinfo* root_soinfos[], size_t root_soinfos_size, F action) {
|
2014-09-02 01:15:52 +02:00
|
|
|
SoinfoLinkedList visit_list;
|
|
|
|
SoinfoLinkedList visited;
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
for (size_t i = 0; i < root_soinfos_size; ++i) {
|
|
|
|
visit_list.push_back(root_soinfos[i]);
|
|
|
|
}
|
|
|
|
|
|
|
|
soinfo* si;
|
|
|
|
while ((si = visit_list.pop_front()) != nullptr) {
|
|
|
|
if (visited.contains(si)) {
|
2014-08-13 06:02:13 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
if (!action(si)) {
|
|
|
|
return false;
|
2014-07-29 02:32:20 +02:00
|
|
|
}
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
visited.push_back(si);
|
|
|
|
|
|
|
|
si->get_children().for_each([&](soinfo* child) {
|
2014-07-29 02:32:20 +02:00
|
|
|
visit_list.push_back(child);
|
|
|
|
});
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
|
2015-05-12 20:12:27 +02:00
|
|
|
static const ElfW(Sym)* dlsym_handle_lookup(soinfo* root, soinfo* skip_until,
|
|
|
|
soinfo** found, SymbolName& symbol_name) {
|
2015-04-09 22:42:33 +02:00
|
|
|
const ElfW(Sym)* result = nullptr;
|
2015-05-12 20:12:27 +02:00
|
|
|
bool skip_lookup = skip_until != nullptr;
|
|
|
|
|
|
|
|
walk_dependencies_tree(&root, 1, [&](soinfo* current_soinfo) {
|
|
|
|
if (skip_lookup) {
|
|
|
|
skip_lookup = current_soinfo != skip_until;
|
|
|
|
return true;
|
|
|
|
}
|
2014-10-21 18:23:18 +02:00
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
if (!current_soinfo->find_symbol_by_name(symbol_name, nullptr, &result)) {
|
|
|
|
result = nullptr;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
if (result != nullptr) {
|
|
|
|
*found = current_soinfo;
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
return result;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2015-05-12 20:12:27 +02:00
|
|
|
// This is used by dlsym(3). It performs symbol lookup only within the
|
|
|
|
// specified soinfo object and its dependencies in breadth first order.
|
|
|
|
const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name) {
|
2015-05-30 22:04:39 +02:00
|
|
|
// According to man dlopen(3) and posix docs in the case when si is handle
|
|
|
|
// of the main executable we need to search not only in the executable and its
|
|
|
|
// dependencies but also in all libraries loaded with RTLD_GLOBAL.
|
|
|
|
//
|
|
|
|
// Since RTLD_GLOBAL is always set for the main executable and all dt_needed shared
|
|
|
|
// libraries and they are loaded in breath-first (correct) order we can just execute
|
|
|
|
// dlsym(RTLD_DEFAULT, ...); instead of doing two stage lookup.
|
|
|
|
if (si == somain) {
|
|
|
|
return dlsym_linear_lookup(name, found, nullptr, RTLD_DEFAULT);
|
|
|
|
}
|
|
|
|
|
2015-05-12 20:12:27 +02:00
|
|
|
SymbolName symbol_name(name);
|
|
|
|
return dlsym_handle_lookup(si, nullptr, found, symbol_name);
|
|
|
|
}
|
|
|
|
|
2013-03-01 00:58:45 +01:00
|
|
|
/* This is used by dlsym(3) to performs a global symbol lookup. If the
|
|
|
|
start value is null (for RTLD_DEFAULT), the search starts at the
|
|
|
|
beginning of the global solist. Otherwise the search starts at the
|
|
|
|
specified soinfo (for RTLD_NEXT).
|
2009-09-29 04:38:04 +02:00
|
|
|
*/
|
2015-04-09 22:42:33 +02:00
|
|
|
const ElfW(Sym)* dlsym_linear_lookup(const char* name,
|
|
|
|
soinfo** found,
|
|
|
|
soinfo* caller,
|
|
|
|
void* handle) {
|
2014-11-10 04:27:20 +01:00
|
|
|
SymbolName symbol_name(name);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-04-01 23:45:10 +02:00
|
|
|
soinfo* start = solist;
|
|
|
|
|
|
|
|
if (handle == RTLD_NEXT) {
|
2015-05-22 21:34:42 +02:00
|
|
|
if (caller == nullptr) {
|
2015-04-01 23:45:10 +02:00
|
|
|
return nullptr;
|
|
|
|
} else {
|
|
|
|
start = caller->next;
|
|
|
|
}
|
2012-12-20 23:42:14 +01:00
|
|
|
}
|
2009-12-31 19:17:56 +01:00
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
const ElfW(Sym)* s = nullptr;
|
2015-04-01 23:45:10 +02:00
|
|
|
for (soinfo* si = start; si != nullptr; si = si->next) {
|
2015-06-03 02:36:54 +02:00
|
|
|
// Do not skip RTLD_LOCAL libraries in dlsym(RTLD_DEFAULT, ...)
|
|
|
|
// if the library is opened by application with target api level <= 22
|
|
|
|
// See http://b/21565766
|
|
|
|
if ((si->get_rtld_flags() & RTLD_GLOBAL) == 0 && si->get_target_sdk_version() > 22) {
|
2014-09-16 02:00:10 +02:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
if (!si->find_symbol_by_name(symbol_name, nullptr, &s)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2014-08-29 21:02:36 +02:00
|
|
|
if (s != nullptr) {
|
2012-12-20 23:42:14 +01:00
|
|
|
*found = si;
|
|
|
|
break;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2012-12-20 23:42:14 +01:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-05-12 20:12:27 +02:00
|
|
|
// If not found - use dlsym_handle_lookup for caller's
|
|
|
|
// local_group unless it is part of the global group in which
|
2015-04-01 23:45:10 +02:00
|
|
|
// case we already did it.
|
|
|
|
if (s == nullptr && caller != nullptr &&
|
|
|
|
(caller->get_rtld_flags() & RTLD_GLOBAL) == 0) {
|
2015-05-12 20:12:27 +02:00
|
|
|
return dlsym_handle_lookup(caller->get_local_group_root(),
|
|
|
|
(handle == RTLD_NEXT) ? caller : nullptr, found, symbol_name);
|
2015-04-01 23:45:10 +02:00
|
|
|
}
|
|
|
|
|
2014-08-29 21:02:36 +02:00
|
|
|
if (s != nullptr) {
|
2013-10-05 02:01:33 +02:00
|
|
|
TRACE_TYPE(LOOKUP, "%s s->st_value = %p, found->base = %p",
|
|
|
|
name, reinterpret_cast<void*>(s->st_value), reinterpret_cast<void*>((*found)->base));
|
2012-12-20 23:42:14 +01:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-12-20 23:42:14 +01:00
|
|
|
return s;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2013-03-12 07:58:06 +01:00
|
|
|
soinfo* find_containing_library(const void* p) {
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Addr) address = reinterpret_cast<ElfW(Addr)>(p);
|
2014-08-29 21:02:36 +02:00
|
|
|
for (soinfo* si = solist; si != nullptr; si = si->next) {
|
2013-03-12 07:58:06 +01:00
|
|
|
if (address >= si->base && address - si->base < si->size) {
|
|
|
|
return si;
|
2009-12-31 19:17:40 +01:00
|
|
|
}
|
2013-03-12 07:58:06 +01:00
|
|
|
}
|
2014-08-29 21:02:36 +02:00
|
|
|
return nullptr;
|
2009-12-31 19:17:40 +01:00
|
|
|
}
|
|
|
|
|
2014-11-10 04:27:20 +01:00
|
|
|
ElfW(Sym)* soinfo::find_symbol_by_address(const void* addr) {
|
|
|
|
return is_gnu_hash() ? gnu_addr_lookup(addr) : elf_addr_lookup(addr);
|
|
|
|
}
|
|
|
|
|
|
|
|
static bool symbol_matches_soaddr(const ElfW(Sym)* sym, ElfW(Addr) soaddr) {
|
|
|
|
return sym->st_shndx != SHN_UNDEF &&
|
|
|
|
soaddr >= sym->st_value &&
|
|
|
|
soaddr < sym->st_value + sym->st_size;
|
|
|
|
}
|
|
|
|
|
|
|
|
ElfW(Sym)* soinfo::gnu_addr_lookup(const void* addr) {
|
2013-11-14 02:22:33 +01:00
|
|
|
ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
|
2014-11-10 04:27:20 +01:00
|
|
|
|
2015-03-09 20:02:02 +01:00
|
|
|
for (size_t i = 0; i < gnu_nbucket_; ++i) {
|
|
|
|
uint32_t n = gnu_bucket_[i];
|
2014-11-10 04:27:20 +01:00
|
|
|
|
|
|
|
if (n == 0) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
do {
|
2014-11-13 18:39:20 +01:00
|
|
|
ElfW(Sym)* sym = symtab_ + n;
|
2014-11-10 04:27:20 +01:00
|
|
|
if (symbol_matches_soaddr(sym, soaddr)) {
|
|
|
|
return sym;
|
|
|
|
}
|
2015-03-09 20:02:02 +01:00
|
|
|
} while ((gnu_chain_[n++] & 1) == 0);
|
2014-11-10 04:27:20 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
ElfW(Sym)* soinfo::elf_addr_lookup(const void* addr) {
|
2013-11-14 02:22:33 +01:00
|
|
|
ElfW(Addr) soaddr = reinterpret_cast<ElfW(Addr)>(addr) - load_bias;
|
2013-03-12 07:58:06 +01:00
|
|
|
|
|
|
|
// Search the library's symbol table for any defined symbol which
|
|
|
|
// contains this address.
|
2014-11-13 18:39:20 +01:00
|
|
|
for (size_t i = 0; i < nchain_; ++i) {
|
|
|
|
ElfW(Sym)* sym = symtab_ + i;
|
2014-11-10 04:27:20 +01:00
|
|
|
if (symbol_matches_soaddr(sym, soaddr)) {
|
2013-03-12 07:58:06 +01:00
|
|
|
return sym;
|
2009-12-31 19:17:40 +01:00
|
|
|
}
|
2013-03-12 07:58:06 +01:00
|
|
|
}
|
2009-12-31 19:17:40 +01:00
|
|
|
|
2014-08-29 21:02:36 +02:00
|
|
|
return nullptr;
|
2009-12-31 19:17:40 +01:00
|
|
|
}
|
|
|
|
|
2015-09-29 01:38:31 +02:00
|
|
|
class ZipArchiveCache {
|
|
|
|
public:
|
|
|
|
ZipArchiveCache() {}
|
|
|
|
~ZipArchiveCache();
|
|
|
|
|
|
|
|
bool get_or_open(const char* zip_path, ZipArchiveHandle* handle);
|
|
|
|
private:
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(ZipArchiveCache);
|
|
|
|
|
|
|
|
std::unordered_map<std::string, ZipArchiveHandle> cache_;
|
|
|
|
};
|
|
|
|
|
|
|
|
bool ZipArchiveCache::get_or_open(const char* zip_path, ZipArchiveHandle* handle) {
|
|
|
|
std::string key(zip_path);
|
|
|
|
|
|
|
|
auto it = cache_.find(key);
|
|
|
|
if (it != cache_.end()) {
|
|
|
|
*handle = it->second;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
|
|
|
|
if (fd == -1) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
if (OpenArchiveFd(fd, "", handle) != 0) {
|
|
|
|
// invalid zip-file (?)
|
|
|
|
close(fd);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
cache_[key] = *handle;
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZipArchiveCache::~ZipArchiveCache() {
|
2015-10-13 21:14:16 +02:00
|
|
|
for (const auto& it : cache_) {
|
2015-09-29 01:38:31 +02:00
|
|
|
CloseArchive(it.second);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
static int open_library_in_zipfile(ZipArchiveCache* zip_archive_cache,
|
2015-10-02 03:41:57 +02:00
|
|
|
const char* const input_path,
|
|
|
|
off64_t* file_offset, std::string* realpath) {
|
|
|
|
std::string normalized_path;
|
|
|
|
if (!normalize_path(input_path, &normalized_path)) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* const path = normalized_path.c_str();
|
|
|
|
TRACE("Trying zip file open from path '%s' -> normalized '%s'", input_path, path);
|
2015-01-16 14:22:54 +01:00
|
|
|
|
2015-06-09 22:46:51 +02:00
|
|
|
// Treat an '!/' separator inside a path as the separator between the name
|
2015-01-16 14:22:54 +01:00
|
|
|
// of the zip file on disk and the subdirectory to search within it.
|
2015-06-09 22:46:51 +02:00
|
|
|
// For example, if path is "foo.zip!/bar/bas/x.so", then we search for
|
2015-01-16 14:22:54 +01:00
|
|
|
// "bar/bas/x.so" within "foo.zip".
|
2015-10-02 03:41:57 +02:00
|
|
|
const char* const separator = strstr(path, kZipFileSeparator);
|
2015-01-16 14:22:54 +01:00
|
|
|
if (separator == nullptr) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
char buf[512];
|
|
|
|
if (strlcpy(buf, path, sizeof(buf)) >= sizeof(buf)) {
|
|
|
|
PRINT("Warning: ignoring very long library path: %s", path);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
buf[separator - path] = '\0';
|
|
|
|
|
|
|
|
const char* zip_path = buf;
|
2015-06-09 22:46:51 +02:00
|
|
|
const char* file_path = &buf[separator - path + 2];
|
2015-01-16 14:22:54 +01:00
|
|
|
int fd = TEMP_FAILURE_RETRY(open(zip_path, O_RDONLY | O_CLOEXEC));
|
|
|
|
if (fd == -1) {
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZipArchiveHandle handle;
|
2015-09-29 01:38:31 +02:00
|
|
|
if (!zip_archive_cache->get_or_open(zip_path, &handle)) {
|
2015-01-16 14:22:54 +01:00
|
|
|
// invalid zip-file (?)
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
ZipEntry entry;
|
|
|
|
|
2015-06-25 23:56:07 +02:00
|
|
|
if (FindEntry(handle, ZipString(file_path), &entry) != 0) {
|
2015-01-16 14:22:54 +01:00
|
|
|
// Entry was not found.
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if it is properly stored
|
|
|
|
if (entry.method != kCompressStored || (entry.offset % PAGE_SIZE) != 0) {
|
|
|
|
close(fd);
|
|
|
|
return -1;
|
|
|
|
}
|
|
|
|
|
|
|
|
*file_offset = entry.offset;
|
2015-10-02 03:41:57 +02:00
|
|
|
|
|
|
|
if (realpath_fd(fd, realpath)) {
|
|
|
|
*realpath += separator;
|
|
|
|
} else {
|
|
|
|
PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.",
|
|
|
|
normalized_path.c_str());
|
|
|
|
*realpath = normalized_path;
|
|
|
|
}
|
|
|
|
|
2015-01-16 14:22:54 +01:00
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
|
2015-03-24 02:43:02 +01:00
|
|
|
static bool format_path(char* buf, size_t buf_size, const char* path, const char* name) {
|
|
|
|
int n = __libc_format_buffer(buf, buf_size, "%s/%s", path, name);
|
|
|
|
if (n < 0 || n >= static_cast<int>(buf_size)) {
|
|
|
|
PRINT("Warning: ignoring very long library path: %s/%s", path, name);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
2015-01-16 14:22:54 +01:00
|
|
|
|
2015-10-02 03:41:57 +02:00
|
|
|
static int open_library_on_default_path(const char* name, off64_t* file_offset, std::string* realpath) {
|
2015-07-11 02:54:01 +02:00
|
|
|
for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
|
2015-03-24 02:43:02 +01:00
|
|
|
char buf[512];
|
2015-07-11 02:54:01 +02:00
|
|
|
if (!format_path(buf, sizeof(buf), g_default_ld_paths[i], name)) {
|
2015-03-24 02:43:02 +01:00
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
|
|
|
|
if (fd != -1) {
|
|
|
|
*file_offset = 0;
|
2015-10-02 03:41:57 +02:00
|
|
|
if (!realpath_fd(fd, realpath)) {
|
|
|
|
PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
|
|
|
|
*realpath = buf;
|
|
|
|
}
|
2015-03-24 02:43:02 +01:00
|
|
|
return fd;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2015-03-24 02:43:02 +01:00
|
|
|
}
|
2015-01-16 14:22:54 +01:00
|
|
|
|
2015-03-24 02:43:02 +01:00
|
|
|
return -1;
|
|
|
|
}
|
2015-01-16 14:22:54 +01:00
|
|
|
|
2015-09-29 01:38:31 +02:00
|
|
|
static int open_library_on_paths(ZipArchiveCache* zip_archive_cache,
|
|
|
|
const char* name, off64_t* file_offset,
|
2015-10-02 03:41:57 +02:00
|
|
|
const std::vector<std::string>& paths,
|
|
|
|
std::string* realpath) {
|
2015-06-10 22:38:39 +02:00
|
|
|
for (const auto& path_str : paths) {
|
2015-03-24 02:43:02 +01:00
|
|
|
char buf[512];
|
|
|
|
const char* const path = path_str.c_str();
|
|
|
|
if (!format_path(buf, sizeof(buf), path, name)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
|
|
|
int fd = -1;
|
2015-07-16 13:52:06 +02:00
|
|
|
if (strstr(buf, kZipFileSeparator) != nullptr) {
|
2015-10-02 03:41:57 +02:00
|
|
|
fd = open_library_in_zipfile(zip_archive_cache, buf, file_offset, realpath);
|
2015-01-16 14:22:54 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
if (fd == -1) {
|
|
|
|
fd = TEMP_FAILURE_RETRY(open(buf, O_RDONLY | O_CLOEXEC));
|
|
|
|
if (fd != -1) {
|
|
|
|
*file_offset = 0;
|
2015-10-02 03:41:57 +02:00
|
|
|
if (!realpath_fd(fd, realpath)) {
|
|
|
|
PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", buf);
|
|
|
|
*realpath = buf;
|
|
|
|
}
|
2015-01-16 14:22:54 +01:00
|
|
|
}
|
2012-10-31 22:20:03 +01:00
|
|
|
}
|
2015-03-24 02:43:02 +01:00
|
|
|
|
|
|
|
if (fd != -1) {
|
|
|
|
return fd;
|
|
|
|
}
|
2012-10-31 22:20:03 +01:00
|
|
|
}
|
2015-01-16 14:22:54 +01:00
|
|
|
|
2015-03-24 02:43:02 +01:00
|
|
|
return -1;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2015-09-29 01:38:31 +02:00
|
|
|
static int open_library(ZipArchiveCache* zip_archive_cache,
|
|
|
|
const char* name, soinfo *needed_by,
|
2015-10-02 03:41:57 +02:00
|
|
|
off64_t* file_offset, std::string* realpath) {
|
2013-03-12 18:40:45 +01:00
|
|
|
TRACE("[ opening %s ]", name);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-10-31 22:20:03 +01:00
|
|
|
// If the name contains a slash, we should attempt to open it directly and not search the paths.
|
2014-08-29 21:02:36 +02:00
|
|
|
if (strchr(name, '/') != nullptr) {
|
2015-07-16 13:52:06 +02:00
|
|
|
if (strstr(name, kZipFileSeparator) != nullptr) {
|
2015-10-02 03:41:57 +02:00
|
|
|
int fd = open_library_in_zipfile(zip_archive_cache, name, file_offset, realpath);
|
2015-01-16 14:22:54 +01:00
|
|
|
if (fd != -1) {
|
|
|
|
return fd;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2012-11-02 06:59:19 +01:00
|
|
|
int fd = TEMP_FAILURE_RETRY(open(name, O_RDONLY | O_CLOEXEC));
|
|
|
|
if (fd != -1) {
|
2015-01-16 14:22:54 +01:00
|
|
|
*file_offset = 0;
|
2015-10-13 16:43:00 +02:00
|
|
|
if (!realpath_fd(fd, realpath)) {
|
|
|
|
PRINT("warning: unable to get realpath for the library \"%s\". Will use given path.", name);
|
|
|
|
*realpath = name;
|
|
|
|
}
|
2012-11-02 06:59:19 +01:00
|
|
|
}
|
2015-03-18 01:12:18 +01:00
|
|
|
return fd;
|
2012-10-31 22:20:03 +01:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-10-31 22:20:03 +01:00
|
|
|
// Otherwise we try LD_LIBRARY_PATH first, and fall back to the built-in well known paths.
|
2015-10-02 03:41:57 +02:00
|
|
|
int fd = open_library_on_paths(zip_archive_cache, name, file_offset, g_ld_library_paths, realpath);
|
2015-06-10 22:38:39 +02:00
|
|
|
if (fd == -1 && needed_by) {
|
2015-10-02 03:41:57 +02:00
|
|
|
fd = open_library_on_paths(zip_archive_cache, name, file_offset, needed_by->get_dt_runpath(), realpath);
|
2015-06-10 22:38:39 +02:00
|
|
|
}
|
2015-09-29 01:38:31 +02:00
|
|
|
|
2012-10-31 22:20:03 +01:00
|
|
|
if (fd == -1) {
|
2015-10-02 03:41:57 +02:00
|
|
|
fd = open_library_on_default_path(name, file_offset, realpath);
|
2012-10-31 22:20:03 +01:00
|
|
|
}
|
2015-09-29 01:38:31 +02:00
|
|
|
|
2012-10-31 22:20:03 +01:00
|
|
|
return fd;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2015-05-28 03:29:41 +02:00
|
|
|
static const char* fix_dt_needed(const char* dt_needed, const char* sopath __unused) {
|
|
|
|
#if !defined(__LP64__)
|
|
|
|
// Work around incorrect DT_NEEDED entries for old apps: http://b/21364029
|
2015-06-03 02:36:54 +02:00
|
|
|
if (get_application_target_sdk_version() <= 22) {
|
2015-05-28 03:29:41 +02:00
|
|
|
const char* bname = basename(dt_needed);
|
|
|
|
if (bname != dt_needed) {
|
|
|
|
DL_WARN("'%s' library has invalid DT_NEEDED entry '%s'", sopath, dt_needed);
|
|
|
|
}
|
|
|
|
|
|
|
|
return bname;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
return dt_needed;
|
|
|
|
}
|
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
template<typename F>
|
|
|
|
static void for_each_dt_needed(const soinfo* si, F action) {
|
|
|
|
for (ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
|
|
|
|
if (d->d_tag == DT_NEEDED) {
|
2015-05-28 03:29:41 +02:00
|
|
|
action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
2014-09-06 01:42:53 +02:00
|
|
|
}
|
|
|
|
}
|
2014-05-09 18:10:14 +02:00
|
|
|
|
2015-04-23 03:06:51 +02:00
|
|
|
static soinfo* load_library(int fd, off64_t file_offset,
|
|
|
|
LoadTaskList& load_tasks,
|
2015-01-16 14:22:54 +01:00
|
|
|
const char* name, int rtld_flags,
|
2015-10-02 03:41:57 +02:00
|
|
|
const android_dlextinfo* extinfo,
|
|
|
|
const std::string& realpath) {
|
2014-10-04 02:52:44 +02:00
|
|
|
if ((file_offset % PAGE_SIZE) != 0) {
|
2014-10-21 21:09:18 +02:00
|
|
|
DL_ERR("file offset for the library \"%s\" is not page-aligned: %" PRId64, name, file_offset);
|
2014-10-04 02:52:44 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-11-04 20:08:05 +01:00
|
|
|
if (file_offset < 0) {
|
|
|
|
DL_ERR("file offset for the library \"%s\" is negative: %" PRId64, name, file_offset);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-10-04 02:52:44 +02:00
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
struct stat file_stat;
|
|
|
|
if (TEMP_FAILURE_RETRY(fstat(fd, &file_stat)) != 0) {
|
2014-10-21 21:09:18 +02:00
|
|
|
DL_ERR("unable to stat file for the library \"%s\": %s", name, strerror(errno));
|
2014-09-06 01:42:53 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-11-04 20:08:05 +01:00
|
|
|
if (file_offset >= file_stat.st_size) {
|
2015-03-31 03:43:38 +02:00
|
|
|
DL_ERR("file offset for the library \"%s\" >= file size: %" PRId64 " >= %" PRId64,
|
|
|
|
name, file_offset, file_stat.st_size);
|
2014-11-04 20:08:05 +01:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-05-09 18:10:14 +02:00
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
// Check for symlink and other situations where
|
2015-04-03 01:03:56 +02:00
|
|
|
// file can have different names, unless ANDROID_DLEXT_FORCE_LOAD is set
|
|
|
|
if (extinfo == nullptr || (extinfo->flags & ANDROID_DLEXT_FORCE_LOAD) == 0) {
|
|
|
|
for (soinfo* si = solist; si != nullptr; si = si->next) {
|
|
|
|
if (si->get_st_dev() != 0 &&
|
|
|
|
si->get_st_ino() != 0 &&
|
|
|
|
si->get_st_dev() == file_stat.st_dev &&
|
|
|
|
si->get_st_ino() == file_stat.st_ino &&
|
|
|
|
si->get_file_offset() == file_offset) {
|
|
|
|
TRACE("library \"%s\" is already loaded under different name/path \"%s\" - "
|
2015-03-31 20:14:03 +02:00
|
|
|
"will return existing soinfo", name, si->get_realpath());
|
2015-04-03 01:03:56 +02:00
|
|
|
return si;
|
|
|
|
}
|
2014-09-05 23:57:59 +02:00
|
|
|
}
|
2014-09-06 01:42:53 +02:00
|
|
|
}
|
2014-05-09 18:10:14 +02:00
|
|
|
|
2014-09-16 02:00:10 +02:00
|
|
|
if ((rtld_flags & RTLD_NOLOAD) != 0) {
|
2014-09-09 19:21:42 +02:00
|
|
|
DL_ERR("library \"%s\" wasn't loaded and RTLD_NOLOAD prevented it", name);
|
2014-09-06 01:42:53 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-29 23:21:45 +02:00
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
// Read the ELF header and load the segments.
|
2015-06-26 00:51:41 +02:00
|
|
|
ElfReader elf_reader(realpath.c_str(), fd, file_offset, file_stat.st_size);
|
2014-09-06 01:42:53 +02:00
|
|
|
if (!elf_reader.Load(extinfo)) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-05-09 18:10:14 +02:00
|
|
|
|
2015-03-31 20:14:03 +02:00
|
|
|
soinfo* si = soinfo_alloc(realpath.c_str(), &file_stat, file_offset, rtld_flags);
|
2014-09-06 01:42:53 +02:00
|
|
|
if (si == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
si->base = elf_reader.load_start();
|
|
|
|
si->size = elf_reader.load_size();
|
|
|
|
si->load_bias = elf_reader.load_bias();
|
|
|
|
si->phnum = elf_reader.phdr_count();
|
|
|
|
si->phdr = elf_reader.loaded_phdr();
|
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
if (!si->prelink_image()) {
|
2014-09-06 01:42:53 +02:00
|
|
|
soinfo_free(si);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2014-07-29 23:21:45 +02:00
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
for_each_dt_needed(si, [&] (const char* name) {
|
|
|
|
load_tasks.push_back(LoadTask::create(name, si));
|
|
|
|
});
|
|
|
|
|
|
|
|
return si;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2015-09-29 01:38:31 +02:00
|
|
|
static soinfo* load_library(ZipArchiveCache* zip_archive_cache,
|
|
|
|
LoadTaskList& load_tasks, const char* name,
|
2015-06-10 22:38:39 +02:00
|
|
|
soinfo* needed_by, int rtld_flags,
|
2015-04-23 03:06:51 +02:00
|
|
|
const android_dlextinfo* extinfo) {
|
2015-10-02 03:41:57 +02:00
|
|
|
off64_t file_offset;
|
|
|
|
std::string realpath;
|
2015-04-23 03:06:51 +02:00
|
|
|
if (extinfo != nullptr && (extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) != 0) {
|
2015-10-02 03:41:57 +02:00
|
|
|
file_offset = 0;
|
2015-04-23 03:06:51 +02:00
|
|
|
if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
|
|
|
|
file_offset = extinfo->library_fd_offset;
|
|
|
|
}
|
2015-10-02 03:41:57 +02:00
|
|
|
|
|
|
|
if (!realpath_fd(extinfo->library_fd, &realpath)) {
|
|
|
|
PRINT("warning: unable to get realpath for the library \"%s\" by extinfo->library_fd. "
|
|
|
|
"Will use given name.", name);
|
|
|
|
realpath = name;
|
|
|
|
}
|
|
|
|
return load_library(extinfo->library_fd, file_offset, load_tasks, name, rtld_flags, extinfo, realpath);
|
2015-04-23 03:06:51 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
// Open the file.
|
2015-10-02 03:41:57 +02:00
|
|
|
int fd = open_library(zip_archive_cache, name, needed_by, &file_offset, &realpath);
|
2015-04-23 03:06:51 +02:00
|
|
|
if (fd == -1) {
|
|
|
|
DL_ERR("library \"%s\" not found", name);
|
|
|
|
return nullptr;
|
|
|
|
}
|
2015-10-02 03:41:57 +02:00
|
|
|
soinfo* result = load_library(fd, file_offset, load_tasks, name, rtld_flags, extinfo, realpath);
|
2015-04-23 03:06:51 +02:00
|
|
|
close(fd);
|
|
|
|
return result;
|
|
|
|
}
|
|
|
|
|
2015-06-17 00:38:21 +02:00
|
|
|
// Returns true if library was found and false in 2 cases
|
|
|
|
// 1. The library was found but loaded under different target_sdk_version
|
|
|
|
// (*candidate != nullptr)
|
|
|
|
// 2. The library was not found by soname (*candidate is nullptr)
|
|
|
|
static bool find_loaded_library_by_soname(const char* name, soinfo** candidate) {
|
|
|
|
*candidate = nullptr;
|
|
|
|
|
2015-03-18 04:06:36 +01:00
|
|
|
// Ignore filename with path.
|
|
|
|
if (strchr(name, '/') != nullptr) {
|
2015-06-17 00:38:21 +02:00
|
|
|
return false;
|
2015-03-18 04:06:36 +01:00
|
|
|
}
|
|
|
|
|
2015-06-17 00:38:21 +02:00
|
|
|
uint32_t target_sdk_version = get_application_target_sdk_version();
|
|
|
|
|
2014-08-29 21:02:36 +02:00
|
|
|
for (soinfo* si = solist; si != nullptr; si = si->next) {
|
2015-03-18 04:06:36 +01:00
|
|
|
const char* soname = si->get_soname();
|
|
|
|
if (soname != nullptr && (strcmp(name, soname) == 0)) {
|
2015-06-17 00:38:21 +02:00
|
|
|
// If the library was opened under different target sdk version
|
|
|
|
// skip this step and try to reopen it. The exceptions are
|
|
|
|
// "libdl.so" and global group. There is no point in skipping
|
|
|
|
// them because relocation process is going to use them
|
|
|
|
// in any case.
|
|
|
|
bool is_libdl = si == solist;
|
|
|
|
if (is_libdl || (si->get_dt_flags_1() & DF_1_GLOBAL) != 0 ||
|
|
|
|
!si->is_linked() || si->get_target_sdk_version() == target_sdk_version) {
|
|
|
|
*candidate = si;
|
|
|
|
return true;
|
|
|
|
} else if (*candidate == nullptr) {
|
|
|
|
// for the different sdk version - remember the first library.
|
|
|
|
*candidate = si;
|
|
|
|
}
|
linker: avoid clobbering the .dynamic section of shared libs
This patch removes the DT_NEEDED hack which stores pointers
to soinfo structs in the .dynamic section of the library
being loaded.
Instead, it caches the soinfo struct pointers on the stack
during relocation time. After relocation time, i.e. when
calling constructors and destructors of the shared library
and its dependencies, uncached access is used instead,
doing lookups using the string table entries pointed to by
the DT_NEEDED entries.
By removing this hack, it is no longer needed to undo the
PT_GNURELRO protection, i.e., all non-writable mappings
can remain non-writable during their entire lifespan.
Even though, strictly speaking, the algorithmic complexity
has increased somewhat, the real-world adverse effect
is negligible on the systems I have tested.
Change-Id: I2361502560b96b5878f7f94a8e8a215350d70d64
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@gmail.com>
2012-08-14 12:30:09 +02:00
|
|
|
}
|
2014-05-20 00:19:52 +02:00
|
|
|
}
|
2015-06-17 00:38:21 +02:00
|
|
|
|
|
|
|
return false;
|
linker: avoid clobbering the .dynamic section of shared libs
This patch removes the DT_NEEDED hack which stores pointers
to soinfo structs in the .dynamic section of the library
being loaded.
Instead, it caches the soinfo struct pointers on the stack
during relocation time. After relocation time, i.e. when
calling constructors and destructors of the shared library
and its dependencies, uncached access is used instead,
doing lookups using the string table entries pointed to by
the DT_NEEDED entries.
By removing this hack, it is no longer needed to undo the
PT_GNURELRO protection, i.e., all non-writable mappings
can remain non-writable during their entire lifespan.
Even though, strictly speaking, the algorithmic complexity
has increased somewhat, the real-world adverse effect
is negligible on the systems I have tested.
Change-Id: I2361502560b96b5878f7f94a8e8a215350d70d64
Signed-off-by: Ard Biesheuvel <ard.biesheuvel@gmail.com>
2012-08-14 12:30:09 +02:00
|
|
|
}
|
|
|
|
|
2015-09-29 01:38:31 +02:00
|
|
|
static soinfo* find_library_internal(ZipArchiveCache* zip_archive_cache,
|
|
|
|
LoadTaskList& load_tasks, const char* name,
|
2015-06-10 22:38:39 +02:00
|
|
|
soinfo* needed_by, int rtld_flags,
|
|
|
|
const android_dlextinfo* extinfo) {
|
2015-06-17 00:38:21 +02:00
|
|
|
soinfo* candidate;
|
|
|
|
|
|
|
|
if (find_loaded_library_by_soname(name, &candidate)) {
|
|
|
|
return candidate;
|
|
|
|
}
|
2014-05-20 00:06:58 +02:00
|
|
|
|
|
|
|
// Library might still be loaded, the accurate detection
|
2014-09-06 01:42:53 +02:00
|
|
|
// of this fact is done by load_library.
|
2015-06-17 00:38:21 +02:00
|
|
|
TRACE("[ '%s' find_loaded_library_by_soname returned false (*candidate=%s@%p). Trying harder...]",
|
|
|
|
name, candidate == nullptr ? "n/a" : candidate->get_realpath(), candidate);
|
|
|
|
|
2015-09-29 01:38:31 +02:00
|
|
|
soinfo* si = load_library(zip_archive_cache, load_tasks, name, needed_by, rtld_flags, extinfo);
|
2015-06-17 00:38:21 +02:00
|
|
|
|
|
|
|
// In case we were unable to load the library but there
|
|
|
|
// is a candidate loaded under the same soname but different
|
|
|
|
// sdk level - return it anyways.
|
|
|
|
if (si == nullptr && candidate != nullptr) {
|
|
|
|
si = candidate;
|
2014-05-20 00:06:58 +02:00
|
|
|
}
|
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
return si;
|
|
|
|
}
|
|
|
|
|
|
|
|
static void soinfo_unload(soinfo* si);
|
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
// TODO: this is slightly unusual way to construct
|
|
|
|
// the global group for relocation. Not every RTLD_GLOBAL
|
|
|
|
// library is included in this group for backwards-compatibility
|
|
|
|
// reasons.
|
|
|
|
//
|
|
|
|
// This group consists of the main executable, LD_PRELOADs
|
|
|
|
// and libraries with the DF_1_GLOBAL flag set.
|
|
|
|
static soinfo::soinfo_list_t make_global_group() {
|
|
|
|
soinfo::soinfo_list_t global_group;
|
|
|
|
for (soinfo* si = somain; si != nullptr; si = si->next) {
|
|
|
|
if ((si->get_dt_flags_1() & DF_1_GLOBAL) != 0) {
|
|
|
|
global_group.push_back(si);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return global_group;
|
|
|
|
}
|
|
|
|
|
2015-07-07 02:56:31 +02:00
|
|
|
// add_as_children - add first-level loaded libraries (i.e. library_names[], but
|
|
|
|
// not their transitive dependencies) as children of the start_with library.
|
|
|
|
// This is false when find_libraries is called for dlopen(), when newly loaded
|
|
|
|
// libraries must form a disjoint tree.
|
|
|
|
static bool find_libraries(soinfo* start_with,
|
|
|
|
const char* const library_names[],
|
|
|
|
size_t library_names_count, soinfo* soinfos[],
|
|
|
|
std::vector<soinfo*>* ld_preloads,
|
|
|
|
size_t ld_preloads_count, int rtld_flags,
|
|
|
|
const android_dlextinfo* extinfo,
|
|
|
|
bool add_as_children) {
|
2014-09-06 01:42:53 +02:00
|
|
|
// Step 0: prepare.
|
|
|
|
LoadTaskList load_tasks;
|
2014-10-21 18:23:18 +02:00
|
|
|
for (size_t i = 0; i < library_names_count; ++i) {
|
2014-09-06 01:42:53 +02:00
|
|
|
const char* name = library_names[i];
|
2014-10-21 18:23:18 +02:00
|
|
|
load_tasks.push_back(LoadTask::create(name, start_with));
|
|
|
|
}
|
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
// Construct global_group.
|
|
|
|
soinfo::soinfo_list_t global_group = make_global_group();
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
// If soinfos array is null allocate one on stack.
|
|
|
|
// The array is needed in case of failure; for example
|
|
|
|
// when library_names[] = {libone.so, libtwo.so} and libone.so
|
|
|
|
// is loaded correctly but libtwo.so failed for some reason.
|
|
|
|
// In this case libone.so should be unloaded on return.
|
|
|
|
// See also implementation of failure_guard below.
|
|
|
|
|
|
|
|
if (soinfos == nullptr) {
|
|
|
|
size_t soinfos_size = sizeof(soinfo*)*library_names_count;
|
|
|
|
soinfos = reinterpret_cast<soinfo**>(alloca(soinfos_size));
|
|
|
|
memset(soinfos, 0, soinfos_size);
|
2014-09-06 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
// list of libraries to link - see step 2.
|
|
|
|
size_t soinfos_count = 0;
|
2014-09-06 01:42:53 +02:00
|
|
|
|
2014-09-09 01:22:22 +02:00
|
|
|
auto failure_guard = make_scope_guard([&]() {
|
2014-09-06 01:42:53 +02:00
|
|
|
// Housekeeping
|
|
|
|
load_tasks.for_each([] (LoadTask* t) {
|
|
|
|
LoadTask::deleter(t);
|
|
|
|
});
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
for (size_t i = 0; i<soinfos_count; ++i) {
|
2014-09-06 01:42:53 +02:00
|
|
|
soinfo_unload(soinfos[i]);
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
2015-09-29 01:38:31 +02:00
|
|
|
ZipArchiveCache zip_archive_cache;
|
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
// Step 1: load and pre-link all DT_NEEDED libraries in breadth first order.
|
2015-03-31 03:43:38 +02:00
|
|
|
for (LoadTask::unique_ptr task(load_tasks.pop_front());
|
|
|
|
task.get() != nullptr; task.reset(load_tasks.pop_front())) {
|
2015-06-10 22:38:39 +02:00
|
|
|
soinfo* needed_by = task->get_needed_by();
|
2015-09-03 01:32:02 +02:00
|
|
|
bool is_dt_needed = needed_by != nullptr && (needed_by != start_with || add_as_children);
|
|
|
|
|
2015-09-29 01:38:31 +02:00
|
|
|
soinfo* si = find_library_internal(&zip_archive_cache, load_tasks,
|
|
|
|
task->get_name(), needed_by, rtld_flags,
|
2015-09-03 01:32:02 +02:00
|
|
|
is_dt_needed ? nullptr : extinfo);
|
2015-09-29 01:38:31 +02:00
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
if (si == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-09-03 01:32:02 +02:00
|
|
|
if (is_dt_needed) {
|
2014-09-06 01:42:53 +02:00
|
|
|
needed_by->add_child(si);
|
|
|
|
}
|
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
if (si->is_linked()) {
|
|
|
|
si->increment_ref_count();
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
// When ld_preloads is not null, the first
|
|
|
|
// ld_preloads_count libs are in fact ld_preloads.
|
|
|
|
if (ld_preloads != nullptr && soinfos_count < ld_preloads_count) {
|
2014-08-28 23:12:12 +02:00
|
|
|
// Add LD_PRELOADed libraries to the global group for future runs.
|
|
|
|
// There is no need to explicitly add them to the global group
|
|
|
|
// for this run because they are going to appear in the local
|
|
|
|
// group in the correct order.
|
|
|
|
si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
|
2015-03-24 02:43:02 +01:00
|
|
|
ld_preloads->push_back(si);
|
2014-09-06 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
if (soinfos_count < library_names_count) {
|
|
|
|
soinfos[soinfos_count++] = si;
|
2014-09-06 01:42:53 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Step 2: link libraries.
|
2014-10-21 18:23:18 +02:00
|
|
|
soinfo::soinfo_list_t local_group;
|
|
|
|
walk_dependencies_tree(
|
2015-07-07 02:56:31 +02:00
|
|
|
(start_with != nullptr && add_as_children) ? &start_with : soinfos,
|
|
|
|
(start_with != nullptr && add_as_children) ? 1 : soinfos_count,
|
2014-10-21 18:23:18 +02:00
|
|
|
[&] (soinfo* si) {
|
|
|
|
local_group.push_back(si);
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
// We need to increment ref_count in case
|
|
|
|
// the root of the local group was not linked.
|
|
|
|
bool was_local_group_root_linked = local_group.front()->is_linked();
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
bool linked = local_group.visit([&](soinfo* si) {
|
2014-11-29 22:57:41 +01:00
|
|
|
if (!si->is_linked()) {
|
2014-11-13 18:39:20 +01:00
|
|
|
if (!si->link_image(global_group, local_group, extinfo)) {
|
2014-09-06 01:42:53 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-29 22:57:41 +01:00
|
|
|
si->set_linked();
|
2014-09-06 01:42:53 +02:00
|
|
|
}
|
2014-10-21 18:23:18 +02:00
|
|
|
|
|
|
|
return true;
|
|
|
|
});
|
|
|
|
|
|
|
|
if (linked) {
|
|
|
|
failure_guard.disable();
|
2014-07-29 23:21:45 +02:00
|
|
|
}
|
2014-09-06 01:42:53 +02:00
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
if (!was_local_group_root_linked) {
|
|
|
|
local_group.front()->increment_ref_count();
|
|
|
|
}
|
|
|
|
|
2014-10-21 18:23:18 +02:00
|
|
|
return linked;
|
2014-09-06 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2015-07-07 02:56:31 +02:00
|
|
|
static soinfo* find_library(const char* name, int rtld_flags,
|
|
|
|
const android_dlextinfo* extinfo,
|
|
|
|
soinfo* needed_by) {
|
2014-09-06 01:42:53 +02:00
|
|
|
soinfo* si;
|
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
if (name == nullptr) {
|
|
|
|
si = somain;
|
2015-07-07 02:56:31 +02:00
|
|
|
} else if (!find_libraries(needed_by, &name, 1, &si, nullptr, 0, rtld_flags,
|
|
|
|
extinfo, /* add_as_children */ false)) {
|
2014-09-06 01:42:53 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
2012-11-01 23:16:56 +01:00
|
|
|
return si;
|
|
|
|
}
|
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
static void soinfo_unload(soinfo* root) {
|
|
|
|
// Note that the library can be loaded but not linked;
|
|
|
|
// in which case there is no root but we still need
|
|
|
|
// to walk the tree and unload soinfos involved.
|
|
|
|
//
|
|
|
|
// This happens on unsuccessful dlopen, when one of
|
|
|
|
// the DT_NEEDED libraries could not be linked/found.
|
|
|
|
if (root->is_linked()) {
|
|
|
|
root = root->get_local_group_root();
|
2014-05-20 00:06:58 +02:00
|
|
|
}
|
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
if (!root->can_unload()) {
|
2015-05-07 19:48:00 +02:00
|
|
|
TRACE("not unloading '%s' - the binary is flagged with NODELETE", root->get_realpath());
|
2014-11-29 22:57:41 +01:00
|
|
|
return;
|
|
|
|
}
|
2012-11-01 23:16:56 +01:00
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
size_t ref_count = root->is_linked() ? root->decrement_ref_count() : 0;
|
|
|
|
|
|
|
|
if (ref_count == 0) {
|
|
|
|
soinfo::soinfo_list_t local_unload_list;
|
|
|
|
soinfo::soinfo_list_t external_unload_list;
|
|
|
|
soinfo::soinfo_list_t depth_first_list;
|
|
|
|
depth_first_list.push_back(root);
|
|
|
|
soinfo* si = nullptr;
|
|
|
|
|
|
|
|
while ((si = depth_first_list.pop_front()) != nullptr) {
|
2014-12-03 02:08:42 +01:00
|
|
|
if (local_unload_list.contains(si)) {
|
|
|
|
continue;
|
|
|
|
}
|
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
local_unload_list.push_back(si);
|
2014-12-03 02:08:42 +01:00
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
if (si->has_min_version(0)) {
|
|
|
|
soinfo* child = nullptr;
|
|
|
|
while ((child = si->get_children().pop_front()) != nullptr) {
|
2015-05-07 19:48:00 +02:00
|
|
|
TRACE("%s@%p needs to unload %s@%p", si->get_realpath(), si,
|
|
|
|
child->get_realpath(), child);
|
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
if (local_unload_list.contains(child)) {
|
|
|
|
continue;
|
2014-12-03 02:08:42 +01:00
|
|
|
} else if (child->is_linked() && child->get_local_group_root() != root) {
|
2014-11-29 22:57:41 +01:00
|
|
|
external_unload_list.push_back(child);
|
|
|
|
} else {
|
|
|
|
depth_first_list.push_front(child);
|
|
|
|
}
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
2014-11-29 22:57:41 +01:00
|
|
|
} else {
|
2015-09-28 19:14:17 +02:00
|
|
|
#if !defined(__work_around_b_24465209__)
|
2015-05-07 19:48:00 +02:00
|
|
|
__libc_fatal("soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
|
2014-12-03 02:08:42 +01:00
|
|
|
#else
|
2015-05-07 19:48:00 +02:00
|
|
|
PRINT("warning: soinfo for \"%s\"@%p has no version", si->get_realpath(), si);
|
2014-11-29 22:57:41 +01:00
|
|
|
for_each_dt_needed(si, [&] (const char* library_name) {
|
2015-03-31 20:14:03 +02:00
|
|
|
TRACE("deprecated (old format of soinfo): %s needs to unload %s",
|
2015-05-07 19:48:00 +02:00
|
|
|
si->get_realpath(), library_name);
|
2015-03-31 20:14:03 +02:00
|
|
|
|
2015-07-07 02:56:31 +02:00
|
|
|
soinfo* needed = find_library(library_name, RTLD_NOLOAD, nullptr, nullptr);
|
2014-11-29 22:57:41 +01:00
|
|
|
if (needed != nullptr) {
|
|
|
|
// Not found: for example if symlink was deleted between dlopen and dlclose
|
|
|
|
// Since we cannot really handle errors at this point - print and continue.
|
2015-03-31 20:14:03 +02:00
|
|
|
PRINT("warning: couldn't find %s needed by %s on unload.",
|
2015-05-07 19:48:00 +02:00
|
|
|
library_name, si->get_realpath());
|
2014-11-29 22:57:41 +01:00
|
|
|
return;
|
|
|
|
} else if (local_unload_list.contains(needed)) {
|
|
|
|
// already visited
|
|
|
|
return;
|
2014-12-03 02:08:42 +01:00
|
|
|
} else if (needed->is_linked() && needed->get_local_group_root() != root) {
|
2014-11-29 22:57:41 +01:00
|
|
|
// external group
|
|
|
|
external_unload_list.push_back(needed);
|
|
|
|
} else {
|
|
|
|
// local group
|
|
|
|
depth_first_list.push_front(needed);
|
|
|
|
}
|
|
|
|
});
|
2014-12-03 02:08:42 +01:00
|
|
|
#endif
|
2014-11-29 22:57:41 +01:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
local_unload_list.for_each([](soinfo* si) {
|
|
|
|
si->call_destructors();
|
|
|
|
});
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
while ((si = local_unload_list.pop_front()) != nullptr) {
|
|
|
|
notify_gdb_of_unload(si);
|
|
|
|
soinfo_free(si);
|
|
|
|
}
|
2014-11-18 21:03:09 +01:00
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
while ((si = external_unload_list.pop_front()) != nullptr) {
|
|
|
|
soinfo_unload(si);
|
|
|
|
}
|
|
|
|
} else {
|
2015-05-07 19:48:00 +02:00
|
|
|
TRACE("not unloading '%s' group, decrementing ref_count to %zd",
|
|
|
|
root->get_realpath(), ref_count);
|
2014-11-18 21:03:09 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2014-01-14 01:37:47 +01:00
|
|
|
void do_android_get_LD_LIBRARY_PATH(char* buffer, size_t buffer_size) {
|
2014-08-27 05:48:11 +02:00
|
|
|
// Use basic string manipulation calls to avoid snprintf.
|
|
|
|
// snprintf indirectly calls pthread_getspecific to get the size of a buffer.
|
|
|
|
// When debug malloc is enabled, this call returns 0. This in turn causes
|
|
|
|
// snprintf to do nothing, which causes libraries to fail to load.
|
|
|
|
// See b/17302493 for further details.
|
|
|
|
// Once the above bug is fixed, this code can be modified to use
|
|
|
|
// snprintf again.
|
2015-07-11 02:54:01 +02:00
|
|
|
size_t required_len = 0;
|
|
|
|
for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
|
|
|
|
required_len += strlen(g_default_ld_paths[i]) + 1;
|
|
|
|
}
|
2014-08-27 05:48:11 +02:00
|
|
|
if (buffer_size < required_len) {
|
2015-03-31 03:43:38 +02:00
|
|
|
__libc_fatal("android_get_LD_LIBRARY_PATH failed, buffer too small: "
|
|
|
|
"buffer len %zu, required len %zu", buffer_size, required_len);
|
2014-08-27 05:48:11 +02:00
|
|
|
}
|
2015-07-11 02:54:01 +02:00
|
|
|
char* end = buffer;
|
|
|
|
for (size_t i = 0; g_default_ld_paths[i] != nullptr; ++i) {
|
|
|
|
if (i > 0) *end++ = ':';
|
|
|
|
end = stpcpy(end, g_default_ld_paths[i]);
|
|
|
|
}
|
2014-01-14 01:37:47 +01:00
|
|
|
}
|
|
|
|
|
2012-12-20 23:42:14 +01:00
|
|
|
void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path) {
|
linker: Allow an app to update it's own LD_LIBRARY_PATH
When the kernel executes a program which is setuid, setgid, has
file capabilities, or causes an SELinux domain transition, the
AT_SECURE flag is set. This flag instructs the dynamic linker to
prune any dangerous environment variables passed across security
boundaries.
For SELinux in particular, whether this flag is set depends on the
the "noatsecure" process permission. If that permission does not
exist, then AT_SECURE=1 whenever a domain transition occurs.
In https://android-review.googlesource.com/129971 , Android stopped
using noatsecure when executing init services. In
https://android-review.googlesource.com/130610 , init was flipped
back into SELinux enforcing mode, making ag/129971 active. The
combination of those two changes ensured that AT_SECURE=1 was
set when executing init spawned services.
In particular, AT_SECURE=1 is set when init executes zygote. Due to
the forking nature of zygote, AT_SECURE remains untouched when
executing zygote's children.
This causes problems for the code added in
https://android-review.googlesource.com/48409 . Specifically, if
AT_SECURE=1, an attempt to call android_update_LD_LIBRARY_PATH()
is silently ignored. This causes problems when art tries to adjust
the LD_LIBRARY_PATH for Android apps. Ultimately, apps are unable
to find shared libraries they depend on.
As discussed in bug 7896159, there's no security reason for
preventing an application from updating it's own LD_LIBRARY_PATH.
We only need to prune LD_LIBRARY_PATH when transitioning across
security boundaries, but not when we're entirely within a security
boundary.
Remove the AT_SECURE check within do_android_update_LD_LIBRARY_PATH().
It's unneeded and prevents an application from modifying it's own
LD_LIBRARY_PATH. This allows an application to specify a location
where it's dlopen()ed shared libraries should be loaded from.
There is no change to AT_SECURE handling in
__sanitize_environment_variables(). We continue to honor it there
to prevent using security sensitive environment variables across
an exec boundary.
Bug: 19559835
Change-Id: If4af2ee8e84265aaa0c93de8b281208b20d7942a
2015-03-07 22:37:05 +01:00
|
|
|
parse_LD_LIBRARY_PATH(ld_library_path);
|
2012-12-20 23:42:14 +01:00
|
|
|
}
|
|
|
|
|
2015-07-07 02:56:31 +02:00
|
|
|
soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo, soinfo *caller) {
|
2014-05-20 00:06:58 +02:00
|
|
|
if ((flags & ~(RTLD_NOW|RTLD_LAZY|RTLD_LOCAL|RTLD_GLOBAL|RTLD_NODELETE|RTLD_NOLOAD)) != 0) {
|
2012-12-19 00:57:55 +01:00
|
|
|
DL_ERR("invalid flags to dlopen: %x", flags);
|
2014-08-29 21:02:36 +02:00
|
|
|
return nullptr;
|
2012-12-19 00:57:55 +01:00
|
|
|
}
|
2014-10-04 02:52:44 +02:00
|
|
|
if (extinfo != nullptr) {
|
|
|
|
if ((extinfo->flags & ~(ANDROID_DLEXT_VALID_FLAG_BITS)) != 0) {
|
|
|
|
DL_ERR("invalid extended flags to android_dlopen_ext: 0x%" PRIx64, extinfo->flags);
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
if ((extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD) == 0 &&
|
2014-10-21 21:09:18 +02:00
|
|
|
(extinfo->flags & ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET) != 0) {
|
2015-03-31 03:43:38 +02:00
|
|
|
DL_ERR("invalid extended flag combination (ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET without "
|
|
|
|
"ANDROID_DLEXT_USE_LIBRARY_FD): 0x%" PRIx64, extinfo->flags);
|
2014-10-04 02:52:44 +02:00
|
|
|
return nullptr;
|
|
|
|
}
|
2014-02-06 15:34:21 +01:00
|
|
|
}
|
2015-01-23 21:03:53 +01:00
|
|
|
|
|
|
|
ProtectedDataGuard guard;
|
2015-07-07 02:56:31 +02:00
|
|
|
soinfo* si = find_library(name, flags, extinfo, caller);
|
2014-08-29 21:02:36 +02:00
|
|
|
if (si != nullptr) {
|
2014-11-13 18:39:20 +01:00
|
|
|
si->call_constructors();
|
2012-11-01 23:16:56 +01:00
|
|
|
}
|
|
|
|
return si;
|
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-05-20 00:06:58 +02:00
|
|
|
void do_dlclose(soinfo* si) {
|
2015-01-23 21:03:53 +01:00
|
|
|
ProtectedDataGuard guard;
|
2014-05-20 00:06:58 +02:00
|
|
|
soinfo_unload(si);
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2014-09-12 00:16:03 +02:00
|
|
|
static ElfW(Addr) call_ifunc_resolver(ElfW(Addr) resolver_addr) {
|
|
|
|
typedef ElfW(Addr) (*ifunc_resolver_t)(void);
|
|
|
|
ifunc_resolver_t ifunc_resolver = reinterpret_cast<ifunc_resolver_t>(resolver_addr);
|
|
|
|
ElfW(Addr) ifunc_addr = ifunc_resolver();
|
2015-03-31 03:43:38 +02:00
|
|
|
TRACE_TYPE(RELO, "Called ifunc_resolver@%p. The result is %p",
|
|
|
|
ifunc_resolver, reinterpret_cast<void*>(ifunc_addr));
|
2014-07-23 20:22:25 +02:00
|
|
|
|
2014-09-12 00:16:03 +02:00
|
|
|
return ifunc_addr;
|
2014-07-23 20:22:25 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
const version_info* VersionTracker::get_version_info(ElfW(Versym) source_symver) const {
|
|
|
|
if (source_symver < 2 ||
|
|
|
|
source_symver >= version_infos.size() ||
|
|
|
|
version_infos[source_symver].name == nullptr) {
|
|
|
|
return nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return &version_infos[source_symver];
|
|
|
|
}
|
|
|
|
|
|
|
|
void VersionTracker::add_version_info(size_t source_index,
|
|
|
|
ElfW(Word) elf_hash,
|
|
|
|
const char* ver_name,
|
|
|
|
const soinfo* target_si) {
|
|
|
|
if (source_index >= version_infos.size()) {
|
|
|
|
version_infos.resize(source_index+1);
|
|
|
|
}
|
|
|
|
|
|
|
|
version_infos[source_index].elf_hash = elf_hash;
|
|
|
|
version_infos[source_index].name = ver_name;
|
|
|
|
version_infos[source_index].target_si = target_si;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VersionTracker::init_verneed(const soinfo* si_from) {
|
|
|
|
uintptr_t verneed_ptr = si_from->get_verneed_ptr();
|
|
|
|
|
|
|
|
if (verneed_ptr == 0) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t verneed_cnt = si_from->get_verneed_cnt();
|
|
|
|
|
|
|
|
for (size_t i = 0, offset = 0; i<verneed_cnt; ++i) {
|
|
|
|
const ElfW(Verneed)* verneed = reinterpret_cast<ElfW(Verneed)*>(verneed_ptr + offset);
|
|
|
|
size_t vernaux_offset = offset + verneed->vn_aux;
|
|
|
|
offset += verneed->vn_next;
|
|
|
|
|
|
|
|
if (verneed->vn_version != 1) {
|
|
|
|
DL_ERR("unsupported verneed[%zd] vn_version: %d (expected 1)", i, verneed->vn_version);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* target_soname = si_from->get_string(verneed->vn_file);
|
|
|
|
// find it in dependencies
|
|
|
|
soinfo* target_si = si_from->get_children().find_if([&](const soinfo* si) {
|
2015-05-06 20:05:27 +02:00
|
|
|
return si->get_soname() != nullptr && strcmp(si->get_soname(), target_soname) == 0;
|
2015-04-09 22:42:33 +02:00
|
|
|
});
|
|
|
|
|
|
|
|
if (target_si == nullptr) {
|
|
|
|
DL_ERR("cannot find \"%s\" from verneed[%zd] in DT_NEEDED list for \"%s\"",
|
2015-05-07 19:48:00 +02:00
|
|
|
target_soname, i, si_from->get_realpath());
|
2015-04-09 22:42:33 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
|
|
|
for (size_t j = 0; j<verneed->vn_cnt; ++j) {
|
|
|
|
const ElfW(Vernaux)* vernaux = reinterpret_cast<ElfW(Vernaux)*>(verneed_ptr + vernaux_offset);
|
|
|
|
vernaux_offset += vernaux->vna_next;
|
|
|
|
|
|
|
|
const ElfW(Word) elf_hash = vernaux->vna_hash;
|
|
|
|
const char* ver_name = si_from->get_string(vernaux->vna_name);
|
|
|
|
ElfW(Half) source_index = vernaux->vna_other;
|
|
|
|
|
|
|
|
add_version_info(source_index, elf_hash, ver_name, target_si);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VersionTracker::init_verdef(const soinfo* si_from) {
|
|
|
|
return for_each_verdef(si_from,
|
|
|
|
[&](size_t, const ElfW(Verdef)* verdef, const ElfW(Verdaux)* verdaux) {
|
|
|
|
add_version_info(verdef->vd_ndx, verdef->vd_hash,
|
|
|
|
si_from->get_string(verdaux->vda_name), si_from);
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
);
|
|
|
|
}
|
|
|
|
|
|
|
|
bool VersionTracker::init(const soinfo* si_from) {
|
|
|
|
if (!si_from->has_min_version(2)) {
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
return init_verneed(si_from) && init_verdef(si_from);
|
|
|
|
}
|
|
|
|
|
2015-05-01 01:11:48 +02:00
|
|
|
bool soinfo::lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
|
|
|
|
const char* sym_name, const version_info** vi) {
|
|
|
|
const ElfW(Versym)* sym_ver_ptr = get_versym(sym);
|
|
|
|
ElfW(Versym) sym_ver = sym_ver_ptr == nullptr ? 0 : *sym_ver_ptr;
|
|
|
|
|
|
|
|
if (sym_ver != VER_NDX_LOCAL && sym_ver != VER_NDX_GLOBAL) {
|
|
|
|
*vi = version_tracker.get_version_info(sym_ver);
|
|
|
|
|
|
|
|
if (*vi == nullptr) {
|
|
|
|
DL_ERR("cannot find verneed/verdef for version index=%d "
|
2015-05-07 19:48:00 +02:00
|
|
|
"referenced by symbol \"%s\" at \"%s\"", sym_ver, sym_name, get_realpath());
|
2015-05-01 01:11:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// there is no version info
|
|
|
|
*vi = nullptr;
|
|
|
|
}
|
|
|
|
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
2015-01-13 21:12:38 +01:00
|
|
|
#if !defined(__mips__)
|
2013-10-26 02:38:02 +02:00
|
|
|
#if defined(USE_RELA)
|
2015-01-13 21:12:38 +01:00
|
|
|
static ElfW(Addr) get_addend(ElfW(Rela)* rela, ElfW(Addr) reloc_addr __unused) {
|
|
|
|
return rela->r_addend;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
static ElfW(Addr) get_addend(ElfW(Rel)* rel, ElfW(Addr) reloc_addr) {
|
2015-03-31 03:43:38 +02:00
|
|
|
if (ELFW(R_TYPE)(rel->r_info) == R_GENERIC_RELATIVE ||
|
|
|
|
ELFW(R_TYPE)(rel->r_info) == R_GENERIC_IRELATIVE) {
|
2015-01-13 21:12:38 +01:00
|
|
|
return *reinterpret_cast<ElfW(Addr)*>(reloc_addr);
|
|
|
|
}
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-04 01:06:47 +01:00
|
|
|
template<typename ElfRelIteratorT>
|
2015-05-01 04:49:19 +02:00
|
|
|
bool soinfo::relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
|
|
|
|
const soinfo_list_t& global_group, const soinfo_list_t& local_group) {
|
2015-02-04 01:06:47 +01:00
|
|
|
for (size_t idx = 0; rel_iterator.has_next(); ++idx) {
|
|
|
|
const auto rel = rel_iterator.next();
|
2015-02-05 01:05:30 +01:00
|
|
|
if (rel == nullptr) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-01-13 21:12:38 +01:00
|
|
|
ElfW(Word) type = ELFW(R_TYPE)(rel->r_info);
|
|
|
|
ElfW(Word) sym = ELFW(R_SYM)(rel->r_info);
|
|
|
|
|
|
|
|
ElfW(Addr) reloc = static_cast<ElfW(Addr)>(rel->r_offset + load_bias);
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Addr) sym_addr = 0;
|
2014-08-29 21:02:36 +02:00
|
|
|
const char* sym_name = nullptr;
|
2015-01-13 21:12:38 +01:00
|
|
|
ElfW(Addr) addend = get_addend(rel, reloc);
|
2013-10-05 02:01:33 +02:00
|
|
|
|
2015-05-07 19:48:00 +02:00
|
|
|
DEBUG("Processing '%s' relocation at index %zd", get_realpath(), idx);
|
2015-01-09 08:30:15 +01:00
|
|
|
if (type == R_GENERIC_NONE) {
|
2013-10-05 02:01:33 +02:00
|
|
|
continue;
|
|
|
|
}
|
2014-09-06 01:42:53 +02:00
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
const ElfW(Sym)* s = nullptr;
|
2014-09-06 01:42:53 +02:00
|
|
|
soinfo* lsi = nullptr;
|
|
|
|
|
2013-10-05 02:01:33 +02:00
|
|
|
if (sym != 0) {
|
2014-11-13 18:39:20 +01:00
|
|
|
sym_name = get_string(symtab_[sym].st_name);
|
2015-05-01 01:11:48 +02:00
|
|
|
const version_info* vi = nullptr;
|
2015-04-09 22:42:33 +02:00
|
|
|
|
2015-05-01 01:11:48 +02:00
|
|
|
if (!lookup_version_info(version_tracker, sym, sym_name, &vi)) {
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-09 22:42:33 +02:00
|
|
|
|
2015-05-01 01:11:48 +02:00
|
|
|
if (!soinfo_do_lookup(this, sym_name, vi, &lsi, global_group, local_group, &s)) {
|
|
|
|
return false;
|
2015-04-09 22:42:33 +02:00
|
|
|
}
|
2015-05-01 01:11:48 +02:00
|
|
|
|
2014-08-29 21:02:36 +02:00
|
|
|
if (s == nullptr) {
|
2013-10-05 02:01:33 +02:00
|
|
|
// We only allow an undefined symbol if this is a weak reference...
|
2014-11-13 18:39:20 +01:00
|
|
|
s = &symtab_[sym];
|
2013-10-05 02:01:33 +02:00
|
|
|
if (ELF_ST_BIND(s->st_info) != STB_WEAK) {
|
2015-05-07 19:48:00 +02:00
|
|
|
DL_ERR("cannot locate symbol \"%s\" referenced by \"%s\"...", sym_name, get_realpath());
|
2015-01-14 20:36:38 +01:00
|
|
|
return false;
|
2013-10-05 02:01:33 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
/* IHI0044C AAELF 4.5.1.1:
|
|
|
|
|
|
|
|
Libraries are not searched to resolve weak references.
|
|
|
|
It is not an error for a weak reference to remain unsatisfied.
|
|
|
|
|
|
|
|
During linking, the value of an undefined weak reference is:
|
|
|
|
- Zero if the relocation type is absolute
|
|
|
|
- The address of the place if the relocation is pc-relative
|
|
|
|
- The address of nominal base address if the relocation
|
|
|
|
type is base-relative.
|
|
|
|
*/
|
|
|
|
|
|
|
|
switch (type) {
|
2015-01-13 21:17:31 +01:00
|
|
|
case R_GENERIC_JUMP_SLOT:
|
|
|
|
case R_GENERIC_GLOB_DAT:
|
|
|
|
case R_GENERIC_RELATIVE:
|
|
|
|
case R_GENERIC_IRELATIVE:
|
2013-10-10 16:19:31 +02:00
|
|
|
#if defined(__aarch64__)
|
2014-09-12 18:43:13 +02:00
|
|
|
case R_AARCH64_ABS64:
|
|
|
|
case R_AARCH64_ABS32:
|
|
|
|
case R_AARCH64_ABS16:
|
2015-01-13 21:17:31 +01:00
|
|
|
#elif defined(__x86_64__)
|
|
|
|
case R_X86_64_32:
|
|
|
|
case R_X86_64_64:
|
2015-01-13 21:12:38 +01:00
|
|
|
#elif defined(__arm__)
|
|
|
|
case R_ARM_ABS32:
|
|
|
|
#elif defined(__i386__)
|
|
|
|
case R_386_32:
|
2015-01-13 21:17:31 +01:00
|
|
|
#endif
|
2014-09-12 18:43:13 +02:00
|
|
|
/*
|
|
|
|
* The sym_addr was initialized to be zero above, or the relocation
|
|
|
|
* code below does not care about value of sym_addr.
|
|
|
|
* No need to do anything.
|
|
|
|
*/
|
|
|
|
break;
|
2015-01-13 21:17:31 +01:00
|
|
|
#if defined(__x86_64__)
|
2015-01-13 23:31:54 +01:00
|
|
|
case R_X86_64_PC32:
|
|
|
|
sym_addr = reloc;
|
|
|
|
break;
|
2015-01-13 21:12:38 +01:00
|
|
|
#elif defined(__i386__)
|
|
|
|
case R_386_PC32:
|
|
|
|
sym_addr = reloc;
|
|
|
|
break;
|
2015-01-13 23:31:54 +01:00
|
|
|
#endif
|
2014-09-12 18:43:13 +02:00
|
|
|
default:
|
2015-01-13 21:12:38 +01:00
|
|
|
DL_ERR("unknown weak reloc type %d @ %p (%zu)", type, rel, idx);
|
2015-01-14 20:36:38 +01:00
|
|
|
return false;
|
2013-10-05 02:01:33 +02:00
|
|
|
}
|
2015-07-26 16:37:09 +02:00
|
|
|
} else { // We got a definition.
|
|
|
|
#if !defined(__LP64__)
|
|
|
|
// When relocating dso with text_relocation .text segment is
|
|
|
|
// not executable. We need to restore elf flags before resolving
|
|
|
|
// STT_GNU_IFUNC symbol.
|
|
|
|
bool protect_segments = has_text_relocations &&
|
|
|
|
lsi == this &&
|
|
|
|
ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC;
|
|
|
|
if (protect_segments) {
|
|
|
|
if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
|
|
|
|
DL_ERR("can't protect segments for \"%s\": %s",
|
|
|
|
get_realpath(), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2014-09-12 00:16:03 +02:00
|
|
|
sym_addr = lsi->resolve_symbol_address(s);
|
2015-07-26 16:37:09 +02:00
|
|
|
#if !defined(__LP64__)
|
|
|
|
if (protect_segments) {
|
|
|
|
if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
|
|
|
|
DL_ERR("can't unprotect loadable segments for \"%s\": %s",
|
|
|
|
get_realpath(), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
2013-10-05 02:01:33 +02:00
|
|
|
}
|
|
|
|
count_relocation(kRelocSymbol);
|
|
|
|
}
|
|
|
|
|
|
|
|
switch (type) {
|
2015-01-09 08:30:15 +01:00
|
|
|
case R_GENERIC_JUMP_SLOT:
|
2013-10-10 16:19:31 +02:00
|
|
|
count_relocation(kRelocAbsolute);
|
2015-01-13 21:12:38 +01:00
|
|
|
MARK(rel->r_offset);
|
|
|
|
TRACE_TYPE(RELO, "RELO JMP_SLOT %16p <- %16p %s\n",
|
|
|
|
reinterpret_cast<void*>(reloc),
|
|
|
|
reinterpret_cast<void*>(sym_addr + addend), sym_name);
|
|
|
|
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
|
2013-10-10 16:19:31 +02:00
|
|
|
break;
|
2015-01-09 08:30:15 +01:00
|
|
|
case R_GENERIC_GLOB_DAT:
|
2013-10-10 16:19:31 +02:00
|
|
|
count_relocation(kRelocAbsolute);
|
2015-01-13 21:12:38 +01:00
|
|
|
MARK(rel->r_offset);
|
|
|
|
TRACE_TYPE(RELO, "RELO GLOB_DAT %16p <- %16p %s\n",
|
|
|
|
reinterpret_cast<void*>(reloc),
|
|
|
|
reinterpret_cast<void*>(sym_addr + addend), sym_name);
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
|
2013-10-10 16:19:31 +02:00
|
|
|
break;
|
2015-01-09 08:30:15 +01:00
|
|
|
case R_GENERIC_RELATIVE:
|
|
|
|
count_relocation(kRelocRelative);
|
2015-01-13 21:12:38 +01:00
|
|
|
MARK(rel->r_offset);
|
|
|
|
TRACE_TYPE(RELO, "RELO RELATIVE %16p <- %16p\n",
|
|
|
|
reinterpret_cast<void*>(reloc),
|
2015-02-05 01:05:30 +01:00
|
|
|
reinterpret_cast<void*>(load_bias + addend));
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) = (load_bias + addend);
|
2015-01-09 08:30:15 +01:00
|
|
|
break;
|
|
|
|
case R_GENERIC_IRELATIVE:
|
|
|
|
count_relocation(kRelocRelative);
|
2015-01-13 21:12:38 +01:00
|
|
|
MARK(rel->r_offset);
|
|
|
|
TRACE_TYPE(RELO, "RELO IRELATIVE %16p <- %16p\n",
|
|
|
|
reinterpret_cast<void*>(reloc),
|
2015-02-05 01:05:30 +01:00
|
|
|
reinterpret_cast<void*>(load_bias + addend));
|
2015-07-26 16:37:09 +02:00
|
|
|
{
|
|
|
|
#if !defined(__LP64__)
|
|
|
|
// When relocating dso with text_relocation .text segment is
|
|
|
|
// not executable. We need to restore elf flags for this
|
|
|
|
// particular call.
|
|
|
|
if (has_text_relocations) {
|
|
|
|
if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
|
|
|
|
DL_ERR("can't protect segments for \"%s\": %s",
|
|
|
|
get_realpath(), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
ElfW(Addr) ifunc_addr = call_ifunc_resolver(load_bias + addend);
|
|
|
|
#if !defined(__LP64__)
|
|
|
|
// Unprotect it afterwards...
|
|
|
|
if (has_text_relocations) {
|
|
|
|
if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
|
|
|
|
DL_ERR("can't unprotect loadable segments for \"%s\": %s",
|
|
|
|
get_realpath(), strerror(errno));
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) = ifunc_addr;
|
|
|
|
}
|
2015-01-09 08:30:15 +01:00
|
|
|
break;
|
|
|
|
|
|
|
|
#if defined(__aarch64__)
|
2014-09-12 18:43:13 +02:00
|
|
|
case R_AARCH64_ABS64:
|
2013-10-10 16:19:31 +02:00
|
|
|
count_relocation(kRelocAbsolute);
|
2015-01-13 21:12:38 +01:00
|
|
|
MARK(rel->r_offset);
|
2014-02-11 02:46:57 +01:00
|
|
|
TRACE_TYPE(RELO, "RELO ABS64 %16llx <- %16llx %s\n",
|
2015-10-15 22:26:03 +02:00
|
|
|
reloc, sym_addr + addend, sym_name);
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
|
2013-10-10 16:19:31 +02:00
|
|
|
break;
|
2014-09-12 18:43:13 +02:00
|
|
|
case R_AARCH64_ABS32:
|
2013-10-10 16:19:31 +02:00
|
|
|
count_relocation(kRelocAbsolute);
|
2015-01-13 21:12:38 +01:00
|
|
|
MARK(rel->r_offset);
|
2014-02-11 02:46:57 +01:00
|
|
|
TRACE_TYPE(RELO, "RELO ABS32 %16llx <- %16llx %s\n",
|
2015-10-15 22:26:03 +02:00
|
|
|
reloc, sym_addr + addend, sym_name);
|
2015-03-31 03:43:38 +02:00
|
|
|
{
|
|
|
|
const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
|
|
|
|
const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
|
2015-10-15 22:26:03 +02:00
|
|
|
if ((min_value <= (sym_addr + addend)) &&
|
|
|
|
((sym_addr + addend) <= max_value)) {
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
|
2015-03-31 03:43:38 +02:00
|
|
|
} else {
|
|
|
|
DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
|
2015-10-15 22:26:03 +02:00
|
|
|
sym_addr + addend, min_value, max_value);
|
2015-03-31 03:43:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-10-10 16:19:31 +02:00
|
|
|
}
|
|
|
|
break;
|
2014-09-12 18:43:13 +02:00
|
|
|
case R_AARCH64_ABS16:
|
2013-10-10 16:19:31 +02:00
|
|
|
count_relocation(kRelocAbsolute);
|
2015-01-13 21:12:38 +01:00
|
|
|
MARK(rel->r_offset);
|
2014-02-11 02:46:57 +01:00
|
|
|
TRACE_TYPE(RELO, "RELO ABS16 %16llx <- %16llx %s\n",
|
2015-10-15 22:26:03 +02:00
|
|
|
reloc, sym_addr + addend, sym_name);
|
2015-03-31 03:43:38 +02:00
|
|
|
{
|
|
|
|
const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
|
|
|
|
const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
|
2015-10-15 22:26:03 +02:00
|
|
|
if ((min_value <= (sym_addr + addend)) &&
|
|
|
|
((sym_addr + addend) <= max_value)) {
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) = (sym_addr + addend);
|
2015-03-31 03:43:38 +02:00
|
|
|
} else {
|
|
|
|
DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
|
2015-10-15 22:26:03 +02:00
|
|
|
sym_addr + addend, min_value, max_value);
|
2015-03-31 03:43:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-10-10 16:19:31 +02:00
|
|
|
}
|
|
|
|
break;
|
2014-09-12 18:43:13 +02:00
|
|
|
case R_AARCH64_PREL64:
|
2013-10-10 16:19:31 +02:00
|
|
|
count_relocation(kRelocRelative);
|
2015-01-13 21:12:38 +01:00
|
|
|
MARK(rel->r_offset);
|
2014-02-11 02:46:57 +01:00
|
|
|
TRACE_TYPE(RELO, "RELO REL64 %16llx <- %16llx - %16llx %s\n",
|
2015-10-15 22:26:03 +02:00
|
|
|
reloc, sym_addr + addend, rel->r_offset, sym_name);
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
|
2013-10-10 16:19:31 +02:00
|
|
|
break;
|
2014-09-12 18:43:13 +02:00
|
|
|
case R_AARCH64_PREL32:
|
2013-10-10 16:19:31 +02:00
|
|
|
count_relocation(kRelocRelative);
|
2015-01-13 21:12:38 +01:00
|
|
|
MARK(rel->r_offset);
|
2014-02-11 02:46:57 +01:00
|
|
|
TRACE_TYPE(RELO, "RELO REL32 %16llx <- %16llx - %16llx %s\n",
|
2015-10-15 22:26:03 +02:00
|
|
|
reloc, sym_addr + addend, rel->r_offset, sym_name);
|
2015-03-31 03:43:38 +02:00
|
|
|
{
|
|
|
|
const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT32_MIN);
|
|
|
|
const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT32_MAX);
|
2015-10-15 22:26:03 +02:00
|
|
|
if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
|
|
|
|
((sym_addr + addend - rel->r_offset) <= max_value)) {
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
|
2015-03-31 03:43:38 +02:00
|
|
|
} else {
|
|
|
|
DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
|
2015-10-15 22:26:03 +02:00
|
|
|
sym_addr + addend - rel->r_offset, min_value, max_value);
|
2015-03-31 03:43:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-10-10 16:19:31 +02:00
|
|
|
}
|
|
|
|
break;
|
2014-09-12 18:43:13 +02:00
|
|
|
case R_AARCH64_PREL16:
|
2013-10-10 16:19:31 +02:00
|
|
|
count_relocation(kRelocRelative);
|
2015-01-13 21:12:38 +01:00
|
|
|
MARK(rel->r_offset);
|
2014-02-11 02:46:57 +01:00
|
|
|
TRACE_TYPE(RELO, "RELO REL16 %16llx <- %16llx - %16llx %s\n",
|
2015-10-15 22:26:03 +02:00
|
|
|
reloc, sym_addr + addend, rel->r_offset, sym_name);
|
2015-03-31 03:43:38 +02:00
|
|
|
{
|
|
|
|
const ElfW(Addr) min_value = static_cast<ElfW(Addr)>(INT16_MIN);
|
|
|
|
const ElfW(Addr) max_value = static_cast<ElfW(Addr)>(UINT16_MAX);
|
2015-10-15 22:26:03 +02:00
|
|
|
if ((min_value <= (sym_addr + addend - rel->r_offset)) &&
|
|
|
|
((sym_addr + addend - rel->r_offset) <= max_value)) {
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - rel->r_offset;
|
2015-03-31 03:43:38 +02:00
|
|
|
} else {
|
|
|
|
DL_ERR("0x%016llx out of range 0x%016llx to 0x%016llx",
|
2015-10-15 22:26:03 +02:00
|
|
|
sym_addr + addend - rel->r_offset, min_value, max_value);
|
2015-03-31 03:43:38 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-10-10 16:19:31 +02:00
|
|
|
}
|
|
|
|
break;
|
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case R_AARCH64_COPY:
|
2014-07-03 21:04:31 +02:00
|
|
|
/*
|
|
|
|
* ET_EXEC is not supported so this should not happen.
|
|
|
|
*
|
2015-03-31 20:14:03 +02:00
|
|
|
* http://infocenter.arm.com/help/topic/com.arm.doc.ihi0056b/IHI0056B_aaelf64.pdf
|
2014-07-03 21:04:31 +02:00
|
|
|
*
|
2015-03-31 20:14:03 +02:00
|
|
|
* Section 4.6.11 "Dynamic relocations"
|
2014-07-03 21:04:31 +02:00
|
|
|
* R_AARCH64_COPY may only appear in executable objects where e_type is
|
|
|
|
* set to ET_EXEC.
|
|
|
|
*/
|
2015-05-07 19:48:00 +02:00
|
|
|
DL_ERR("%s R_AARCH64_COPY relocations are not supported", get_realpath());
|
2015-01-14 20:36:38 +01:00
|
|
|
return false;
|
2014-09-12 18:43:13 +02:00
|
|
|
case R_AARCH64_TLS_TPREL64:
|
2014-02-11 02:46:57 +01:00
|
|
|
TRACE_TYPE(RELO, "RELO TLS_TPREL64 *** %16llx <- %16llx - %16llx\n",
|
2015-01-13 21:12:38 +01:00
|
|
|
reloc, (sym_addr + addend), rel->r_offset);
|
2013-10-10 16:19:31 +02:00
|
|
|
break;
|
2014-09-12 18:43:13 +02:00
|
|
|
case R_AARCH64_TLS_DTPREL32:
|
2014-02-11 02:46:57 +01:00
|
|
|
TRACE_TYPE(RELO, "RELO TLS_DTPREL32 *** %16llx <- %16llx - %16llx\n",
|
2015-01-13 21:12:38 +01:00
|
|
|
reloc, (sym_addr + addend), rel->r_offset);
|
2013-10-10 16:19:31 +02:00
|
|
|
break;
|
|
|
|
#elif defined(__x86_64__)
|
2014-09-12 18:43:13 +02:00
|
|
|
case R_X86_64_32:
|
|
|
|
count_relocation(kRelocRelative);
|
2015-01-13 21:12:38 +01:00
|
|
|
MARK(rel->r_offset);
|
2014-09-12 18:43:13 +02:00
|
|
|
TRACE_TYPE(RELO, "RELO R_X86_64_32 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
|
|
|
|
static_cast<size_t>(sym_addr), sym_name);
|
2015-01-13 21:12:38 +01:00
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
|
|
|
case R_X86_64_64:
|
|
|
|
count_relocation(kRelocRelative);
|
2015-01-13 21:12:38 +01:00
|
|
|
MARK(rel->r_offset);
|
2014-09-12 18:43:13 +02:00
|
|
|
TRACE_TYPE(RELO, "RELO R_X86_64_64 %08zx <- +%08zx %s", static_cast<size_t>(reloc),
|
|
|
|
static_cast<size_t>(sym_addr), sym_name);
|
2015-01-13 21:12:38 +01:00
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend;
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
|
|
|
case R_X86_64_PC32:
|
|
|
|
count_relocation(kRelocRelative);
|
2015-01-13 21:12:38 +01:00
|
|
|
MARK(rel->r_offset);
|
2014-09-12 18:43:13 +02:00
|
|
|
TRACE_TYPE(RELO, "RELO R_X86_64_PC32 %08zx <- +%08zx (%08zx - %08zx) %s",
|
|
|
|
static_cast<size_t>(reloc), static_cast<size_t>(sym_addr - reloc),
|
|
|
|
static_cast<size_t>(sym_addr), static_cast<size_t>(reloc), sym_name);
|
2015-01-13 21:12:38 +01:00
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) = sym_addr + addend - reloc;
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2015-01-13 21:12:38 +01:00
|
|
|
#elif defined(__arm__)
|
2014-09-12 18:43:13 +02:00
|
|
|
case R_ARM_ABS32:
|
|
|
|
count_relocation(kRelocAbsolute);
|
|
|
|
MARK(rel->r_offset);
|
|
|
|
TRACE_TYPE(RELO, "RELO ABS %08x <- %08x %s", reloc, sym_addr, sym_name);
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
|
|
|
|
break;
|
|
|
|
case R_ARM_REL32:
|
|
|
|
count_relocation(kRelocRelative);
|
|
|
|
MARK(rel->r_offset);
|
|
|
|
TRACE_TYPE(RELO, "RELO REL32 %08x <- %08x - %08x %s",
|
|
|
|
reloc, sym_addr, rel->r_offset, sym_name);
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr - rel->r_offset;
|
|
|
|
break;
|
|
|
|
case R_ARM_COPY:
|
|
|
|
/*
|
|
|
|
* ET_EXEC is not supported so this should not happen.
|
|
|
|
*
|
|
|
|
* http://infocenter.arm.com/help/topic/com.arm.doc.ihi0044d/IHI0044D_aaelf.pdf
|
|
|
|
*
|
2015-03-31 20:14:03 +02:00
|
|
|
* Section 4.6.1.10 "Dynamic relocations"
|
2014-09-12 18:43:13 +02:00
|
|
|
* R_ARM_COPY may only appear in executable objects where e_type is
|
|
|
|
* set to ET_EXEC.
|
|
|
|
*/
|
2015-05-07 19:48:00 +02:00
|
|
|
DL_ERR("%s R_ARM_COPY relocations are not supported", get_realpath());
|
2015-01-14 20:36:38 +01:00
|
|
|
return false;
|
2013-10-26 02:38:02 +02:00
|
|
|
#elif defined(__i386__)
|
2014-09-12 18:43:13 +02:00
|
|
|
case R_386_32:
|
|
|
|
count_relocation(kRelocRelative);
|
|
|
|
MARK(rel->r_offset);
|
|
|
|
TRACE_TYPE(RELO, "RELO R_386_32 %08x <- +%08x %s", reloc, sym_addr, sym_name);
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) += sym_addr;
|
|
|
|
break;
|
|
|
|
case R_386_PC32:
|
|
|
|
count_relocation(kRelocRelative);
|
|
|
|
MARK(rel->r_offset);
|
|
|
|
TRACE_TYPE(RELO, "RELO R_386_PC32 %08x <- +%08x (%08x - %08x) %s",
|
|
|
|
reloc, (sym_addr - reloc), sym_addr, reloc, sym_name);
|
|
|
|
*reinterpret_cast<ElfW(Addr)*>(reloc) += (sym_addr - reloc);
|
|
|
|
break;
|
2013-10-26 02:38:02 +02:00
|
|
|
#endif
|
2014-09-12 18:43:13 +02:00
|
|
|
default:
|
|
|
|
DL_ERR("unknown reloc type %d @ %p (%zu)", type, rel, idx);
|
|
|
|
return false;
|
2012-07-31 21:07:22 +02:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
|
|
|
return true;
|
2012-07-31 21:07:22 +02:00
|
|
|
}
|
2015-01-14 20:36:38 +01:00
|
|
|
#endif // !defined(__mips__)
|
2012-07-31 21:07:22 +02:00
|
|
|
|
2015-03-31 03:43:38 +02:00
|
|
|
void soinfo::call_array(const char* array_name __unused, linker_function_t* functions,
|
|
|
|
size_t count, bool reverse) {
|
2014-08-29 21:02:36 +02:00
|
|
|
if (functions == nullptr) {
|
2012-11-01 23:16:56 +01:00
|
|
|
return;
|
|
|
|
}
|
2009-05-18 14:37:41 +02:00
|
|
|
|
2015-05-07 19:48:00 +02:00
|
|
|
TRACE("[ Calling %s (size %zd) @ %p for '%s' ]", array_name, count, functions, get_realpath());
|
2009-05-18 14:37:41 +02:00
|
|
|
|
2013-03-12 18:40:45 +01:00
|
|
|
int begin = reverse ? (count - 1) : 0;
|
|
|
|
int end = reverse ? -1 : count;
|
|
|
|
int step = reverse ? -1 : 1;
|
2009-05-18 14:37:41 +02:00
|
|
|
|
2013-03-12 18:40:45 +01:00
|
|
|
for (int i = begin; i != end; i += step) {
|
|
|
|
TRACE("[ %s[%d] == %p ]", array_name, i, functions[i]);
|
2014-11-13 18:39:20 +01:00
|
|
|
call_function("function", functions[i]);
|
2012-11-01 23:16:56 +01:00
|
|
|
}
|
|
|
|
|
2015-05-07 19:48:00 +02:00
|
|
|
TRACE("[ Done calling %s for '%s' ]", array_name, get_realpath());
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
void soinfo::call_function(const char* function_name __unused, linker_function_t function) {
|
2014-08-29 21:02:36 +02:00
|
|
|
if (function == nullptr || reinterpret_cast<uintptr_t>(function) == static_cast<uintptr_t>(-1)) {
|
2012-11-01 23:16:56 +01:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
2015-05-07 19:48:00 +02:00
|
|
|
TRACE("[ Calling %s @ %p for '%s' ]", function_name, function, get_realpath());
|
2012-11-01 23:16:56 +01:00
|
|
|
function();
|
2015-05-07 19:48:00 +02:00
|
|
|
TRACE("[ Done calling %s @ %p for '%s' ]", function_name, function, get_realpath());
|
2012-08-13 15:58:37 +02:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
void soinfo::call_pre_init_constructors() {
|
2013-05-09 23:19:58 +02:00
|
|
|
// DT_PREINIT_ARRAY functions are called before any other constructors for executables,
|
|
|
|
// but ignored in a shared library.
|
2014-11-13 18:39:20 +01:00
|
|
|
call_array("DT_PREINIT_ARRAY", preinit_array_, preinit_array_count_, false);
|
2012-11-01 23:16:56 +01:00
|
|
|
}
|
2011-12-21 10:03:54 +01:00
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
void soinfo::call_constructors() {
|
2012-11-01 23:16:56 +01:00
|
|
|
if (constructors_called) {
|
|
|
|
return;
|
|
|
|
}
|
2011-12-21 10:03:54 +01:00
|
|
|
|
2012-11-01 23:16:56 +01:00
|
|
|
// We set constructors_called before actually calling the constructors, otherwise it doesn't
|
|
|
|
// protect against recursive constructor calls. One simple example of constructor recursion
|
|
|
|
// is the libc debug malloc, which is implemented in libc_malloc_debug_leak.so:
|
|
|
|
// 1. The program depends on libc, so libc's constructor is called here.
|
|
|
|
// 2. The libc constructor calls dlopen() to load libc_malloc_debug_leak.so.
|
|
|
|
// 3. dlopen() calls the constructors on the newly created
|
|
|
|
// soinfo for libc_malloc_debug_leak.so.
|
|
|
|
// 4. The debug .so depends on libc, so CallConstructors is
|
|
|
|
// called again with the libc soinfo. If it doesn't trigger the early-
|
|
|
|
// out above, the libc constructor will be called again (recursively!).
|
|
|
|
constructors_called = true;
|
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
if (!is_main_executable() && preinit_array_ != nullptr) {
|
2013-05-09 23:19:58 +02:00
|
|
|
// The GNU dynamic linker silently ignores these, but we warn the developer.
|
2013-10-01 03:43:46 +02:00
|
|
|
PRINT("\"%s\": ignoring %zd-entry DT_PREINIT_ARRAY in shared library!",
|
2015-05-07 19:48:00 +02:00
|
|
|
get_realpath(), preinit_array_count_);
|
2012-11-01 23:16:56 +01:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
get_children().for_each([] (soinfo* si) {
|
2014-11-13 18:39:20 +01:00
|
|
|
si->call_constructors();
|
2014-05-09 18:10:14 +02:00
|
|
|
});
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-05-07 19:48:00 +02:00
|
|
|
TRACE("\"%s\": calling constructors", get_realpath());
|
2013-05-09 23:19:58 +02:00
|
|
|
|
|
|
|
// DT_INIT should be called before DT_INIT_ARRAY if both are present.
|
2014-11-13 18:39:20 +01:00
|
|
|
call_function("DT_INIT", init_func_);
|
|
|
|
call_array("DT_INIT_ARRAY", init_array_, init_array_count_, false);
|
2011-12-21 10:03:54 +01:00
|
|
|
}
|
2009-05-18 14:37:41 +02:00
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
void soinfo::call_destructors() {
|
2014-09-06 01:42:53 +02:00
|
|
|
if (!constructors_called) {
|
|
|
|
return;
|
|
|
|
}
|
2015-05-07 19:48:00 +02:00
|
|
|
TRACE("\"%s\": calling destructors", get_realpath());
|
2013-05-09 23:19:58 +02:00
|
|
|
|
|
|
|
// DT_FINI_ARRAY must be parsed in reverse order.
|
2014-11-13 18:39:20 +01:00
|
|
|
call_array("DT_FINI_ARRAY", fini_array_, fini_array_count_, true);
|
2013-05-09 23:19:58 +02:00
|
|
|
|
|
|
|
// DT_FINI should be called after DT_FINI_ARRAY if both are present.
|
2014-11-13 18:39:20 +01:00
|
|
|
call_function("DT_FINI", fini_func_);
|
2014-05-20 00:06:58 +02:00
|
|
|
|
|
|
|
// This is needed on second call to dlopen
|
|
|
|
// after library has been unloaded with RTLD_NODELETE
|
|
|
|
constructors_called = false;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
void soinfo::add_child(soinfo* child) {
|
2014-08-22 21:25:04 +02:00
|
|
|
if (has_min_version(0)) {
|
2014-11-13 18:39:20 +01:00
|
|
|
child->parents_.push_back(this);
|
|
|
|
this->children_.push_back(child);
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
void soinfo::remove_all_links() {
|
2014-08-22 21:25:04 +02:00
|
|
|
if (!has_min_version(0)) {
|
2014-05-09 18:10:14 +02:00
|
|
|
return;
|
|
|
|
}
|
|
|
|
|
|
|
|
// 1. Untie connected soinfos from 'this'.
|
2014-11-13 18:39:20 +01:00
|
|
|
children_.for_each([&] (soinfo* child) {
|
|
|
|
child->parents_.remove_if([&] (const soinfo* parent) {
|
2014-05-09 18:10:14 +02:00
|
|
|
return parent == this;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
parents_.for_each([&] (soinfo* parent) {
|
|
|
|
parent->children_.remove_if([&] (const soinfo* child) {
|
2014-05-09 18:10:14 +02:00
|
|
|
return child == this;
|
|
|
|
});
|
|
|
|
});
|
|
|
|
|
|
|
|
// 2. Once everything untied - clear local lists.
|
2014-11-13 18:39:20 +01:00
|
|
|
parents_.clear();
|
|
|
|
children_.clear();
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
dev_t soinfo::get_st_dev() const {
|
2014-08-22 21:25:04 +02:00
|
|
|
if (has_min_version(0)) {
|
2014-11-13 18:39:20 +01:00
|
|
|
return st_dev_;
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 21:25:04 +02:00
|
|
|
return 0;
|
2014-05-09 18:10:14 +02:00
|
|
|
};
|
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
ino_t soinfo::get_st_ino() const {
|
2014-08-22 21:25:04 +02:00
|
|
|
if (has_min_version(0)) {
|
2014-11-13 18:39:20 +01:00
|
|
|
return st_ino_;
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 21:25:04 +02:00
|
|
|
return 0;
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
off64_t soinfo::get_file_offset() const {
|
2014-10-04 02:52:44 +02:00
|
|
|
if (has_min_version(1)) {
|
2014-11-13 18:39:20 +01:00
|
|
|
return file_offset_;
|
2014-10-04 02:52:44 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
uint32_t soinfo::get_rtld_flags() const {
|
2014-09-16 02:00:10 +02:00
|
|
|
if (has_min_version(1)) {
|
2014-11-13 18:39:20 +01:00
|
|
|
return rtld_flags_;
|
2014-09-16 02:00:10 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
uint32_t soinfo::get_dt_flags_1() const {
|
|
|
|
if (has_min_version(1)) {
|
2014-11-13 18:39:20 +01:00
|
|
|
return dt_flags_1_;
|
2014-08-28 23:12:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
return 0;
|
|
|
|
}
|
2015-03-18 04:06:36 +01:00
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
void soinfo::set_dt_flags_1(uint32_t dt_flags_1) {
|
|
|
|
if (has_min_version(1)) {
|
|
|
|
if ((dt_flags_1 & DF_1_GLOBAL) != 0) {
|
2014-11-13 18:39:20 +01:00
|
|
|
rtld_flags_ |= RTLD_GLOBAL;
|
2014-08-28 23:12:12 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if ((dt_flags_1 & DF_1_NODELETE) != 0) {
|
2014-11-13 18:39:20 +01:00
|
|
|
rtld_flags_ |= RTLD_NODELETE;
|
2014-08-28 23:12:12 +02:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
dt_flags_1_ = dt_flags_1;
|
2014-08-28 23:12:12 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-03-31 20:14:03 +02:00
|
|
|
const char* soinfo::get_realpath() const {
|
2015-09-28 19:14:17 +02:00
|
|
|
#if defined(__work_around_b_24465209__)
|
2015-03-31 20:14:03 +02:00
|
|
|
if (has_min_version(2)) {
|
|
|
|
return realpath_.c_str();
|
|
|
|
} else {
|
|
|
|
return old_name_;
|
|
|
|
}
|
|
|
|
#else
|
|
|
|
return realpath_.c_str();
|
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
|
|
|
const char* soinfo::get_soname() const {
|
2015-09-28 19:14:17 +02:00
|
|
|
#if defined(__work_around_b_24465209__)
|
2015-03-18 04:06:36 +01:00
|
|
|
if (has_min_version(2)) {
|
|
|
|
return soname_;
|
|
|
|
} else {
|
2015-03-31 20:14:03 +02:00
|
|
|
return old_name_;
|
2015-03-18 04:06:36 +01:00
|
|
|
}
|
2015-03-31 20:14:03 +02:00
|
|
|
#else
|
|
|
|
return soname_;
|
|
|
|
#endif
|
2015-03-18 04:06:36 +01:00
|
|
|
}
|
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
// This is a return on get_children()/get_parents() if
|
2014-05-09 18:10:14 +02:00
|
|
|
// 'this->flags' does not have FLAG_NEW_SOINFO set.
|
|
|
|
static soinfo::soinfo_list_t g_empty_list;
|
|
|
|
|
|
|
|
soinfo::soinfo_list_t& soinfo::get_children() {
|
2014-08-22 21:25:04 +02:00
|
|
|
if (has_min_version(0)) {
|
2014-11-13 18:39:20 +01:00
|
|
|
return children_;
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
|
|
|
|
2014-08-22 21:25:04 +02:00
|
|
|
return g_empty_list;
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
const soinfo::soinfo_list_t& soinfo::get_children() const {
|
|
|
|
if (has_min_version(0)) {
|
|
|
|
return children_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_empty_list;
|
|
|
|
}
|
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
soinfo::soinfo_list_t& soinfo::get_parents() {
|
2014-11-13 18:39:20 +01:00
|
|
|
if (has_min_version(0)) {
|
|
|
|
return parents_;
|
2014-09-06 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
return g_empty_list;
|
2014-09-06 01:42:53 +02:00
|
|
|
}
|
|
|
|
|
2015-06-10 22:38:39 +02:00
|
|
|
static std::vector<std::string> g_empty_runpath;
|
|
|
|
|
|
|
|
const std::vector<std::string>& soinfo::get_dt_runpath() const {
|
|
|
|
if (has_min_version(2)) {
|
|
|
|
return dt_runpath_;
|
|
|
|
}
|
|
|
|
|
|
|
|
return g_empty_runpath;
|
|
|
|
}
|
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
ElfW(Addr) soinfo::resolve_symbol_address(const ElfW(Sym)* s) const {
|
2014-09-12 00:16:03 +02:00
|
|
|
if (ELF_ST_TYPE(s->st_info) == STT_GNU_IFUNC) {
|
|
|
|
return call_ifunc_resolver(s->st_value + load_bias);
|
|
|
|
}
|
|
|
|
|
|
|
|
return static_cast<ElfW(Addr)>(s->st_value + load_bias);
|
|
|
|
}
|
|
|
|
|
2014-09-30 04:14:45 +02:00
|
|
|
const char* soinfo::get_string(ElfW(Word) index) const {
|
2014-11-13 18:39:20 +01:00
|
|
|
if (has_min_version(1) && (index >= strtab_size_)) {
|
2015-03-31 20:14:03 +02:00
|
|
|
__libc_fatal("%s: strtab out of bounds error; STRSZ=%zd, name=%d",
|
2015-05-07 19:48:00 +02:00
|
|
|
get_realpath(), strtab_size_, index);
|
2014-09-30 04:14:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
return strtab_ + index;
|
2014-09-30 04:14:45 +02:00
|
|
|
}
|
|
|
|
|
2014-11-10 04:27:20 +01:00
|
|
|
bool soinfo::is_gnu_hash() const {
|
2014-11-29 22:57:41 +01:00
|
|
|
return (flags_ & FLAG_GNU_HASH) != 0;
|
2014-11-10 04:27:20 +01:00
|
|
|
}
|
|
|
|
|
2014-05-20 00:06:58 +02:00
|
|
|
bool soinfo::can_unload() const {
|
2014-08-28 23:12:12 +02:00
|
|
|
return (get_rtld_flags() & (RTLD_NODELETE | RTLD_GLOBAL)) == 0;
|
2014-05-20 00:06:58 +02:00
|
|
|
}
|
2014-08-28 23:12:12 +02:00
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
bool soinfo::is_linked() const {
|
|
|
|
return (flags_ & FLAG_LINKED) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
bool soinfo::is_main_executable() const {
|
|
|
|
return (flags_ & FLAG_EXE) != 0;
|
|
|
|
}
|
|
|
|
|
|
|
|
void soinfo::set_linked() {
|
|
|
|
flags_ |= FLAG_LINKED;
|
|
|
|
}
|
|
|
|
|
|
|
|
void soinfo::set_linker_flag() {
|
|
|
|
flags_ |= FLAG_LINKER;
|
|
|
|
}
|
|
|
|
|
|
|
|
void soinfo::set_main_executable() {
|
|
|
|
flags_ |= FLAG_EXE;
|
|
|
|
}
|
|
|
|
|
|
|
|
void soinfo::increment_ref_count() {
|
|
|
|
local_group_root_->ref_count_++;
|
|
|
|
}
|
|
|
|
|
|
|
|
size_t soinfo::decrement_ref_count() {
|
|
|
|
return --local_group_root_->ref_count_;
|
|
|
|
}
|
|
|
|
|
|
|
|
soinfo* soinfo::get_local_group_root() const {
|
|
|
|
return local_group_root_;
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:36:54 +02:00
|
|
|
// This function returns api-level at the time of
|
|
|
|
// dlopen/load. Note that libraries opened by system
|
|
|
|
// will always have 'current' api level.
|
|
|
|
uint32_t soinfo::get_target_sdk_version() const {
|
|
|
|
if (!has_min_version(2)) {
|
|
|
|
return __ANDROID_API__;
|
|
|
|
}
|
|
|
|
|
|
|
|
return local_group_root_->target_sdk_version_;
|
|
|
|
}
|
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
bool soinfo::prelink_image() {
|
2014-09-16 09:22:10 +02:00
|
|
|
/* Extract dynamic section */
|
|
|
|
ElfW(Word) dynamic_flags = 0;
|
|
|
|
phdr_table_get_dynamic_section(phdr, phnum, load_bias, &dynamic, &dynamic_flags);
|
2014-09-05 23:57:59 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
/* We can't log anything until the linker is relocated */
|
2014-11-29 22:57:41 +01:00
|
|
|
bool relocating_linker = (flags_ & FLAG_LINKER) != 0;
|
2014-09-12 18:43:13 +02:00
|
|
|
if (!relocating_linker) {
|
2015-04-29 03:09:53 +02:00
|
|
|
INFO("[ linking %s ]", get_realpath());
|
2014-11-29 22:57:41 +01:00
|
|
|
DEBUG("si->base = %p si->flags = 0x%08x", reinterpret_cast<void*>(base), flags_);
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
|
|
|
|
|
|
|
if (dynamic == nullptr) {
|
2012-06-19 01:24:17 +02:00
|
|
|
if (!relocating_linker) {
|
2015-05-07 19:48:00 +02:00
|
|
|
DL_ERR("missing PT_DYNAMIC in \"%s\"", get_realpath());
|
2012-06-19 01:24:17 +02:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
|
|
|
} else {
|
|
|
|
if (!relocating_linker) {
|
|
|
|
DEBUG("dynamic = %p", dynamic);
|
2012-06-19 00:08:39 +02:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
2012-06-19 00:08:39 +02:00
|
|
|
|
2013-10-26 02:38:02 +02:00
|
|
|
#if defined(__arm__)
|
2014-09-12 18:43:13 +02:00
|
|
|
(void) phdr_table_get_arm_exidx(phdr, phnum, load_bias,
|
|
|
|
&ARM_exidx, &ARM_exidx_count);
|
2012-06-19 00:08:39 +02:00
|
|
|
#endif
|
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
// Extract useful information from dynamic section.
|
2015-03-18 04:06:36 +01:00
|
|
|
// Note that: "Except for the DT_NULL element at the end of the array,
|
|
|
|
// and the relative order of DT_NEEDED elements, entries may appear in any order."
|
|
|
|
//
|
|
|
|
// source: http://www.sco.com/developers/gabi/1998-04-29/ch5.dynamic.html
|
2014-09-12 18:43:13 +02:00
|
|
|
uint32_t needed_count = 0;
|
|
|
|
for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
|
|
|
|
DEBUG("d = %p, d[0](tag) = %p d[1](val) = %p",
|
|
|
|
d, reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
|
|
|
|
switch (d->d_tag) {
|
2014-09-17 00:51:25 +02:00
|
|
|
case DT_SONAME:
|
2015-03-18 04:06:36 +01:00
|
|
|
// this is parsed after we have strtab initialized (see below).
|
2014-09-17 00:51:25 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_HASH:
|
2014-11-13 18:39:20 +01:00
|
|
|
nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
|
|
|
|
nchain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
|
|
|
|
bucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8);
|
|
|
|
chain_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr + 8 + nbucket_ * 4);
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-11-10 04:27:20 +01:00
|
|
|
case DT_GNU_HASH:
|
2015-03-09 20:02:02 +01:00
|
|
|
gnu_nbucket_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[0];
|
2014-11-10 04:27:20 +01:00
|
|
|
// skip symndx
|
2014-11-13 18:39:20 +01:00
|
|
|
gnu_maskwords_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[2];
|
|
|
|
gnu_shift2_ = reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[3];
|
2014-11-10 04:27:20 +01:00
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
gnu_bloom_filter_ = reinterpret_cast<ElfW(Addr)*>(load_bias + d->d_un.d_ptr + 16);
|
2015-03-09 20:02:02 +01:00
|
|
|
gnu_bucket_ = reinterpret_cast<uint32_t*>(gnu_bloom_filter_ + gnu_maskwords_);
|
2014-11-10 04:27:20 +01:00
|
|
|
// amend chain for symndx = header[1]
|
2015-03-31 03:43:38 +02:00
|
|
|
gnu_chain_ = gnu_bucket_ + gnu_nbucket_ -
|
|
|
|
reinterpret_cast<uint32_t*>(load_bias + d->d_un.d_ptr)[1];
|
2014-11-10 04:27:20 +01:00
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
if (!powerof2(gnu_maskwords_)) {
|
2015-03-31 03:43:38 +02:00
|
|
|
DL_ERR("invalid maskwords for gnu_hash = 0x%x, in \"%s\" expecting power to two",
|
2015-03-31 20:14:03 +02:00
|
|
|
gnu_maskwords_, get_realpath());
|
2014-11-10 04:27:20 +01:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-13 18:39:20 +01:00
|
|
|
--gnu_maskwords_;
|
2014-11-10 04:27:20 +01:00
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
flags_ |= FLAG_GNU_HASH;
|
2014-11-10 04:27:20 +01:00
|
|
|
break;
|
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_STRTAB:
|
2014-11-13 18:39:20 +01:00
|
|
|
strtab_ = reinterpret_cast<const char*>(load_bias + d->d_un.d_ptr);
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-30 04:14:45 +02:00
|
|
|
case DT_STRSZ:
|
2014-11-13 18:39:20 +01:00
|
|
|
strtab_size_ = d->d_un.d_val;
|
2014-09-30 04:14:45 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_SYMTAB:
|
2014-11-13 18:39:20 +01:00
|
|
|
symtab_ = reinterpret_cast<ElfW(Sym)*>(load_bias + d->d_un.d_ptr);
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-17 00:51:25 +02:00
|
|
|
case DT_SYMENT:
|
|
|
|
if (d->d_un.d_val != sizeof(ElfW(Sym))) {
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("invalid DT_SYMENT: %zd in \"%s\"",
|
|
|
|
static_cast<size_t>(d->d_un.d_val), get_realpath());
|
2014-09-17 00:51:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_PLTREL:
|
2014-10-06 20:30:43 +02:00
|
|
|
#if defined(USE_RELA)
|
|
|
|
if (d->d_un.d_val != DT_RELA) {
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_RELA", get_realpath());
|
2014-10-06 20:30:43 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#else
|
2014-09-12 18:43:13 +02:00
|
|
|
if (d->d_un.d_val != DT_REL) {
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("unsupported DT_PLTREL in \"%s\"; expected DT_REL", get_realpath());
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2013-10-05 02:01:33 +02:00
|
|
|
#endif
|
2014-10-06 20:30:43 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_JMPREL:
|
2013-10-26 02:38:02 +02:00
|
|
|
#if defined(USE_RELA)
|
2014-11-13 18:39:20 +01:00
|
|
|
plt_rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
|
2013-10-05 02:01:33 +02:00
|
|
|
#else
|
2014-11-13 18:39:20 +01:00
|
|
|
plt_rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
|
2013-10-05 02:01:33 +02:00
|
|
|
#endif
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_PLTRELSZ:
|
2013-10-26 02:38:02 +02:00
|
|
|
#if defined(USE_RELA)
|
2014-11-13 18:39:20 +01:00
|
|
|
plt_rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
|
2013-10-05 02:01:33 +02:00
|
|
|
#else
|
2014-11-13 18:39:20 +01:00
|
|
|
plt_rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
|
2013-10-05 02:01:33 +02:00
|
|
|
#endif
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_PLTGOT:
|
2014-09-17 00:51:25 +02:00
|
|
|
#if defined(__mips__)
|
2014-09-12 18:43:13 +02:00
|
|
|
// Used by mips and mips64.
|
2014-11-13 18:39:20 +01:00
|
|
|
plt_got_ = reinterpret_cast<ElfW(Addr)**>(load_bias + d->d_un.d_ptr);
|
2013-10-05 02:01:33 +02:00
|
|
|
#endif
|
2014-09-17 00:51:25 +02:00
|
|
|
// Ignore for other platforms... (because RTLD_LAZY is not supported)
|
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_DEBUG:
|
|
|
|
// Set the DT_DEBUG entry to the address of _r_debug for GDB
|
|
|
|
// if the dynamic table is writable
|
2014-02-07 05:36:51 +01:00
|
|
|
// FIXME: not working currently for N64
|
|
|
|
// The flags for the LOAD and DYNAMIC program headers do not agree.
|
2014-09-06 01:42:53 +02:00
|
|
|
// The LOAD section containing the dynamic table has been mapped as
|
2014-02-07 05:36:51 +01:00
|
|
|
// read-only, but the DYNAMIC header claims it is writable.
|
|
|
|
#if !(defined(__mips__) && defined(__LP64__))
|
2014-09-12 18:43:13 +02:00
|
|
|
if ((dynamic_flags & PF_W) != 0) {
|
|
|
|
d->d_un.d_val = reinterpret_cast<uintptr_t>(&_r_debug);
|
|
|
|
}
|
2014-02-07 05:36:51 +01:00
|
|
|
#endif
|
2015-02-14 01:29:50 +01:00
|
|
|
break;
|
2013-10-26 02:38:02 +02:00
|
|
|
#if defined(USE_RELA)
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_RELA:
|
2014-11-13 18:39:20 +01:00
|
|
|
rela_ = reinterpret_cast<ElfW(Rela)*>(load_bias + d->d_un.d_ptr);
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_RELASZ:
|
2014-11-13 18:39:20 +01:00
|
|
|
rela_count_ = d->d_un.d_val / sizeof(ElfW(Rela));
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2015-02-05 01:05:30 +01:00
|
|
|
case DT_ANDROID_RELA:
|
|
|
|
android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DT_ANDROID_RELASZ:
|
|
|
|
android_relocs_size_ = d->d_un.d_val;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DT_ANDROID_REL:
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("unsupported DT_ANDROID_REL in \"%s\"", get_realpath());
|
2015-02-05 01:05:30 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case DT_ANDROID_RELSZ:
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("unsupported DT_ANDROID_RELSZ in \"%s\"", get_realpath());
|
2015-02-05 01:05:30 +01:00
|
|
|
return false;
|
|
|
|
|
2014-09-17 00:51:25 +02:00
|
|
|
case DT_RELAENT:
|
|
|
|
if (d->d_un.d_val != sizeof(ElfW(Rela))) {
|
2014-09-17 08:34:20 +02:00
|
|
|
DL_ERR("invalid DT_RELAENT: %zd", static_cast<size_t>(d->d_un.d_val));
|
2014-09-17 00:51:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
|
|
|
// ignored (see DT_RELCOUNT comments for details)
|
2014-09-17 00:51:25 +02:00
|
|
|
case DT_RELACOUNT:
|
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_REL:
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("unsupported DT_REL in \"%s\"", get_realpath());
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_RELSZ:
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("unsupported DT_RELSZ in \"%s\"", get_realpath());
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
2015-02-05 01:05:30 +01:00
|
|
|
|
2013-10-05 02:01:33 +02:00
|
|
|
#else
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_REL:
|
2014-11-13 18:39:20 +01:00
|
|
|
rel_ = reinterpret_cast<ElfW(Rel)*>(load_bias + d->d_un.d_ptr);
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_RELSZ:
|
2014-11-13 18:39:20 +01:00
|
|
|
rel_count_ = d->d_un.d_val / sizeof(ElfW(Rel));
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-17 00:51:25 +02:00
|
|
|
case DT_RELENT:
|
|
|
|
if (d->d_un.d_val != sizeof(ElfW(Rel))) {
|
2014-09-17 08:34:20 +02:00
|
|
|
DL_ERR("invalid DT_RELENT: %zd", static_cast<size_t>(d->d_un.d_val));
|
2014-09-17 00:51:25 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2015-02-05 01:05:30 +01:00
|
|
|
case DT_ANDROID_REL:
|
|
|
|
android_relocs_ = reinterpret_cast<uint8_t*>(load_bias + d->d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DT_ANDROID_RELSZ:
|
|
|
|
android_relocs_size_ = d->d_un.d_val;
|
|
|
|
break;
|
|
|
|
|
|
|
|
case DT_ANDROID_RELA:
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("unsupported DT_ANDROID_RELA in \"%s\"", get_realpath());
|
2015-02-05 01:05:30 +01:00
|
|
|
return false;
|
|
|
|
|
|
|
|
case DT_ANDROID_RELASZ:
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("unsupported DT_ANDROID_RELASZ in \"%s\"", get_realpath());
|
2015-02-05 01:05:30 +01:00
|
|
|
return false;
|
|
|
|
|
2014-10-15 23:59:01 +02:00
|
|
|
// "Indicates that all RELATIVE relocations have been concatenated together,
|
|
|
|
// and specifies the RELATIVE relocation count."
|
|
|
|
//
|
|
|
|
// TODO: Spec also mentions that this can be used to optimize relocation process;
|
|
|
|
// Not currently used by bionic linker - ignored.
|
2014-09-17 00:51:25 +02:00
|
|
|
case DT_RELCOUNT:
|
|
|
|
break;
|
2015-02-05 01:05:30 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_RELA:
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("unsupported DT_RELA in \"%s\"", get_realpath());
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
2015-02-05 01:05:30 +01:00
|
|
|
|
|
|
|
case DT_RELASZ:
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("unsupported DT_RELASZ in \"%s\"", get_realpath());
|
2015-02-05 01:05:30 +01:00
|
|
|
return false;
|
|
|
|
|
2013-10-05 02:01:33 +02:00
|
|
|
#endif
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_INIT:
|
2014-11-13 18:39:20 +01:00
|
|
|
init_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
|
2015-03-31 20:14:03 +02:00
|
|
|
DEBUG("%s constructors (DT_INIT) found at %p", get_realpath(), init_func_);
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_FINI:
|
2014-11-13 18:39:20 +01:00
|
|
|
fini_func_ = reinterpret_cast<linker_function_t>(load_bias + d->d_un.d_ptr);
|
2015-03-31 20:14:03 +02:00
|
|
|
DEBUG("%s destructors (DT_FINI) found at %p", get_realpath(), fini_func_);
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_INIT_ARRAY:
|
2014-11-13 18:39:20 +01:00
|
|
|
init_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
|
2015-03-31 20:14:03 +02:00
|
|
|
DEBUG("%s constructors (DT_INIT_ARRAY) found at %p", get_realpath(), init_array_);
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_INIT_ARRAYSZ:
|
2015-01-23 01:04:25 +01:00
|
|
|
init_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_FINI_ARRAY:
|
2014-11-13 18:39:20 +01:00
|
|
|
fini_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
|
2015-03-31 20:14:03 +02:00
|
|
|
DEBUG("%s destructors (DT_FINI_ARRAY) found at %p", get_realpath(), fini_array_);
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_FINI_ARRAYSZ:
|
2015-01-23 01:04:25 +01:00
|
|
|
fini_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_PREINIT_ARRAY:
|
2014-11-13 18:39:20 +01:00
|
|
|
preinit_array_ = reinterpret_cast<linker_function_t*>(load_bias + d->d_un.d_ptr);
|
2015-03-31 20:14:03 +02:00
|
|
|
DEBUG("%s constructors (DT_PREINIT_ARRAY) found at %p", get_realpath(), preinit_array_);
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_PREINIT_ARRAYSZ:
|
2015-01-23 01:04:25 +01:00
|
|
|
preinit_array_count_ = static_cast<uint32_t>(d->d_un.d_val) / sizeof(ElfW(Addr));
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_TEXTREL:
|
2015-04-01 23:18:48 +02:00
|
|
|
#if defined(__LP64__)
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("text relocations (DT_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
2015-04-01 23:18:48 +02:00
|
|
|
#else
|
|
|
|
has_text_relocations = true;
|
|
|
|
break;
|
|
|
|
#endif
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_SYMBOLIC:
|
2014-09-29 21:10:36 +02:00
|
|
|
has_DT_SYMBOLIC = true;
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_NEEDED:
|
|
|
|
++needed_count;
|
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_FLAGS:
|
|
|
|
if (d->d_un.d_val & DF_TEXTREL) {
|
2015-04-01 23:18:48 +02:00
|
|
|
#if defined(__LP64__)
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("text relocations (DF_TEXTREL) found in 64-bit ELF file \"%s\"", get_realpath());
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
2015-04-01 23:18:48 +02:00
|
|
|
#else
|
|
|
|
has_text_relocations = true;
|
|
|
|
#endif
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
2014-09-29 21:10:36 +02:00
|
|
|
if (d->d_un.d_val & DF_SYMBOLIC) {
|
|
|
|
has_DT_SYMBOLIC = true;
|
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-30 04:14:45 +02:00
|
|
|
case DT_FLAGS_1:
|
2014-08-28 23:12:12 +02:00
|
|
|
set_dt_flags_1(d->d_un.d_val);
|
2014-09-30 04:14:45 +02:00
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
if ((d->d_un.d_val & ~SUPPORTED_DT_FLAGS_1) != 0) {
|
2015-05-28 20:44:31 +02:00
|
|
|
DL_WARN("%s: unsupported flags DT_FLAGS_1=%p", get_realpath(), reinterpret_cast<void*>(d->d_un.d_val));
|
2014-09-30 04:14:45 +02:00
|
|
|
}
|
|
|
|
break;
|
2013-10-26 02:38:02 +02:00
|
|
|
#if defined(__mips__)
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_MIPS_RLD_MAP:
|
|
|
|
// Set the DT_MIPS_RLD_MAP entry to the address of _r_debug for GDB.
|
|
|
|
{
|
|
|
|
r_debug** dp = reinterpret_cast<r_debug**>(load_bias + d->d_un.d_ptr);
|
|
|
|
*dp = &_r_debug;
|
|
|
|
}
|
|
|
|
break;
|
2014-12-19 04:12:19 +01:00
|
|
|
case DT_MIPS_RLD_MAP2:
|
|
|
|
// Set the DT_MIPS_RLD_MAP2 entry to the address of _r_debug for GDB.
|
|
|
|
{
|
2015-03-31 03:43:38 +02:00
|
|
|
r_debug** dp = reinterpret_cast<r_debug**>(
|
|
|
|
reinterpret_cast<ElfW(Addr)>(d) + d->d_un.d_val);
|
2014-12-19 04:12:19 +01:00
|
|
|
*dp = &_r_debug;
|
|
|
|
}
|
|
|
|
break;
|
2014-10-15 23:59:01 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_MIPS_RLD_VERSION:
|
|
|
|
case DT_MIPS_FLAGS:
|
|
|
|
case DT_MIPS_BASE_ADDRESS:
|
|
|
|
case DT_MIPS_UNREFEXTNO:
|
|
|
|
break;
|
2012-07-31 21:07:22 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_MIPS_SYMTABNO:
|
2014-11-13 18:39:20 +01:00
|
|
|
mips_symtabno_ = d->d_un.d_val;
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2012-07-31 21:07:22 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_MIPS_LOCAL_GOTNO:
|
2014-11-13 18:39:20 +01:00
|
|
|
mips_local_gotno_ = d->d_un.d_val;
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2012-07-31 21:07:22 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
case DT_MIPS_GOTSYM:
|
2014-11-13 18:39:20 +01:00
|
|
|
mips_gotsym_ = d->d_un.d_val;
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2013-10-26 02:38:02 +02:00
|
|
|
#endif
|
2014-10-15 23:59:01 +02:00
|
|
|
// Ignored: "Its use has been superseded by the DF_BIND_NOW flag"
|
|
|
|
case DT_BIND_NOW:
|
|
|
|
break;
|
|
|
|
|
2014-10-06 20:30:43 +02:00
|
|
|
case DT_VERSYM:
|
2015-04-09 22:42:33 +02:00
|
|
|
versym_ = reinterpret_cast<ElfW(Versym)*>(load_bias + d->d_un.d_ptr);
|
|
|
|
break;
|
|
|
|
|
2014-10-06 20:30:43 +02:00
|
|
|
case DT_VERDEF:
|
2015-04-09 22:42:33 +02:00
|
|
|
verdef_ptr_ = load_bias + d->d_un.d_ptr;
|
|
|
|
break;
|
2014-10-06 20:30:43 +02:00
|
|
|
case DT_VERDEFNUM:
|
2015-04-09 22:42:33 +02:00
|
|
|
verdef_cnt_ = d->d_un.d_val;
|
|
|
|
break;
|
|
|
|
|
2014-12-02 13:32:25 +01:00
|
|
|
case DT_VERNEED:
|
2015-04-09 22:42:33 +02:00
|
|
|
verneed_ptr_ = load_bias + d->d_un.d_ptr;
|
|
|
|
break;
|
|
|
|
|
2014-12-02 13:32:25 +01:00
|
|
|
case DT_VERNEEDNUM:
|
2015-04-09 22:42:33 +02:00
|
|
|
verneed_cnt_ = d->d_un.d_val;
|
2014-10-06 20:30:43 +02:00
|
|
|
break;
|
2012-07-31 21:07:22 +02:00
|
|
|
|
2015-06-10 22:38:39 +02:00
|
|
|
case DT_RUNPATH:
|
|
|
|
// this is parsed after we have strtab initialized (see below).
|
|
|
|
break;
|
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
default:
|
2014-09-16 23:31:06 +02:00
|
|
|
if (!relocating_linker) {
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_WARN("%s: unused DT entry: type %p arg %p", get_realpath(),
|
2014-09-16 23:31:06 +02:00
|
|
|
reinterpret_cast<void*>(d->d_tag), reinterpret_cast<void*>(d->d_un.d_val));
|
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
break;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-06-02 01:29:14 +02:00
|
|
|
#if defined(__mips__) && !defined(__LP64__)
|
|
|
|
if (!mips_check_and_adjust_fp_modes()) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
DEBUG("si->base = %p, si->strtab = %p, si->symtab = %p",
|
2014-11-13 18:39:20 +01:00
|
|
|
reinterpret_cast<void*>(base), strtab_, symtab_);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
// Sanity checks.
|
|
|
|
if (relocating_linker && needed_count != 0) {
|
|
|
|
DL_ERR("linker cannot have DT_NEEDED dependencies on other libraries");
|
|
|
|
return false;
|
|
|
|
}
|
2015-03-09 20:02:02 +01:00
|
|
|
if (nbucket_ == 0 && gnu_nbucket_ == 0) {
|
2015-03-31 20:14:03 +02:00
|
|
|
DL_ERR("empty/missing DT_HASH/DT_GNU_HASH in \"%s\" "
|
2015-05-07 19:48:00 +02:00
|
|
|
"(new hash type from the future?)", get_realpath());
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-13 18:39:20 +01:00
|
|
|
if (strtab_ == 0) {
|
2015-05-07 19:48:00 +02:00
|
|
|
DL_ERR("empty/missing DT_STRTAB in \"%s\"", get_realpath());
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2014-11-13 18:39:20 +01:00
|
|
|
if (symtab_ == 0) {
|
2015-05-07 19:48:00 +02:00
|
|
|
DL_ERR("empty/missing DT_SYMTAB in \"%s\"", get_realpath());
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2015-06-02 22:28:06 +02:00
|
|
|
|
2015-06-08 19:41:33 +02:00
|
|
|
// second pass - parse entries relying on strtab
|
|
|
|
for (ElfW(Dyn)* d = dynamic; d->d_tag != DT_NULL; ++d) {
|
2015-06-10 22:38:39 +02:00
|
|
|
switch (d->d_tag) {
|
|
|
|
case DT_SONAME:
|
|
|
|
soname_ = get_string(d->d_un.d_val);
|
2015-09-28 19:14:17 +02:00
|
|
|
#if defined(__work_around_b_24465209__)
|
2015-06-10 22:38:39 +02:00
|
|
|
strlcpy(old_name_, soname_, sizeof(old_name_));
|
2015-06-08 19:41:33 +02:00
|
|
|
#endif
|
2015-06-10 22:38:39 +02:00
|
|
|
break;
|
|
|
|
case DT_RUNPATH:
|
|
|
|
// FIXME: $LIB, $PLATFORM unsupported.
|
|
|
|
set_dt_runpath(get_string(d->d_un.d_val));
|
|
|
|
break;
|
2015-06-08 19:41:33 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2015-06-02 22:28:06 +02:00
|
|
|
// Before M release linker was using basename in place of soname.
|
2015-06-03 02:36:54 +02:00
|
|
|
// In the case when dt_soname is absent some apps stop working
|
2015-06-02 22:28:06 +02:00
|
|
|
// because they can't find dt_needed library by soname.
|
|
|
|
// This workaround should keep them working. (applies only
|
2015-06-03 02:36:54 +02:00
|
|
|
// for apps targeting sdk version <=22). Make an exception for
|
|
|
|
// the main executable and linker; they do not need to have dt_soname
|
|
|
|
if (soname_ == nullptr && this != somain && (flags_ & FLAG_LINKER) == 0 &&
|
|
|
|
get_application_target_sdk_version() <= 22) {
|
2015-06-02 22:28:06 +02:00
|
|
|
soname_ = basename(realpath_.c_str());
|
|
|
|
DL_WARN("%s: is missing DT_SONAME will use basename as a replacement: \"%s\"",
|
|
|
|
get_realpath(), soname_);
|
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
return true;
|
2014-09-06 01:42:53 +02:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-02-05 01:05:30 +01:00
|
|
|
bool soinfo::link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
|
|
|
|
const android_dlextinfo* extinfo) {
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
local_group_root_ = local_group.front();
|
|
|
|
if (local_group_root_ == nullptr) {
|
|
|
|
local_group_root_ = this;
|
|
|
|
}
|
|
|
|
|
2015-06-03 02:36:54 +02:00
|
|
|
if ((flags_ & FLAG_LINKER) == 0 && local_group_root_ == this) {
|
|
|
|
target_sdk_version_ = get_application_target_sdk_version();
|
|
|
|
}
|
|
|
|
|
2015-05-01 04:49:19 +02:00
|
|
|
VersionTracker version_tracker;
|
|
|
|
|
|
|
|
if (!version_tracker.init(this)) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
|
2015-04-01 23:18:48 +02:00
|
|
|
#if !defined(__LP64__)
|
|
|
|
if (has_text_relocations) {
|
2015-06-13 00:00:31 +02:00
|
|
|
// Fail if app is targeting sdk version > 22
|
2015-10-09 22:58:46 +02:00
|
|
|
if (get_application_target_sdk_version() > 22) {
|
2015-10-13 20:07:56 +02:00
|
|
|
PRINT("%s: has text relocations", get_realpath());
|
2015-06-13 00:00:31 +02:00
|
|
|
DL_ERR("%s: has text relocations", get_realpath());
|
|
|
|
return false;
|
|
|
|
}
|
2015-04-01 23:18:48 +02:00
|
|
|
// Make segments writable to allow text relocations to work properly. We will later call
|
2015-10-01 23:02:19 +02:00
|
|
|
// phdr_table_protect_segments() after all of them are applied.
|
2015-04-01 23:18:48 +02:00
|
|
|
DL_WARN("%s has text relocations. This is wasting memory and prevents "
|
2015-05-07 19:48:00 +02:00
|
|
|
"security hardening. Please fix.", get_realpath());
|
2015-04-01 23:18:48 +02:00
|
|
|
if (phdr_table_unprotect_segments(phdr, phnum, load_bias) < 0) {
|
|
|
|
DL_ERR("can't unprotect loadable segments for \"%s\": %s",
|
2015-05-07 19:48:00 +02:00
|
|
|
get_realpath(), strerror(errno));
|
2015-04-01 23:18:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2015-02-05 01:05:30 +01:00
|
|
|
if (android_relocs_ != nullptr) {
|
|
|
|
// check signature
|
|
|
|
if (android_relocs_size_ > 3 &&
|
|
|
|
android_relocs_[0] == 'A' &&
|
|
|
|
android_relocs_[1] == 'P' &&
|
2015-04-22 22:10:04 +02:00
|
|
|
android_relocs_[2] == 'S' &&
|
2015-02-05 01:05:30 +01:00
|
|
|
android_relocs_[3] == '2') {
|
2015-05-07 19:48:00 +02:00
|
|
|
DEBUG("[ android relocating %s ]", get_realpath());
|
2015-02-05 01:05:30 +01:00
|
|
|
|
|
|
|
bool relocated = false;
|
|
|
|
const uint8_t* packed_relocs = android_relocs_ + 4;
|
|
|
|
const size_t packed_relocs_size = android_relocs_size_ - 4;
|
|
|
|
|
2015-04-22 22:10:04 +02:00
|
|
|
relocated = relocate(
|
2015-05-01 04:49:19 +02:00
|
|
|
version_tracker,
|
2015-04-22 22:10:04 +02:00
|
|
|
packed_reloc_iterator<sleb128_decoder>(
|
|
|
|
sleb128_decoder(packed_relocs, packed_relocs_size)),
|
|
|
|
global_group, local_group);
|
2015-02-05 01:05:30 +01:00
|
|
|
|
|
|
|
if (!relocated) {
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
DL_ERR("bad android relocation header.");
|
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2013-10-26 02:38:02 +02:00
|
|
|
#if defined(USE_RELA)
|
2014-11-13 18:39:20 +01:00
|
|
|
if (rela_ != nullptr) {
|
2015-05-07 19:48:00 +02:00
|
|
|
DEBUG("[ relocating %s ]", get_realpath());
|
2015-05-01 04:49:19 +02:00
|
|
|
if (!relocate(version_tracker,
|
|
|
|
plain_reloc_iterator(rela_, rela_count_), global_group, local_group)) {
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
2013-10-05 02:01:33 +02:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
2014-11-13 18:39:20 +01:00
|
|
|
if (plt_rela_ != nullptr) {
|
2015-05-07 19:48:00 +02:00
|
|
|
DEBUG("[ relocating %s plt ]", get_realpath());
|
2015-05-01 04:49:19 +02:00
|
|
|
if (!relocate(version_tracker,
|
|
|
|
plain_reloc_iterator(plt_rela_, plt_rela_count_), global_group, local_group)) {
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
2014-09-12 00:16:03 +02:00
|
|
|
#else
|
2014-11-13 18:39:20 +01:00
|
|
|
if (rel_ != nullptr) {
|
2015-05-07 19:48:00 +02:00
|
|
|
DEBUG("[ relocating %s ]", get_realpath());
|
2015-05-01 04:49:19 +02:00
|
|
|
if (!relocate(version_tracker,
|
|
|
|
plain_reloc_iterator(rel_, rel_count_), global_group, local_group)) {
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
2014-11-13 18:39:20 +01:00
|
|
|
if (plt_rel_ != nullptr) {
|
2015-05-07 19:48:00 +02:00
|
|
|
DEBUG("[ relocating %s plt ]", get_realpath());
|
2015-05-01 04:49:19 +02:00
|
|
|
if (!relocate(version_tracker,
|
|
|
|
plain_reloc_iterator(plt_rel_, plt_rel_count_), global_group, local_group)) {
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
2014-07-23 20:22:25 +02:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
2014-09-12 00:16:03 +02:00
|
|
|
#endif
|
2014-07-23 20:22:25 +02:00
|
|
|
|
2013-10-26 02:38:02 +02:00
|
|
|
#if defined(__mips__)
|
2015-05-01 05:17:03 +02:00
|
|
|
if (!mips_relocate_got(version_tracker, global_group, local_group)) {
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-07-31 21:07:22 +02:00
|
|
|
#endif
|
|
|
|
|
2015-05-07 19:48:00 +02:00
|
|
|
DEBUG("[ finished linking %s ]", get_realpath());
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-04-01 23:18:48 +02:00
|
|
|
#if !defined(__LP64__)
|
|
|
|
if (has_text_relocations) {
|
|
|
|
// All relocations are done, we can protect our segments back to read-only.
|
|
|
|
if (phdr_table_protect_segments(phdr, phnum, load_bias) < 0) {
|
|
|
|
DL_ERR("can't protect segments for \"%s\": %s",
|
2015-05-07 19:48:00 +02:00
|
|
|
get_realpath(), strerror(errno));
|
2015-04-01 23:18:48 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
|
|
|
#endif
|
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
/* We can also turn on GNU RELRO protection */
|
|
|
|
if (phdr_table_protect_gnu_relro(phdr, phnum, load_bias) < 0) {
|
|
|
|
DL_ERR("can't enable GNU RELRO protection for \"%s\": %s",
|
2015-05-07 19:48:00 +02:00
|
|
|
get_realpath(), strerror(errno));
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
2012-02-28 19:40:00 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
/* Handle serializing/sharing the RELRO segment */
|
|
|
|
if (extinfo && (extinfo->flags & ANDROID_DLEXT_WRITE_RELRO)) {
|
|
|
|
if (phdr_table_serialize_gnu_relro(phdr, phnum, load_bias,
|
|
|
|
extinfo->relro_fd) < 0) {
|
|
|
|
DL_ERR("failed serializing GNU RELRO section for \"%s\": %s",
|
2015-05-07 19:48:00 +02:00
|
|
|
get_realpath(), strerror(errno));
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
2014-02-27 14:18:00 +01:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
} else if (extinfo && (extinfo->flags & ANDROID_DLEXT_USE_RELRO)) {
|
|
|
|
if (phdr_table_map_gnu_relro(phdr, phnum, load_bias,
|
|
|
|
extinfo->relro_fd) < 0) {
|
|
|
|
DL_ERR("failed mapping GNU RELRO section for \"%s\": %s",
|
2015-05-07 19:48:00 +02:00
|
|
|
get_realpath(), strerror(errno));
|
2014-09-12 18:43:13 +02:00
|
|
|
return false;
|
|
|
|
}
|
|
|
|
}
|
2014-02-27 14:18:00 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
notify_gdb_of_load(this);
|
|
|
|
return true;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
|
|
|
|
2013-01-25 13:40:13 +01:00
|
|
|
/*
|
|
|
|
* This function add vdso to internal dso list.
|
|
|
|
* It helps to stack unwinding through signal handlers.
|
|
|
|
* Also, it makes bionic more like glibc.
|
|
|
|
*/
|
2014-03-25 15:53:56 +01:00
|
|
|
static void add_vdso(KernelArgumentBlock& args __unused) {
|
2013-10-26 02:38:02 +02:00
|
|
|
#if defined(AT_SYSINFO_EHDR)
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Ehdr)* ehdr_vdso = reinterpret_cast<ElfW(Ehdr)*>(args.getauxval(AT_SYSINFO_EHDR));
|
2014-08-29 21:02:36 +02:00
|
|
|
if (ehdr_vdso == nullptr) {
|
2014-02-11 02:46:57 +01:00
|
|
|
return;
|
|
|
|
}
|
2013-01-25 13:40:13 +01:00
|
|
|
|
2014-10-04 02:52:44 +02:00
|
|
|
soinfo* si = soinfo_alloc("[vdso]", nullptr, 0, 0);
|
2013-10-31 15:02:12 +01:00
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
si->phdr = reinterpret_cast<ElfW(Phdr)*>(reinterpret_cast<char*>(ehdr_vdso) + ehdr_vdso->e_phoff);
|
|
|
|
si->phnum = ehdr_vdso->e_phnum;
|
|
|
|
si->base = reinterpret_cast<ElfW(Addr)>(ehdr_vdso);
|
|
|
|
si->size = phdr_table_get_load_size(si->phdr, si->phnum);
|
|
|
|
si->load_bias = get_elf_exec_load_bias(ehdr_vdso);
|
2013-10-31 15:02:12 +01:00
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
si->prelink_image();
|
|
|
|
si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr);
|
2013-01-25 13:40:13 +01:00
|
|
|
#endif
|
|
|
|
}
|
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
/*
|
|
|
|
* This is linker soinfo for GDB. See details below.
|
|
|
|
*/
|
2014-08-22 21:25:04 +02:00
|
|
|
#if defined(__LP64__)
|
|
|
|
#define LINKER_PATH "/system/bin/linker64"
|
|
|
|
#else
|
|
|
|
#define LINKER_PATH "/system/bin/linker"
|
|
|
|
#endif
|
2015-03-31 20:14:03 +02:00
|
|
|
|
|
|
|
// This is done to avoid calling c-tor prematurely
|
|
|
|
// because soinfo c-tor needs memory allocator
|
|
|
|
// which might be initialized after global variables.
|
|
|
|
static uint8_t linker_soinfo_for_gdb_buf[sizeof(soinfo)] __attribute__((aligned(8)));
|
|
|
|
static soinfo* linker_soinfo_for_gdb = nullptr;
|
2014-05-09 18:10:14 +02:00
|
|
|
|
|
|
|
/* gdb expects the linker to be in the debug shared object list.
|
|
|
|
* Without this, gdb has trouble locating the linker's ".text"
|
|
|
|
* and ".plt" sections. Gdb could also potentially use this to
|
|
|
|
* relocate the offset of our exported 'rtld_db_dlactivity' symbol.
|
|
|
|
* Don't use soinfo_alloc(), because the linker shouldn't
|
|
|
|
* be on the soinfo list.
|
|
|
|
*/
|
|
|
|
static void init_linker_info_for_gdb(ElfW(Addr) linker_base) {
|
2015-03-31 20:14:03 +02:00
|
|
|
linker_soinfo_for_gdb = new (linker_soinfo_for_gdb_buf) soinfo(LINKER_PATH, nullptr, 0, 0);
|
|
|
|
|
2015-06-11 04:46:19 +02:00
|
|
|
linker_soinfo_for_gdb->load_bias = linker_base;
|
2014-05-09 18:10:14 +02:00
|
|
|
|
|
|
|
/*
|
|
|
|
* Set the dynamic field in the link map otherwise gdb will complain with
|
|
|
|
* the following:
|
|
|
|
* warning: .dynamic section for "/system/bin/linker" is not at the
|
|
|
|
* expected address (wrong library or version mismatch?)
|
|
|
|
*/
|
|
|
|
ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_base);
|
|
|
|
ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_base + elf_hdr->e_phoff);
|
|
|
|
phdr_table_get_dynamic_section(phdr, elf_hdr->e_phnum, linker_base,
|
2015-03-31 20:14:03 +02:00
|
|
|
&linker_soinfo_for_gdb->dynamic, nullptr);
|
|
|
|
insert_soinfo_into_debug_map(linker_soinfo_for_gdb);
|
2014-05-09 18:10:14 +02:00
|
|
|
}
|
|
|
|
|
2015-07-11 02:54:01 +02:00
|
|
|
static void init_default_ld_library_path() {
|
|
|
|
const char *interp = phdr_table_get_interpreter_name(somain->phdr, somain->phnum,
|
|
|
|
somain->load_bias);
|
|
|
|
const char* bname = basename(interp);
|
|
|
|
if (bname && (strcmp(bname, "linker_asan") == 0 || strcmp(bname, "linker_asan64") == 0))
|
|
|
|
g_default_ld_paths = kAsanDefaultLdPaths;
|
|
|
|
else
|
|
|
|
g_default_ld_paths = kDefaultLdPaths;
|
|
|
|
};
|
|
|
|
|
2015-04-29 00:49:26 +02:00
|
|
|
extern "C" int __system_properties_init(void);
|
|
|
|
|
2011-11-12 00:53:17 +01:00
|
|
|
/*
|
|
|
|
* This code is called after the linker has linked itself and
|
|
|
|
* fixed it's own GOT. It is safe to make references to externs
|
|
|
|
* and other non-local data at this point.
|
|
|
|
*/
|
2014-02-11 02:46:57 +01:00
|
|
|
static ElfW(Addr) __linker_init_post_relocation(KernelArgumentBlock& args, ElfW(Addr) linker_base) {
|
2012-03-22 15:01:53 +01:00
|
|
|
#if TIMING
|
2014-09-12 18:43:13 +02:00
|
|
|
struct timeval t0, t1;
|
|
|
|
gettimeofday(&t0, 0);
|
2012-03-22 15:01:53 +01:00
|
|
|
#endif
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-06-09 03:04:00 +02:00
|
|
|
// Sanitize the environment.
|
|
|
|
__libc_init_AT_SECURE(args);
|
2010-12-16 19:52:02 +01:00
|
|
|
|
2015-04-29 00:49:26 +02:00
|
|
|
// Initialize system properties
|
|
|
|
__system_properties_init(); // may use 'environ'
|
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
debuggerd_init();
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
// Get a few environment variables.
|
2015-06-09 03:04:00 +02:00
|
|
|
const char* LD_DEBUG = getenv("LD_DEBUG");
|
2014-09-12 18:43:13 +02:00
|
|
|
if (LD_DEBUG != nullptr) {
|
|
|
|
g_ld_debug_verbosity = atoi(LD_DEBUG);
|
|
|
|
}
|
2010-12-16 19:52:02 +01:00
|
|
|
|
2015-06-09 03:04:00 +02:00
|
|
|
// These should have been sanitized by __libc_init_AT_SECURE, but the test
|
2014-09-12 18:43:13 +02:00
|
|
|
// doesn't cost us anything.
|
|
|
|
const char* ldpath_env = nullptr;
|
|
|
|
const char* ldpreload_env = nullptr;
|
2015-06-09 03:04:00 +02:00
|
|
|
if (!getauxval(AT_SECURE)) {
|
|
|
|
ldpath_env = getenv("LD_LIBRARY_PATH");
|
|
|
|
ldpreload_env = getenv("LD_PRELOAD");
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
INFO("[ android linker & debugger ]");
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-10-04 02:52:44 +02:00
|
|
|
soinfo* si = soinfo_alloc(args.argv[0], nullptr, 0, RTLD_GLOBAL);
|
2014-09-12 18:43:13 +02:00
|
|
|
if (si == nullptr) {
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
/* bootstrap the link map, the main exe always needs to be first */
|
2014-11-29 22:57:41 +01:00
|
|
|
si->set_main_executable();
|
2014-09-12 18:43:13 +02:00
|
|
|
link_map* map = &(si->link_map_head);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
map->l_addr = 0;
|
|
|
|
map->l_name = args.argv[0];
|
|
|
|
map->l_prev = nullptr;
|
|
|
|
map->l_next = nullptr;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
_r_debug.r_map = map;
|
|
|
|
r_debug_tail = map;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
init_linker_info_for_gdb(linker_base);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
// Extract information passed from the kernel.
|
|
|
|
si->phdr = reinterpret_cast<ElfW(Phdr)*>(args.getauxval(AT_PHDR));
|
|
|
|
si->phnum = args.getauxval(AT_PHNUM);
|
|
|
|
si->entry = args.getauxval(AT_ENTRY);
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
/* Compute the value of si->base. We can't rely on the fact that
|
|
|
|
* the first entry is the PHDR because this will not be true
|
|
|
|
* for certain executables (e.g. some in the NDK unit test suite)
|
|
|
|
*/
|
|
|
|
si->base = 0;
|
|
|
|
si->size = phdr_table_get_load_size(si->phdr, si->phnum);
|
|
|
|
si->load_bias = 0;
|
|
|
|
for (size_t i = 0; i < si->phnum; ++i) {
|
|
|
|
if (si->phdr[i].p_type == PT_PHDR) {
|
|
|
|
si->load_bias = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_vaddr;
|
|
|
|
si->base = reinterpret_cast<ElfW(Addr)>(si->phdr) - si->phdr[i].p_offset;
|
|
|
|
break;
|
2011-11-15 17:17:28 +01:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
|
|
|
si->dynamic = nullptr;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(si->base);
|
|
|
|
if (elf_hdr->e_type != ET_DYN) {
|
|
|
|
__libc_format_fd(2, "error: only position independent executables (PIE) are supported.\n");
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-05-07 19:32:39 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
// Use LD_LIBRARY_PATH and LD_PRELOAD (but only if we aren't setuid/setgid).
|
|
|
|
parse_LD_LIBRARY_PATH(ldpath_env);
|
|
|
|
parse_LD_PRELOAD(ldpreload_env);
|
2009-12-31 19:09:10 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
somain = si;
|
2012-08-30 12:48:32 +02:00
|
|
|
|
2015-07-11 02:54:01 +02:00
|
|
|
init_default_ld_library_path();
|
|
|
|
|
2015-01-08 00:48:25 +01:00
|
|
|
if (!si->prelink_image()) {
|
|
|
|
__libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
2014-09-06 01:42:53 +02:00
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
// add somain to global group
|
|
|
|
si->set_dt_flags_1(si->get_dt_flags_1() | DF_1_GLOBAL);
|
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
// Load ld_preloads and dependencies.
|
|
|
|
StringLinkedList needed_library_name_list;
|
|
|
|
size_t needed_libraries_count = 0;
|
|
|
|
size_t ld_preloads_count = 0;
|
2015-03-24 02:43:02 +01:00
|
|
|
|
|
|
|
for (const auto& ld_preload_name : g_ld_preload_names) {
|
|
|
|
needed_library_name_list.push_back(ld_preload_name.c_str());
|
2014-09-12 18:43:13 +02:00
|
|
|
++needed_libraries_count;
|
2015-04-29 03:09:53 +02:00
|
|
|
++ld_preloads_count;
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
2014-09-06 01:42:53 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
for_each_dt_needed(si, [&](const char* name) {
|
|
|
|
needed_library_name_list.push_back(name);
|
|
|
|
++needed_libraries_count;
|
|
|
|
});
|
2014-09-06 01:42:53 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
const char* needed_library_names[needed_libraries_count];
|
2014-09-06 01:42:53 +02:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
memset(needed_library_names, 0, sizeof(needed_library_names));
|
|
|
|
needed_library_name_list.copy_to_array(needed_library_names, needed_libraries_count);
|
2014-09-06 01:42:53 +02:00
|
|
|
|
2015-03-24 02:43:02 +01:00
|
|
|
if (needed_libraries_count > 0 &&
|
|
|
|
!find_libraries(si, needed_library_names, needed_libraries_count, nullptr,
|
2015-07-07 02:56:31 +02:00
|
|
|
&g_ld_preloads, ld_preloads_count, RTLD_GLOBAL, nullptr,
|
|
|
|
/* add_as_children */ true)) {
|
2014-09-12 18:43:13 +02:00
|
|
|
__libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
|
|
|
|
exit(EXIT_FAILURE);
|
2014-11-29 22:57:41 +01:00
|
|
|
} else if (needed_libraries_count == 0) {
|
|
|
|
if (!si->link_image(g_empty_list, soinfo::soinfo_list_t::make_list(si), nullptr)) {
|
|
|
|
__libc_format_fd(2, "CANNOT LINK EXECUTABLE: %s\n", linker_get_error_buffer());
|
|
|
|
exit(EXIT_FAILURE);
|
|
|
|
}
|
|
|
|
si->increment_ref_count();
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-09-12 18:43:13 +02:00
|
|
|
add_vdso(args);
|
2013-01-25 13:40:13 +01:00
|
|
|
|
2015-01-23 21:03:53 +01:00
|
|
|
{
|
|
|
|
ProtectedDataGuard guard;
|
2012-08-13 15:58:37 +02:00
|
|
|
|
2015-01-23 21:03:53 +01:00
|
|
|
si->call_pre_init_constructors();
|
|
|
|
|
|
|
|
/* After the prelink_image, the si->load_bias is initialized.
|
|
|
|
* For so lib, the map->l_addr will be updated in notify_gdb_of_load.
|
|
|
|
* We need to update this value for so exe here. So Unwind_Backtrace
|
|
|
|
* for some arch like x86 could work correctly within so exe.
|
|
|
|
*/
|
|
|
|
map->l_addr = si->load_bias;
|
|
|
|
si->call_constructors();
|
|
|
|
}
|
2011-12-21 10:03:54 +01:00
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
#if TIMING
|
2014-09-12 18:43:13 +02:00
|
|
|
gettimeofday(&t1, nullptr);
|
|
|
|
PRINT("LINKER TIME: %s: %d microseconds", args.argv[0], (int) (
|
|
|
|
(((long long)t1.tv_sec * 1000000LL) + (long long)t1.tv_usec) -
|
|
|
|
(((long long)t0.tv_sec * 1000000LL) + (long long)t0.tv_usec)));
|
2009-03-04 04:28:35 +01:00
|
|
|
#endif
|
|
|
|
#if STATS
|
2014-09-12 18:43:13 +02:00
|
|
|
PRINT("RELO STATS: %s: %d abs, %d rel, %d copy, %d symbol", args.argv[0],
|
|
|
|
linker_stats.count[kRelocAbsolute],
|
|
|
|
linker_stats.count[kRelocRelative],
|
|
|
|
linker_stats.count[kRelocCopy],
|
|
|
|
linker_stats.count[kRelocSymbol]);
|
2009-03-04 04:28:35 +01:00
|
|
|
#endif
|
|
|
|
#if COUNT_PAGES
|
2014-09-12 18:43:13 +02:00
|
|
|
{
|
|
|
|
unsigned n;
|
|
|
|
unsigned i;
|
|
|
|
unsigned count = 0;
|
|
|
|
for (n = 0; n < 4096; n++) {
|
|
|
|
if (bitmask[n]) {
|
|
|
|
unsigned x = bitmask[n];
|
2013-10-10 16:19:31 +02:00
|
|
|
#if defined(__LP64__)
|
2014-09-12 18:43:13 +02:00
|
|
|
for (i = 0; i < 32; i++) {
|
2013-10-10 16:19:31 +02:00
|
|
|
#else
|
2014-09-12 18:43:13 +02:00
|
|
|
for (i = 0; i < 8; i++) {
|
2013-10-10 16:19:31 +02:00
|
|
|
#endif
|
2014-09-12 18:43:13 +02:00
|
|
|
if (x & 1) {
|
|
|
|
count++;
|
|
|
|
}
|
|
|
|
x >>= 1;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2014-09-12 18:43:13 +02:00
|
|
|
PRINT("PAGES MODIFIED: %s: %d (%dKB)", args.argv[0], count, count * 4);
|
|
|
|
}
|
2009-03-04 04:28:35 +01:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if TIMING || STATS || COUNT_PAGES
|
2014-09-12 18:43:13 +02:00
|
|
|
fflush(stdout);
|
2009-03-04 04:28:35 +01:00
|
|
|
#endif
|
|
|
|
|
2015-05-07 19:48:00 +02:00
|
|
|
TRACE("[ Ready to execute '%s' @ %p ]", si->get_realpath(), reinterpret_cast<void*>(si->entry));
|
2014-09-12 18:43:13 +02:00
|
|
|
return si->entry;
|
2009-03-04 04:28:35 +01:00
|
|
|
}
|
2011-11-12 00:53:17 +01:00
|
|
|
|
2012-06-18 23:38:46 +02:00
|
|
|
/* Compute the load-bias of an existing executable. This shall only
|
|
|
|
* be used to compute the load bias of an executable or shared library
|
|
|
|
* that was loaded by the kernel itself.
|
|
|
|
*
|
|
|
|
* Input:
|
|
|
|
* elf -> address of ELF header, assumed to be at the start of the file.
|
|
|
|
* Return:
|
|
|
|
* load bias, i.e. add the value of any p_vaddr in the file to get
|
|
|
|
* the corresponding address in memory.
|
|
|
|
*/
|
2014-02-11 02:46:57 +01:00
|
|
|
static ElfW(Addr) get_elf_exec_load_bias(const ElfW(Ehdr)* elf) {
|
|
|
|
ElfW(Addr) offset = elf->e_phoff;
|
2015-05-07 19:48:00 +02:00
|
|
|
const ElfW(Phdr)* phdr_table =
|
|
|
|
reinterpret_cast<const ElfW(Phdr)*>(reinterpret_cast<uintptr_t>(elf) + offset);
|
2014-02-11 02:46:57 +01:00
|
|
|
const ElfW(Phdr)* phdr_end = phdr_table + elf->e_phnum;
|
2013-03-12 07:58:06 +01:00
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
for (const ElfW(Phdr)* phdr = phdr_table; phdr < phdr_end; phdr++) {
|
2013-03-12 07:58:06 +01:00
|
|
|
if (phdr->p_type == PT_LOAD) {
|
2014-02-11 02:46:57 +01:00
|
|
|
return reinterpret_cast<ElfW(Addr)>(elf) + phdr->p_offset - phdr->p_vaddr;
|
2012-06-18 23:38:46 +02:00
|
|
|
}
|
2013-03-12 07:58:06 +01:00
|
|
|
}
|
|
|
|
return 0;
|
2012-06-18 23:38:46 +02:00
|
|
|
}
|
|
|
|
|
2014-07-29 00:05:51 +02:00
|
|
|
extern "C" void _start();
|
|
|
|
|
2011-11-12 00:53:17 +01:00
|
|
|
/*
|
|
|
|
* This is the entry point for the linker, called from begin.S. This
|
|
|
|
* method is responsible for fixing the linker's own relocations, and
|
|
|
|
* then calling __linker_init_post_relocation().
|
|
|
|
*
|
|
|
|
* Because this method is called before the linker has fixed it's own
|
|
|
|
* relocations, any attempt to reference an extern variable, extern
|
|
|
|
* function, or other GOT reference will generate a segfault.
|
|
|
|
*/
|
2014-02-11 02:46:57 +01:00
|
|
|
extern "C" ElfW(Addr) __linker_init(void* raw_args) {
|
2013-02-07 19:14:39 +01:00
|
|
|
KernelArgumentBlock args(raw_args);
|
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Addr) linker_addr = args.getauxval(AT_BASE);
|
2014-07-29 00:05:51 +02:00
|
|
|
ElfW(Addr) entry_point = args.getauxval(AT_ENTRY);
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Ehdr)* elf_hdr = reinterpret_cast<ElfW(Ehdr)*>(linker_addr);
|
2014-02-12 01:59:37 +01:00
|
|
|
ElfW(Phdr)* phdr = reinterpret_cast<ElfW(Phdr)*>(linker_addr + elf_hdr->e_phoff);
|
2013-02-07 19:14:39 +01:00
|
|
|
|
2015-03-31 20:14:03 +02:00
|
|
|
soinfo linker_so(nullptr, nullptr, 0, 0);
|
2013-02-07 19:14:39 +01:00
|
|
|
|
2014-07-29 00:05:51 +02:00
|
|
|
// If the linker is not acting as PT_INTERP entry_point is equal to
|
|
|
|
// _start. Which means that the linker is running as an executable and
|
|
|
|
// already linked by PT_INTERP.
|
|
|
|
//
|
|
|
|
// This happens when user tries to run 'adb shell /system/bin/linker'
|
|
|
|
// see also https://code.google.com/p/android/issues/detail?id=63174
|
|
|
|
if (reinterpret_cast<ElfW(Addr)>(&_start) == entry_point) {
|
|
|
|
__libc_fatal("This is %s, the helper program for shared library executables.\n", args.argv[0]);
|
|
|
|
}
|
|
|
|
|
2013-02-07 19:14:39 +01:00
|
|
|
linker_so.base = linker_addr;
|
|
|
|
linker_so.size = phdr_table_get_load_size(phdr, elf_hdr->e_phnum);
|
|
|
|
linker_so.load_bias = get_elf_exec_load_bias(elf_hdr);
|
2014-08-29 21:02:36 +02:00
|
|
|
linker_so.dynamic = nullptr;
|
2013-02-07 19:14:39 +01:00
|
|
|
linker_so.phdr = phdr;
|
|
|
|
linker_so.phnum = elf_hdr->e_phnum;
|
2014-11-29 22:57:41 +01:00
|
|
|
linker_so.set_linker_flag();
|
2013-02-07 19:14:39 +01:00
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
// This might not be obvious... The reasons why we pass g_empty_list
|
|
|
|
// in place of local_group here are (1) we do not really need it, because
|
|
|
|
// linker is built with DT_SYMBOLIC and therefore relocates its symbols against
|
|
|
|
// itself without having to look into local_group and (2) allocators
|
|
|
|
// are not yet initialized, and therefore we cannot use linked_list.push_*
|
|
|
|
// functions at this point.
|
2014-11-13 18:39:20 +01:00
|
|
|
if (!(linker_so.prelink_image() && linker_so.link_image(g_empty_list, g_empty_list, nullptr))) {
|
2013-02-07 19:14:39 +01:00
|
|
|
// It would be nice to print an error message, but if the linker
|
|
|
|
// can't link itself, there's no guarantee that we'll be able to
|
2013-12-22 01:07:45 +01:00
|
|
|
// call write() (because it involves a GOT reference). We may as
|
|
|
|
// well try though...
|
|
|
|
const char* msg = "CANNOT LINK EXECUTABLE: ";
|
|
|
|
write(2, msg, strlen(msg));
|
|
|
|
write(2, __linker_dl_err_buf, strlen(__linker_dl_err_buf));
|
|
|
|
write(2, "\n", 1);
|
|
|
|
_exit(EXIT_FAILURE);
|
2013-02-07 19:14:39 +01:00
|
|
|
}
|
2011-11-12 00:53:17 +01:00
|
|
|
|
2015-07-21 20:57:09 +02:00
|
|
|
__libc_init_main_thread(args);
|
2014-08-26 23:16:52 +02:00
|
|
|
|
2015-10-06 20:08:13 +02:00
|
|
|
// Initialize the linker's static libc's globals
|
|
|
|
__libc_init_globals(args);
|
|
|
|
|
2014-07-29 00:05:51 +02:00
|
|
|
// Initialize the linker's own global variables
|
2014-11-13 18:39:20 +01:00
|
|
|
linker_so.call_constructors();
|
2014-07-25 00:33:25 +02:00
|
|
|
|
2014-08-22 21:25:04 +02:00
|
|
|
// Initialize static variables. Note that in order to
|
|
|
|
// get correct libdl_info we need to call constructors
|
|
|
|
// before get_libdl_info().
|
|
|
|
solist = get_libdl_info();
|
|
|
|
sonext = get_libdl_info();
|
|
|
|
|
2013-02-07 19:14:39 +01:00
|
|
|
// We have successfully fixed our own relocations. It's safe to run
|
|
|
|
// the main part of the linker now.
|
2014-05-14 19:02:03 +02:00
|
|
|
args.abort_message_ptr = &g_abort_message;
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Addr) start_address = __linker_init_post_relocation(args, linker_addr);
|
2012-10-17 00:54:46 +02:00
|
|
|
|
2015-01-23 19:43:58 +01:00
|
|
|
INFO("[ jumping to _start ]");
|
|
|
|
|
2013-02-07 19:14:39 +01:00
|
|
|
// Return the address that the calling assembly stub should jump to.
|
|
|
|
return start_address;
|
2011-11-12 00:53:17 +01:00
|
|
|
}
|