summaryrefslogtreecommitdiffstats
path: root/linker/linker.h
diff options
context:
space:
mode:
Diffstat (limited to 'linker/linker.h')
-rw-r--r--linker/linker.h286
1 files changed, 217 insertions, 69 deletions
diff --git a/linker/linker.h b/linker/linker.h
index fa38c7f..dae3972 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -39,6 +39,9 @@
#include "private/libc_logging.h"
#include "linked_list.h"
+#include <string>
+#include <vector>
+
#define DL_ERR(fmt, x...) \
do { \
__libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
@@ -87,11 +90,16 @@
#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_NEW_SOINFO 0x40000000 // new soinfo format
-#define SOINFO_VERSION 0
+#define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE)
+
+#define SOINFO_VERSION 2
+#if defined(__arm__)
#define SOINFO_NAME_LEN 128
+#endif
typedef void (*linker_function_t)();
@@ -103,91 +111,147 @@ typedef void (*linker_function_t)();
struct soinfo;
class SoinfoListAllocator {
-public:
+ public:
static LinkedListEntry<soinfo>* alloc();
static void free(LinkedListEntry<soinfo>* entry);
-private:
+
+ private:
// unconstructable
DISALLOW_IMPLICIT_CONSTRUCTORS(SoinfoListAllocator);
};
+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);
+};
+
+struct version_info {
+ version_info() : elf_hash(0), name(nullptr), target_si(nullptr) {}
+
+ 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);
+};
+
struct soinfo {
public:
typedef LinkedList<soinfo, SoinfoListAllocator> soinfo_list_t;
+#if defined(__arm__)
+ private:
+ char old_name_[SOINFO_NAME_LEN];
+#endif
public:
- char name[SOINFO_NAME_LEN];
const ElfW(Phdr)* phdr;
size_t phnum;
ElfW(Addr) entry;
ElfW(Addr) base;
size_t size;
-#ifndef __LP64__
+#if defined(__arm__)
uint32_t unused1; // DO NOT USE, maintained for compatibility.
#endif
ElfW(Dyn)* dynamic;
-#ifndef __LP64__
+#if defined(__arm__)
uint32_t unused2; // DO NOT USE, maintained for compatibility
uint32_t unused3; // DO NOT USE, maintained for compatibility
#endif
soinfo* next;
- unsigned flags;
-
private:
- const char* strtab;
- public:
- ElfW(Sym)* symtab;
+ uint32_t flags_;
- size_t nbucket;
- size_t nchain;
- unsigned* bucket;
- unsigned* chain;
+ const char* strtab_;
+ ElfW(Sym)* symtab_;
+
+ size_t nbucket_;
+ size_t nchain_;
+ uint32_t* bucket_;
+ uint32_t* chain_;
#if defined(__mips__) || !defined(__LP64__)
// This is only used by mips and mips64, but needs to be here for
// all 32-bit architectures to preserve binary compatibility.
- ElfW(Addr)** plt_got;
+ ElfW(Addr)** plt_got_;
#endif
#if defined(USE_RELA)
- ElfW(Rela)* plt_rela;
- size_t plt_rela_count;
+ ElfW(Rela)* plt_rela_;
+ size_t plt_rela_count_;
- ElfW(Rela)* rela;
- size_t rela_count;
+ ElfW(Rela)* rela_;
+ size_t rela_count_;
#else
- ElfW(Rel)* plt_rel;
- size_t plt_rel_count;
+ ElfW(Rel)* plt_rel_;
+ size_t plt_rel_count_;
- ElfW(Rel)* rel;
- size_t rel_count;
+ ElfW(Rel)* rel_;
+ size_t rel_count_;
#endif
- linker_function_t* preinit_array;
- size_t preinit_array_count;
+ linker_function_t* preinit_array_;
+ size_t preinit_array_count_;
- linker_function_t* init_array;
- size_t init_array_count;
- linker_function_t* fini_array;
- size_t fini_array_count;
+ linker_function_t* init_array_;
+ size_t init_array_count_;
+ linker_function_t* fini_array_;
+ size_t fini_array_count_;
- linker_function_t init_func;
- linker_function_t fini_func;
+ linker_function_t init_func_;
+ linker_function_t fini_func_;
#if defined(__arm__)
+ public:
// ARM EABI section used for stack unwinding.
- unsigned* ARM_exidx;
+ uint32_t* ARM_exidx;
size_t ARM_exidx_count;
+ private:
#elif defined(__mips__)
- unsigned mips_symtabno;
- unsigned mips_local_gotno;
- unsigned mips_gotsym;
-#endif
+ uint32_t mips_symtabno_;
+ uint32_t mips_local_gotno_;
+ uint32_t mips_gotsym_;
+ bool mips_relocate_got(const soinfo_list_t& global_group, const soinfo_list_t& local_group);
- size_t ref_count;
+#endif
+ size_t ref_count_;
+ public:
link_map link_map_head;
bool constructors_called;
@@ -201,73 +265,157 @@ struct soinfo {
#endif
bool has_DT_SYMBOLIC;
- soinfo(const char* name, const struct stat* file_stat, off64_t file_offset);
+ public:
+ soinfo(const char* name, const struct stat* file_stat, off64_t file_offset, int rtld_flags);
- void CallConstructors();
- void CallDestructors();
- void CallPreInitConstructors();
- bool PrelinkImage();
- bool LinkImage(const android_dlextinfo* extinfo);
+ void call_constructors();
+ void call_destructors();
+ void call_pre_init_constructors();
+ bool prelink_image();
+ bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
+ const android_dlextinfo* extinfo);
void add_child(soinfo* child);
void remove_all_links();
- ino_t get_st_ino();
- dev_t get_st_dev();
- off64_t get_file_offset();
+ ino_t get_st_ino() const;
+ dev_t get_st_dev() const;
+ off64_t get_file_offset() const;
+
+ uint32_t get_rtld_flags() const;
+ uint32_t get_dt_flags_1() const;
+ void set_dt_flags_1(uint32_t dt_flags_1);
soinfo_list_t& get_children();
+ const soinfo_list_t& get_children() const;
+
soinfo_list_t& get_parents();
- ElfW(Addr) resolve_symbol_address(ElfW(Sym)* s);
+ bool find_symbol_by_name(SymbolName& symbol_name,
+ const version_info* vi,
+ const ElfW(Sym)** symbol) const;
+
+ ElfW(Sym)* find_symbol_by_address(const void* addr);
+ ElfW(Addr) resolve_symbol_address(const ElfW(Sym)* s) const;
const char* get_string(ElfW(Word) index) const;
+ bool can_unload() const;
+ bool is_gnu_hash() const;
- bool inline has_min_version(uint32_t min_version) const {
- return (flags & FLAG_NEW_SOINFO) != 0 && version >= min_version;
- }
- private:
- void CallArray(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
- void CallFunction(const char* function_name, linker_function_t function);
-#if defined(USE_RELA)
- int Relocate(ElfW(Rela)* rela, unsigned count);
+ bool inline has_min_version(uint32_t min_version __unused) const {
+#if defined(__arm__)
+ return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
#else
- int Relocate(ElfW(Rel)* rel, unsigned count);
+ return true;
#endif
+ }
+
+ bool is_linked() const;
+ bool is_main_executable() const;
+
+ void set_linked();
+ void set_linker_flag();
+ void set_main_executable();
+
+ void increment_ref_count();
+ size_t decrement_ref_count();
+
+ soinfo* get_local_group_root() const;
+
+ const char* get_soname() const;
+ const char* get_realpath() const;
+ 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;
+
+ private:
+ bool elf_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
+ ElfW(Sym)* elf_addr_lookup(const void* addr);
+ bool gnu_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
+ ElfW(Sym)* gnu_addr_lookup(const void* addr);
+
+ void call_array(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
+ void call_function(const char* function_name, linker_function_t function);
+ template<typename ElfRelIteratorT>
+ bool relocate(ElfRelIteratorT&& rel_iterator, const soinfo_list_t& global_group,
+ const soinfo_list_t& local_group);
private:
// This part of the structure is only available
// when FLAG_NEW_SOINFO is set in this->flags.
- uint32_t version;
+ uint32_t version_;
// version >= 0
- dev_t st_dev;
- ino_t st_ino;
+ dev_t st_dev_;
+ ino_t st_ino_;
// dependency graph
- soinfo_list_t children;
- soinfo_list_t parents;
+ soinfo_list_t children_;
+ soinfo_list_t parents_;
// version >= 1
- off64_t file_offset;
- int rtld_flags;
- size_t strtab_size;
+ off64_t file_offset_;
+ uint32_t rtld_flags_;
+ uint32_t dt_flags_1_;
+ size_t strtab_size_;
+
+ // version >= 2
+
+ size_t gnu_nbucket_;
+ uint32_t* gnu_bucket_;
+ uint32_t* gnu_chain_;
+ uint32_t gnu_maskwords_;
+ uint32_t gnu_shift2_;
+ ElfW(Addr)* gnu_bloom_filter_;
+
+ soinfo* local_group_root_;
+
+ uint8_t* android_relocs_;
+ size_t android_relocs_size_;
+
+ const char* soname_;
+ std::string realpath_;
+
+ const ElfW(Versym)* versym_;
+
+ ElfW(Addr) verdef_ptr_;
+ size_t verdef_cnt_;
+
+ ElfW(Addr) verneed_ptr_;
+ size_t verneed_cnt_;
friend soinfo* get_libdl_info();
};
-extern soinfo* get_libdl_info();
+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);
+
+enum RelocationKind {
+ kRelocAbsolute = 0,
+ kRelocRelative,
+ kRelocCopy,
+ kRelocSymbol,
+ kRelocMax
+};
+
+void count_relocation(RelocationKind kind);
+
+soinfo* get_libdl_info();
void do_android_get_LD_LIBRARY_PATH(char*, size_t);
void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
soinfo* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo);
void do_dlclose(soinfo* si);
-ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* start);
+const ElfW(Sym)* dlsym_linear_lookup(const char* name, soinfo** found, soinfo* caller, void* handle);
soinfo* find_containing_library(const void* addr);
-ElfW(Sym)* dladdr_find_symbol(soinfo* si, const void* addr);
-ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name);
+const ElfW(Sym)* dlsym_handle_lookup(soinfo* si, soinfo** found, const char* name);
void debuggerd_init();
extern "C" abort_msg_t* g_abort_message;