Extract soinfo and globals to separate files.
Move soinfo and globals out of linker.cpp to
separate files.
Breaking up huge linker.cpp into smaller peaces
in order to make it easier to extract part of the
code that belongs to libdl.so and remove parts of
the code that do not belong to linker
(refactoring part 2 of many)
Change-Id: I868417f4b8d2b84d0e8265e354bc7977161497e2
diff --git a/linker/linker.h b/linker/linker.h
index 61ef259..aa6d00b 100644
--- a/linker/linker.h
+++ b/linker/linker.h
@@ -42,25 +42,11 @@
#include "linked_list.h"
#include "linker_common_types.h"
#include "linker_logger.h"
+#include "linker_soinfo.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); \
- /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
- LD_LOG(kLogErrors, "%s\n", linker_get_error_buffer()); \
- } 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)
-
#if defined(__LP64__)
#define ELFW(what) ELF64_ ## what
#else
@@ -81,61 +67,8 @@
#define ELF64_R_TYPE(info) (((info) >> 56) & 0xff)
#endif
-#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
-
#define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE)
-#define SOINFO_VERSION 3
-
-#if defined(__work_around_b_24465209__)
-#define SOINFO_NAME_LEN 128
-#endif
-
-typedef void (*linker_dtor_function_t)();
-typedef void (*linker_ctor_function_t)(int, char**, char**);
-
-// Android uses RELA for aarch64 and x86_64. mips64 still uses REL.
-#if defined(__aarch64__) || defined(__x86_64__)
-#define USE_RELA 1
-#endif
-
-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 {
- constexpr 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:
@@ -154,262 +87,6 @@
DISALLOW_COPY_AND_ASSIGN(VersionTracker);
};
-struct soinfo {
- public:
-#if defined(__work_around_b_24465209__)
- private:
- char old_name_[SOINFO_NAME_LEN];
-#endif
- public:
- const ElfW(Phdr)* phdr;
- size_t phnum;
-#if defined(__work_around_b_24465209__)
- ElfW(Addr) unused0; // DO NOT USE, maintained for compatibility.
-#endif
- ElfW(Addr) base;
- size_t size;
-
-#if defined(__work_around_b_24465209__)
- uint32_t unused1; // DO NOT USE, maintained for compatibility.
-#endif
-
- ElfW(Dyn)* dynamic;
-
-#if defined(__work_around_b_24465209__)
- uint32_t unused2; // DO NOT USE, maintained for compatibility
- uint32_t unused3; // DO NOT USE, maintained for compatibility
-#endif
-
- soinfo* next;
- private:
- uint32_t flags_;
-
- 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_;
-#endif
-
-#if defined(USE_RELA)
- ElfW(Rela)* plt_rela_;
- size_t plt_rela_count_;
-
- ElfW(Rela)* rela_;
- size_t rela_count_;
-#else
- ElfW(Rel)* plt_rel_;
- size_t plt_rel_count_;
-
- ElfW(Rel)* rel_;
- size_t rel_count_;
-#endif
-
- linker_ctor_function_t* preinit_array_;
- size_t preinit_array_count_;
-
- linker_ctor_function_t* init_array_;
- size_t init_array_count_;
- linker_dtor_function_t* fini_array_;
- size_t fini_array_count_;
-
- linker_ctor_function_t init_func_;
- linker_dtor_function_t fini_func_;
-
-#if defined(__arm__)
- public:
- // ARM EABI section used for stack unwinding.
- uint32_t* ARM_exidx;
- size_t ARM_exidx_count;
- private:
-#elif defined(__mips__)
- uint32_t mips_symtabno_;
- uint32_t mips_local_gotno_;
- uint32_t mips_gotsym_;
- bool mips_relocate_got(const VersionTracker& version_tracker,
- const soinfo_list_t& global_group,
- const soinfo_list_t& local_group);
-#if !defined(__LP64__)
- bool mips_check_and_adjust_fp_modes();
-#endif
-#endif
- size_t ref_count_;
- public:
- link_map link_map_head;
-
- bool constructors_called;
-
- // When you read a virtual address from the ELF file, add this
- // value to get the corresponding address in the process' address space.
- ElfW(Addr) load_bias;
-
-#if !defined(__LP64__)
- bool has_text_relocations;
-#endif
- bool has_DT_SYMBOLIC;
-
- public:
- soinfo(android_namespace_t* ns, const char* name, const struct stat* file_stat,
- off64_t file_offset, int rtld_flags);
- ~soinfo();
-
- 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);
- bool protect_relro();
-
- void add_child(soinfo* child);
- void remove_all_links();
-
- 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();
-
- 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 __unused) const {
-#if defined(__work_around_b_24465209__)
- return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
-#else
- return true;
-#endif
- }
-
- bool is_linked() const;
- bool is_linker() const;
- bool is_main_executable() const;
-
- void set_linked();
- void set_linker_flag();
- void set_main_executable();
- void set_nodelete();
-
- void increment_ref_count();
- size_t decrement_ref_count();
-
- soinfo* get_local_group_root() const;
-
- void set_soname(const char* soname);
- 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;
-
- uint32_t get_target_sdk_version() const;
-
- void set_dt_runpath(const char *);
- const std::vector<std::string>& get_dt_runpath() const;
- android_namespace_t* get_primary_namespace();
- void add_secondary_namespace(android_namespace_t* secondary_ns);
-
- void set_mapped_by_caller(bool reserved_map);
- bool is_mapped_by_caller() const;
-
- uintptr_t get_handle() const;
- void generate_handle();
- void* to_handle();
-
- 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);
-
- bool lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
- const char* sym_name, const version_info** vi);
-
- template<typename ElfRelIteratorT>
- bool relocate(const VersionTracker& version_tracker, 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_;
-
- // version >= 0
- dev_t st_dev_;
- ino_t st_ino_;
-
- // dependency graph
- soinfo_list_t children_;
- soinfo_list_t parents_;
-
- // version >= 1
- 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_;
-
- uint32_t target_sdk_version_;
-
- // version >= 3
- std::vector<std::string> dt_runpath_;
- android_namespace_t* primary_namespace_;
- android_namespace_list_t secondary_namespaces_;
- uintptr_t handle_;
-
- friend soinfo* get_libdl_info();
-};
-
bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
soinfo** si_found_in, const soinfo_list_t& global_group,
const soinfo_list_t& local_group, const ElfW(Sym)** symbol);
@@ -438,9 +115,6 @@
int do_dladdr(const void* addr, Dl_info* info);
-char* linker_get_error_buffer();
-size_t linker_get_error_buffer_size();
-
void set_application_target_sdk_version(uint32_t target);
uint32_t get_application_target_sdk_version();