2009-03-04 04:28:35 +01:00
|
|
|
/*
|
|
|
|
* Copyright (C) 2008 The Android Open Source Project
|
|
|
|
* 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.
|
|
|
|
*/
|
|
|
|
|
|
|
|
#ifndef _LINKER_H_
|
|
|
|
#define _LINKER_H_
|
|
|
|
|
2015-12-11 01:08:14 +01:00
|
|
|
#include <dlfcn.h>
|
2015-07-28 23:58:37 +02:00
|
|
|
#include <android/dlext.h>
|
2012-02-28 19:40:00 +01:00
|
|
|
#include <elf.h>
|
2014-08-22 21:25:04 +02:00
|
|
|
#include <inttypes.h>
|
2012-08-17 10:53:29 +02:00
|
|
|
#include <link.h>
|
2014-05-09 18:10:14 +02:00
|
|
|
#include <sys/stat.h>
|
2015-07-28 23:58:37 +02:00
|
|
|
#include <unistd.h>
|
2012-08-04 01:49:39 +02:00
|
|
|
|
2015-07-28 23:58:37 +02:00
|
|
|
#include "private/bionic_page.h"
|
2013-03-15 23:30:25 +01:00
|
|
|
#include "private/libc_logging.h"
|
2014-05-09 18:10:14 +02:00
|
|
|
#include "linked_list.h"
|
2016-07-12 03:11:39 +02:00
|
|
|
#include "linker_logger.h"
|
2013-03-06 03:47:58 +01:00
|
|
|
|
2015-03-31 20:14:03 +02:00
|
|
|
#include <string>
|
2015-04-09 22:42:33 +02:00
|
|
|
#include <vector>
|
2015-03-31 20:14:03 +02:00
|
|
|
|
2013-03-06 03:47:58 +01:00
|
|
|
#define DL_ERR(fmt, x...) \
|
|
|
|
do { \
|
|
|
|
__libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
|
|
|
|
/* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
|
2016-07-12 03:11:39 +02:00
|
|
|
LD_LOG(kLogErrors, "%s\n", linker_get_error_buffer()); \
|
2013-06-18 22:15:00 +02:00
|
|
|
} while (false)
|
|
|
|
|
|
|
|
#define DL_WARN(fmt, x...) \
|
|
|
|
do { \
|
|
|
|
__libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \
|
|
|
|
__libc_format_fd(2, "WARNING: linker: "); \
|
|
|
|
__libc_format_fd(2, fmt, ##x); \
|
|
|
|
__libc_format_fd(2, "\n"); \
|
|
|
|
} while (false)
|
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
#if defined(__LP64__)
|
|
|
|
#define ELFW(what) ELF64_ ## what
|
|
|
|
#else
|
|
|
|
#define ELFW(what) ELF32_ ## what
|
|
|
|
#endif
|
2013-03-06 03:47:58 +01:00
|
|
|
|
2014-02-07 05:36:51 +01:00
|
|
|
// mips64 interprets Elf64_Rel structures' r_info field differently.
|
|
|
|
// bionic (like other C libraries) has macros that assume regular ELF files,
|
|
|
|
// but the dynamic linker needs to be able to load mips64 ELF files.
|
|
|
|
#if defined(__mips__) && defined(__LP64__)
|
|
|
|
#undef ELF64_R_SYM
|
|
|
|
#undef ELF64_R_TYPE
|
|
|
|
#undef ELF64_R_INFO
|
|
|
|
#define ELF64_R_SYM(info) (((info) >> 0) & 0xffffffff)
|
|
|
|
#define ELF64_R_SSYM(info) (((info) >> 32) & 0xff)
|
|
|
|
#define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff)
|
|
|
|
#define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff)
|
|
|
|
#define ELF64_R_TYPE(info) (((info) >> 56) & 0xff)
|
|
|
|
#endif
|
|
|
|
|
2016-01-15 20:13:35 +01:00
|
|
|
#define FLAG_LINKED 0x00000001
|
|
|
|
#define FLAG_EXE 0x00000004 // The main executable
|
|
|
|
#define FLAG_LINKER 0x00000010 // The linker itself
|
|
|
|
#define FLAG_GNU_HASH 0x00000040 // uses gnu hash
|
|
|
|
#define FLAG_MAPPED_BY_CALLER 0x00000080 // the map is reserved by the caller
|
|
|
|
// and should not be unmapped
|
|
|
|
#define FLAG_NEW_SOINFO 0x40000000 // new soinfo format
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
#define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE)
|
|
|
|
|
2015-10-30 01:01:24 +01:00
|
|
|
#define SOINFO_VERSION 3
|
2014-08-22 21:25:04 +02:00
|
|
|
|
2015-09-28 19:14:17 +02:00
|
|
|
#if defined(__work_around_b_24465209__)
|
2009-03-04 04:28:35 +01:00
|
|
|
#define SOINFO_NAME_LEN 128
|
2015-03-31 20:14:03 +02:00
|
|
|
#endif
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2016-07-21 00:33:07 +02:00
|
|
|
typedef void (*linker_dtor_function_t)();
|
|
|
|
typedef void (*linker_ctor_function_t)(int, char**, char**);
|
2013-03-12 18:40:45 +01:00
|
|
|
|
2014-02-07 05:36:51 +01:00
|
|
|
// Android uses RELA for aarch64 and x86_64. mips64 still uses REL.
|
|
|
|
#if defined(__aarch64__) || defined(__x86_64__)
|
2013-10-26 02:38:02 +02:00
|
|
|
#define USE_RELA 1
|
|
|
|
#endif
|
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
struct soinfo;
|
|
|
|
|
|
|
|
class SoinfoListAllocator {
|
2014-11-10 04:27:20 +01:00
|
|
|
public:
|
2014-05-09 18:10:14 +02:00
|
|
|
static LinkedListEntry<soinfo>* alloc();
|
|
|
|
static void free(LinkedListEntry<soinfo>* entry);
|
2014-11-10 04:27:20 +01:00
|
|
|
|
|
|
|
private:
|
2014-05-09 18:10:14 +02:00
|
|
|
// unconstructable
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(SoinfoListAllocator);
|
|
|
|
};
|
|
|
|
|
2016-04-11 21:42:58 +02:00
|
|
|
class NamespaceListAllocator {
|
|
|
|
public:
|
|
|
|
static LinkedListEntry<android_namespace_t>* alloc();
|
|
|
|
static void free(LinkedListEntry<android_namespace_t>* entry);
|
|
|
|
|
|
|
|
private:
|
|
|
|
// unconstructable
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceListAllocator);
|
|
|
|
};
|
|
|
|
|
2014-11-10 04:27:20 +01:00
|
|
|
class SymbolName {
|
|
|
|
public:
|
|
|
|
explicit SymbolName(const char* name)
|
|
|
|
: name_(name), has_elf_hash_(false), has_gnu_hash_(false),
|
|
|
|
elf_hash_(0), gnu_hash_(0) { }
|
|
|
|
|
|
|
|
const char* get_name() {
|
|
|
|
return name_;
|
|
|
|
}
|
|
|
|
|
|
|
|
uint32_t elf_hash();
|
|
|
|
uint32_t gnu_hash();
|
|
|
|
|
|
|
|
private:
|
|
|
|
const char* name_;
|
|
|
|
bool has_elf_hash_;
|
|
|
|
bool has_gnu_hash_;
|
|
|
|
uint32_t elf_hash_;
|
|
|
|
uint32_t gnu_hash_;
|
|
|
|
|
|
|
|
DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolName);
|
|
|
|
};
|
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
struct version_info {
|
2015-12-11 01:08:14 +01:00
|
|
|
constexpr version_info() : elf_hash(0), name(nullptr), target_si(nullptr) {}
|
2015-04-09 22:42:33 +02:00
|
|
|
|
|
|
|
uint32_t elf_hash;
|
|
|
|
const char* name;
|
|
|
|
const soinfo* target_si;
|
|
|
|
};
|
|
|
|
|
|
|
|
// Class used construct version dependency graph.
|
|
|
|
class VersionTracker {
|
|
|
|
public:
|
|
|
|
VersionTracker() = default;
|
|
|
|
bool init(const soinfo* si_from);
|
|
|
|
|
|
|
|
const version_info* get_version_info(ElfW(Versym) source_symver) const;
|
|
|
|
private:
|
|
|
|
bool init_verneed(const soinfo* si_from);
|
|
|
|
bool init_verdef(const soinfo* si_from);
|
|
|
|
void add_version_info(size_t source_index, ElfW(Word) elf_hash,
|
|
|
|
const char* ver_name, const soinfo* target_si);
|
|
|
|
|
|
|
|
std::vector<version_info> version_infos;
|
|
|
|
|
|
|
|
DISALLOW_COPY_AND_ASSIGN(VersionTracker);
|
|
|
|
};
|
|
|
|
|
2012-10-30 01:37:13 +01:00
|
|
|
struct soinfo {
|
2014-05-09 18:10:14 +02:00
|
|
|
public:
|
|
|
|
typedef LinkedList<soinfo, SoinfoListAllocator> soinfo_list_t;
|
2016-04-11 21:42:58 +02:00
|
|
|
typedef LinkedList<android_namespace_t, NamespaceListAllocator> android_namespace_list_t;
|
2015-09-28 19:14:17 +02:00
|
|
|
#if defined(__work_around_b_24465209__)
|
2015-03-31 20:14:03 +02:00
|
|
|
private:
|
|
|
|
char old_name_[SOINFO_NAME_LEN];
|
|
|
|
#endif
|
2013-03-12 18:40:45 +01:00
|
|
|
public:
|
2014-02-11 02:46:57 +01:00
|
|
|
const ElfW(Phdr)* phdr;
|
2013-03-12 18:40:45 +01:00
|
|
|
size_t phnum;
|
2016-02-16 22:25:29 +01:00
|
|
|
#if defined(__work_around_b_24465209__)
|
|
|
|
ElfW(Addr) unused0; // DO NOT USE, maintained for compatibility.
|
|
|
|
#endif
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Addr) base;
|
2013-12-03 12:47:34 +01:00
|
|
|
size_t size;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-09-28 19:14:17 +02:00
|
|
|
#if defined(__work_around_b_24465209__)
|
2013-03-12 18:40:45 +01:00
|
|
|
uint32_t unused1; // DO NOT USE, maintained for compatibility.
|
2013-10-01 03:43:46 +02:00
|
|
|
#endif
|
2011-08-29 22:49:22 +02:00
|
|
|
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Dyn)* dynamic;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2015-09-28 19:14:17 +02:00
|
|
|
#if defined(__work_around_b_24465209__)
|
2013-03-12 18:40:45 +01:00
|
|
|
uint32_t unused2; // DO NOT USE, maintained for compatibility
|
|
|
|
uint32_t unused3; // DO NOT USE, maintained for compatibility
|
2013-10-01 03:43:46 +02:00
|
|
|
#endif
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2012-11-01 23:16:56 +01:00
|
|
|
soinfo* next;
|
2014-09-30 04:14:45 +02:00
|
|
|
private:
|
2014-11-29 22:57:41 +01:00
|
|
|
uint32_t flags_;
|
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
const char* strtab_;
|
|
|
|
ElfW(Sym)* symtab_;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
size_t nbucket_;
|
|
|
|
size_t nchain_;
|
|
|
|
uint32_t* bucket_;
|
|
|
|
uint32_t* chain_;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-02-07 05:36:51 +01:00
|
|
|
#if defined(__mips__) || !defined(__LP64__)
|
|
|
|
// This is only used by mips and mips64, but needs to be here for
|
2013-10-26 02:38:02 +02:00
|
|
|
// all 32-bit architectures to preserve binary compatibility.
|
2014-11-13 18:39:20 +01:00
|
|
|
ElfW(Addr)** plt_got_;
|
2013-10-26 02:38:02 +02:00
|
|
|
#endif
|
|
|
|
|
|
|
|
#if defined(USE_RELA)
|
2014-11-13 18:39:20 +01:00
|
|
|
ElfW(Rela)* plt_rela_;
|
|
|
|
size_t plt_rela_count_;
|
2013-10-05 02:01:33 +02:00
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
ElfW(Rela)* rela_;
|
|
|
|
size_t rela_count_;
|
2013-10-05 02:01:33 +02:00
|
|
|
#else
|
2014-11-13 18:39:20 +01:00
|
|
|
ElfW(Rel)* plt_rel_;
|
|
|
|
size_t plt_rel_count_;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
ElfW(Rel)* rel_;
|
|
|
|
size_t rel_count_;
|
2013-10-05 02:01:33 +02:00
|
|
|
#endif
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2016-07-21 00:33:07 +02:00
|
|
|
linker_ctor_function_t* preinit_array_;
|
2014-11-13 18:39:20 +01:00
|
|
|
size_t preinit_array_count_;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2016-07-21 00:33:07 +02:00
|
|
|
linker_ctor_function_t* init_array_;
|
2014-11-13 18:39:20 +01:00
|
|
|
size_t init_array_count_;
|
2016-07-21 00:33:07 +02:00
|
|
|
linker_dtor_function_t* fini_array_;
|
2014-11-13 18:39:20 +01:00
|
|
|
size_t fini_array_count_;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2016-07-21 00:33:07 +02:00
|
|
|
linker_ctor_function_t init_func_;
|
|
|
|
linker_dtor_function_t fini_func_;
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2013-10-26 02:38:02 +02:00
|
|
|
#if defined(__arm__)
|
2014-11-29 22:57:41 +01:00
|
|
|
public:
|
2012-11-01 23:16:56 +01:00
|
|
|
// ARM EABI section used for stack unwinding.
|
2014-05-20 00:06:58 +02:00
|
|
|
uint32_t* ARM_exidx;
|
2013-03-12 18:40:45 +01:00
|
|
|
size_t ARM_exidx_count;
|
2014-11-13 03:20:39 +01:00
|
|
|
private:
|
2014-11-29 22:57:41 +01:00
|
|
|
#elif defined(__mips__)
|
2014-11-13 18:39:20 +01:00
|
|
|
uint32_t mips_symtabno_;
|
|
|
|
uint32_t mips_local_gotno_;
|
|
|
|
uint32_t mips_gotsym_;
|
2015-05-01 05:17:03 +02:00
|
|
|
bool mips_relocate_got(const VersionTracker& version_tracker,
|
|
|
|
const soinfo_list_t& global_group,
|
|
|
|
const soinfo_list_t& local_group);
|
2015-06-02 01:29:14 +02:00
|
|
|
#if !defined(__LP64__)
|
|
|
|
bool mips_check_and_adjust_fp_modes();
|
|
|
|
#endif
|
2009-03-04 04:28:35 +01:00
|
|
|
#endif
|
2014-11-29 22:57:41 +01:00
|
|
|
size_t ref_count_;
|
2014-11-13 18:39:20 +01:00
|
|
|
public:
|
2014-02-10 22:31:13 +01:00
|
|
|
link_map link_map_head;
|
2011-12-21 10:03:54 +01:00
|
|
|
|
2012-11-01 23:16:56 +01:00
|
|
|
bool constructors_called;
|
2012-02-28 19:40:00 +01:00
|
|
|
|
2012-11-01 23:16:56 +01:00
|
|
|
// When you read a virtual address from the ELF file, add this
|
|
|
|
// value to get the corresponding address in the process' address space.
|
2014-02-11 02:46:57 +01:00
|
|
|
ElfW(Addr) load_bias;
|
2012-08-30 12:48:32 +02:00
|
|
|
|
2015-04-01 23:18:48 +02:00
|
|
|
#if !defined(__LP64__)
|
|
|
|
bool has_text_relocations;
|
2013-10-28 22:19:05 +01:00
|
|
|
#endif
|
2014-09-29 21:10:36 +02:00
|
|
|
bool has_DT_SYMBOLIC;
|
2014-08-22 21:25:04 +02:00
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
public:
|
2015-10-30 01:01:24 +01:00
|
|
|
soinfo(android_namespace_t* ns, const char* name, const struct stat* file_stat,
|
|
|
|
off64_t file_offset, int rtld_flags);
|
2016-03-24 23:30:30 +01:00
|
|
|
~soinfo();
|
2014-08-22 21:25:04 +02:00
|
|
|
|
2014-11-13 18:39:20 +01:00
|
|
|
void call_constructors();
|
|
|
|
void call_destructors();
|
|
|
|
void call_pre_init_constructors();
|
|
|
|
bool prelink_image();
|
2015-03-31 03:43:38 +02:00
|
|
|
bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
|
|
|
|
const android_dlextinfo* extinfo);
|
2015-11-12 08:02:14 +01:00
|
|
|
bool protect_relro();
|
2012-11-01 23:16:56 +01:00
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
void add_child(soinfo* child);
|
|
|
|
void remove_all_links();
|
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
ino_t get_st_ino() const;
|
|
|
|
dev_t get_st_dev() const;
|
|
|
|
off64_t get_file_offset() const;
|
2014-07-23 20:22:25 +02:00
|
|
|
|
2014-08-28 23:12:12 +02:00
|
|
|
uint32_t get_rtld_flags() const;
|
|
|
|
uint32_t get_dt_flags_1() const;
|
|
|
|
void set_dt_flags_1(uint32_t dt_flags_1);
|
2014-09-16 02:00:10 +02:00
|
|
|
|
2014-05-09 18:10:14 +02:00
|
|
|
soinfo_list_t& get_children();
|
2015-04-09 22:42:33 +02:00
|
|
|
const soinfo_list_t& get_children() const;
|
|
|
|
|
2014-09-06 01:42:53 +02:00
|
|
|
soinfo_list_t& get_parents();
|
2014-05-09 18:10:14 +02:00
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
bool find_symbol_by_name(SymbolName& symbol_name,
|
|
|
|
const version_info* vi,
|
|
|
|
const ElfW(Sym)** symbol) const;
|
|
|
|
|
2014-11-10 04:27:20 +01:00
|
|
|
ElfW(Sym)* find_symbol_by_address(const void* addr);
|
2015-04-09 22:42:33 +02:00
|
|
|
ElfW(Addr) resolve_symbol_address(const ElfW(Sym)* s) const;
|
2014-09-12 00:16:03 +02:00
|
|
|
|
2014-09-30 04:14:45 +02:00
|
|
|
const char* get_string(ElfW(Word) index) const;
|
2014-05-20 00:06:58 +02:00
|
|
|
bool can_unload() const;
|
2014-11-10 04:27:20 +01:00
|
|
|
bool is_gnu_hash() const;
|
2014-09-30 04:14:45 +02:00
|
|
|
|
2015-03-31 20:14:03 +02:00
|
|
|
bool inline has_min_version(uint32_t min_version __unused) const {
|
2015-09-28 19:14:17 +02:00
|
|
|
#if defined(__work_around_b_24465209__)
|
2014-11-29 22:57:41 +01:00
|
|
|
return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
|
2015-03-31 20:14:03 +02:00
|
|
|
#else
|
|
|
|
return true;
|
|
|
|
#endif
|
2014-08-22 21:25:04 +02:00
|
|
|
}
|
2014-05-20 00:06:58 +02:00
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
bool is_linked() const;
|
2016-03-02 00:55:56 +01:00
|
|
|
bool is_linker() const;
|
2014-11-29 22:57:41 +01:00
|
|
|
bool is_main_executable() const;
|
|
|
|
|
|
|
|
void set_linked();
|
|
|
|
void set_linker_flag();
|
|
|
|
void set_main_executable();
|
2015-10-30 01:01:24 +01:00
|
|
|
void set_nodelete();
|
2014-11-29 22:57:41 +01:00
|
|
|
|
|
|
|
void increment_ref_count();
|
|
|
|
size_t decrement_ref_count();
|
|
|
|
|
|
|
|
soinfo* get_local_group_root() const;
|
|
|
|
|
2015-10-15 21:07:25 +02:00
|
|
|
void set_soname(const char* soname);
|
2015-03-31 20:14:03 +02:00
|
|
|
const char* get_soname() const;
|
|
|
|
const char* get_realpath() const;
|
2015-04-09 22:42:33 +02:00
|
|
|
const ElfW(Versym)* get_versym(size_t n) const;
|
|
|
|
ElfW(Addr) get_verneed_ptr() const;
|
|
|
|
size_t get_verneed_cnt() const;
|
|
|
|
ElfW(Addr) get_verdef_ptr() const;
|
|
|
|
size_t get_verdef_cnt() const;
|
|
|
|
|
|
|
|
bool find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const;
|
2015-03-18 04:06:36 +01:00
|
|
|
|
2015-06-03 02:36:54 +02:00
|
|
|
uint32_t get_target_sdk_version() const;
|
|
|
|
|
2015-10-15 21:07:25 +02:00
|
|
|
void set_dt_runpath(const char *);
|
2015-06-10 22:38:39 +02:00
|
|
|
const std::vector<std::string>& get_dt_runpath() const;
|
2016-04-11 21:42:58 +02:00
|
|
|
android_namespace_t* get_primary_namespace();
|
|
|
|
void add_secondary_namespace(android_namespace_t* secondary_ns);
|
2015-06-10 22:38:39 +02:00
|
|
|
|
2016-01-15 20:13:35 +01:00
|
|
|
void set_mapped_by_caller(bool reserved_map);
|
|
|
|
bool is_mapped_by_caller() const;
|
|
|
|
|
2016-03-24 23:30:30 +01:00
|
|
|
uintptr_t get_handle() const;
|
|
|
|
void generate_handle();
|
|
|
|
void* to_handle();
|
|
|
|
|
2012-11-01 23:16:56 +01:00
|
|
|
private:
|
2015-04-09 22:42:33 +02:00
|
|
|
bool elf_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
|
2014-11-10 04:27:20 +01:00
|
|
|
ElfW(Sym)* elf_addr_lookup(const void* addr);
|
2015-04-09 22:42:33 +02:00
|
|
|
bool gnu_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
|
2014-11-10 04:27:20 +01:00
|
|
|
ElfW(Sym)* gnu_addr_lookup(const void* addr);
|
|
|
|
|
2015-05-01 01:11:48 +02:00
|
|
|
bool lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
|
|
|
|
const char* sym_name, const version_info** vi);
|
|
|
|
|
2015-02-04 01:06:47 +01:00
|
|
|
template<typename ElfRelIteratorT>
|
2015-05-01 04:49:19 +02:00
|
|
|
bool relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
|
|
|
|
const soinfo_list_t& global_group, const soinfo_list_t& local_group);
|
2014-05-09 18:10:14 +02:00
|
|
|
|
|
|
|
private:
|
|
|
|
// This part of the structure is only available
|
|
|
|
// when FLAG_NEW_SOINFO is set in this->flags.
|
2014-11-13 18:39:20 +01:00
|
|
|
uint32_t version_;
|
2014-07-23 20:22:25 +02:00
|
|
|
|
2014-08-22 21:25:04 +02:00
|
|
|
// version >= 0
|
2014-11-13 18:39:20 +01:00
|
|
|
dev_t st_dev_;
|
|
|
|
ino_t st_ino_;
|
2014-05-09 18:10:14 +02:00
|
|
|
|
|
|
|
// dependency graph
|
2014-11-13 18:39:20 +01:00
|
|
|
soinfo_list_t children_;
|
|
|
|
soinfo_list_t parents_;
|
2014-05-09 18:10:14 +02:00
|
|
|
|
2014-08-22 21:25:04 +02:00
|
|
|
// version >= 1
|
2014-11-13 18:39:20 +01:00
|
|
|
off64_t file_offset_;
|
|
|
|
uint32_t rtld_flags_;
|
|
|
|
uint32_t dt_flags_1_;
|
|
|
|
size_t strtab_size_;
|
2014-09-30 04:14:45 +02:00
|
|
|
|
2014-11-10 04:27:20 +01:00
|
|
|
// version >= 2
|
2015-03-09 20:02:02 +01:00
|
|
|
|
|
|
|
size_t gnu_nbucket_;
|
|
|
|
uint32_t* gnu_bucket_;
|
|
|
|
uint32_t* gnu_chain_;
|
2014-11-13 18:39:20 +01:00
|
|
|
uint32_t gnu_maskwords_;
|
|
|
|
uint32_t gnu_shift2_;
|
|
|
|
ElfW(Addr)* gnu_bloom_filter_;
|
2014-11-10 04:27:20 +01:00
|
|
|
|
2014-11-29 22:57:41 +01:00
|
|
|
soinfo* local_group_root_;
|
|
|
|
|
2015-02-05 01:05:30 +01:00
|
|
|
uint8_t* android_relocs_;
|
|
|
|
size_t android_relocs_size_;
|
|
|
|
|
2015-03-18 04:06:36 +01:00
|
|
|
const char* soname_;
|
2015-03-31 20:14:03 +02:00
|
|
|
std::string realpath_;
|
2015-03-18 04:06:36 +01:00
|
|
|
|
2015-04-09 22:42:33 +02:00
|
|
|
const ElfW(Versym)* versym_;
|
|
|
|
|
|
|
|
ElfW(Addr) verdef_ptr_;
|
|
|
|
size_t verdef_cnt_;
|
|
|
|
|
|
|
|
ElfW(Addr) verneed_ptr_;
|
|
|
|
size_t verneed_cnt_;
|
|
|
|
|
2015-06-03 02:36:54 +02:00
|
|
|
uint32_t target_sdk_version_;
|
|
|
|
|
2015-10-30 01:01:24 +01:00
|
|
|
// version >= 3
|
2015-06-10 22:38:39 +02:00
|
|
|
std::vector<std::string> dt_runpath_;
|
2016-04-11 21:42:58 +02:00
|
|
|
android_namespace_t* primary_namespace_;
|
|
|
|
android_namespace_list_t secondary_namespaces_;
|
2016-03-24 23:30:30 +01:00
|
|
|
uintptr_t handle_;
|
2015-06-10 22:38:39 +02:00
|
|
|
|
2014-09-30 04:14:45 +02:00
|
|
|
friend soinfo* get_libdl_info();
|
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);
|
2015-01-14 20:36:38 +01:00
|
|
|
|
|
|
|
enum RelocationKind {
|
|
|
|
kRelocAbsolute = 0,
|
|
|
|
kRelocRelative,
|
|
|
|
kRelocCopy,
|
|
|
|
kRelocSymbol,
|
|
|
|
kRelocMax
|
|
|
|
};
|
|
|
|
|
|
|
|
void count_relocation(RelocationKind kind);
|
|
|
|
|
2014-05-20 00:06:58 +02:00
|
|
|
soinfo* get_libdl_info();
|
2009-03-04 04:28:35 +01:00
|
|
|
|
2014-01-14 01:37:47 +01:00
|
|
|
void do_android_get_LD_LIBRARY_PATH(char*, size_t);
|
2012-12-20 23:42:14 +01:00
|
|
|
void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
|
2016-03-24 23:30:30 +01:00
|
|
|
void* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo, void* caller_addr);
|
|
|
|
int do_dlclose(void* handle);
|
2012-06-12 16:25:37 +02:00
|
|
|
|
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);
|
|
|
|
|
2015-12-11 01:08:14 +01:00
|
|
|
bool do_dlsym(void* handle, const char* sym_name, const char* sym_ver,
|
|
|
|
void* caller_addr, void** symbol);
|
2015-10-30 01:01:24 +01:00
|
|
|
|
2015-12-11 01:08:14 +01:00
|
|
|
int do_dladdr(const void* addr, Dl_info* info);
|
2012-11-01 23:16:56 +01:00
|
|
|
|
2013-03-06 03:47:58 +01:00
|
|
|
char* linker_get_error_buffer();
|
|
|
|
size_t linker_get_error_buffer_size();
|
|
|
|
|
2015-05-22 02:43:49 +02:00
|
|
|
void set_application_target_sdk_version(uint32_t target);
|
|
|
|
uint32_t get_application_target_sdk_version();
|
|
|
|
|
2016-05-10 02:37:39 +02:00
|
|
|
enum {
|
|
|
|
/* A regular namespace is the namespace with a custom search path that does
|
|
|
|
* not impose any restrictions on the location of native libraries.
|
|
|
|
*/
|
|
|
|
ANDROID_NAMESPACE_TYPE_REGULAR = 0,
|
|
|
|
|
|
|
|
/* An isolated namespace requires all the libraries to be on the search path
|
|
|
|
* or under permitted_when_isolated_path. The search path is the union of
|
|
|
|
* ld_library_path and default_library_path.
|
|
|
|
*/
|
|
|
|
ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
|
|
|
|
|
|
|
|
/* The shared namespace clones the list of libraries of the caller namespace upon creation
|
|
|
|
* which means that they are shared between namespaces - the caller namespace and the new one
|
|
|
|
* will use the same copy of a library if it was loaded prior to android_create_namespace call.
|
|
|
|
*
|
|
|
|
* Note that libraries loaded after the namespace is created will not be shared.
|
|
|
|
*
|
|
|
|
* Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor
|
|
|
|
* permitted_path from the caller's namespace.
|
|
|
|
*/
|
|
|
|
ANDROID_NAMESPACE_TYPE_SHARED = 2,
|
|
|
|
ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED |
|
|
|
|
ANDROID_NAMESPACE_TYPE_ISOLATED,
|
|
|
|
};
|
|
|
|
|
2015-11-23 20:26:35 +01:00
|
|
|
bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path);
|
2016-05-13 00:20:21 +02:00
|
|
|
android_namespace_t* create_namespace(const void* caller_addr,
|
|
|
|
const char* name,
|
|
|
|
const char* ld_library_path,
|
|
|
|
const char* default_library_path,
|
|
|
|
uint64_t type,
|
|
|
|
const char* permitted_when_isolated_path,
|
|
|
|
android_namespace_t* parent_namespace);
|
2015-10-30 01:01:24 +01:00
|
|
|
|
Align-up and randomize shared libraries.
This change implements the following property:
Any 2**N aligned memory region on size 2**N contains no more than one DSO.
The value N can be configured, with 16 or 18 looking like a good choice.
Additionally, DSOs are loaded at random page-aligned address inside these large
regions.
This change has dual purpose:
1. Larger values of N allow a lot more compact CFI shadow implementation.
See change I14dfea630de468eb5620e7f55f92b1397ba06217.
For example, CFI shadow for the system_server process has the following size (RSS, KB):
152 for N = 12, 32 for N = 16, 16 for N = 18.
2. Extra randomization is good for security.
This change does not result in extra RAM usage, because everything is still page-aligned.
It does result in a bit more VM fragmentation because of the gaps between shared libraries.
As it turns out, this fragmentation is barely noticeable because the kernel creates new mapping
at the highest possible address, and we do enough small mappings to almost completely fill the
gaps (ex. in the Zygote the gaps are filled with .ttf file mappings and thread stacks).
I've measured VM fragmentation as the sum of all VM gaps (unmapped regions) that are larger
than 1MB according to /proc/$PID/maps. On aosp_angler-userdebug, the numbers are (in GB):
| N = 12 | N = 18
system_server | 521.9 | 521.1
zygote64 | 522.1 | 521.3
zygote32 | 2.55 | 2.55
mediaserver | 4.00 | 4.00
Change-Id: Ia6df840dd409c82837efd1f263be420d9723c84a
2016-07-16 01:31:42 +02:00
|
|
|
constexpr unsigned kLibraryAlignmentBits = 18;
|
|
|
|
constexpr size_t kLibraryAlignment = 1UL << kLibraryAlignmentBits;
|
|
|
|
|
2009-03-04 04:28:35 +01:00
|
|
|
#endif
|