Home | History | Annotate | Download | only in linker
      1 /*
      2  * Copyright (C) 2016 The Android Open Source Project
      3  * All rights reserved.
      4  *
      5  * Redistribution and use in source and binary forms, with or without
      6  * modification, are permitted provided that the following conditions
      7  * are met:
      8  *  * Redistributions of source code must retain the above copyright
      9  *    notice, this list of conditions and the following disclaimer.
     10  *  * Redistributions in binary form must reproduce the above copyright
     11  *    notice, this list of conditions and the following disclaimer in
     12  *    the documentation and/or other materials provided with the
     13  *    distribution.
     14  *
     15  * THIS SOFTWARE IS PROVIDED BY THE COPYRIGHT HOLDERS AND CONTRIBUTORS
     16  * "AS IS" AND ANY EXPRESS OR IMPLIED WARRANTIES, INCLUDING, BUT NOT
     17  * LIMITED TO, THE IMPLIED WARRANTIES OF MERCHANTABILITY AND FITNESS
     18  * FOR A PARTICULAR PURPOSE ARE DISCLAIMED. IN NO EVENT SHALL THE
     19  * COPYRIGHT OWNER OR CONTRIBUTORS BE LIABLE FOR ANY DIRECT, INDIRECT,
     20  * INCIDENTAL, SPECIAL, EXEMPLARY, OR CONSEQUENTIAL DAMAGES (INCLUDING,
     21  * BUT NOT LIMITED TO, PROCUREMENT OF SUBSTITUTE GOODS OR SERVICES; LOSS
     22  * OF USE, DATA, OR PROFITS; OR BUSINESS INTERRUPTION) HOWEVER CAUSED
     23  * AND ON ANY THEORY OF LIABILITY, WHETHER IN CONTRACT, STRICT LIABILITY,
     24  * OR TORT (INCLUDING NEGLIGENCE OR OTHERWISE) ARISING IN ANY WAY OUT
     25  * OF THE USE OF THIS SOFTWARE, EVEN IF ADVISED OF THE POSSIBILITY OF
     26  * SUCH DAMAGE.
     27  */
     28 
     29 #pragma once
     30 
     31 #include <link.h>
     32 
     33 #include <string>
     34 
     35 #include "linker_namespaces.h"
     36 
     37 #define FLAG_LINKED           0x00000001
     38 #define FLAG_EXE              0x00000004 // The main executable
     39 #define FLAG_LINKER           0x00000010 // The linker itself
     40 #define FLAG_GNU_HASH         0x00000040 // uses gnu hash
     41 #define FLAG_MAPPED_BY_CALLER 0x00000080 // the map is reserved by the caller
     42                                          // and should not be unmapped
     43 #define FLAG_IMAGE_LINKED     0x00000100 // Is image linked - this is a guard on link_image.
     44                                          // The difference between this flag and
     45                                          // FLAG_LINKED is that FLAG_LINKED
     46                                          // means is set when load_group is
     47                                          // successfully loaded whereas this
     48                                          // flag is set to avoid linking image
     49                                          // when link_image called for the
     50                                          // second time. This situation happens
     51                                          // when load group is crossing
     52                                          // namespace boundary twice and second
     53                                          // local group depends on the same libraries.
     54 #define FLAG_TLS_NODELETE     0x00000200 // This flag set when there is at least one
     55                                          // outstanding thread_local dtor
     56                                          // registered with this soinfo. In such
     57                                          // a case the actual unload is
     58                                          // postponed until the last thread_local
     59                                          // destructor associated with this
     60                                          // soinfo is executed and this flag is
     61                                          // unset.
     62 #define FLAG_NEW_SOINFO       0x40000000 // new soinfo format
     63 
     64 #define SOINFO_VERSION 4
     65 
     66 typedef void (*linker_dtor_function_t)();
     67 typedef void (*linker_ctor_function_t)(int, char**, char**);
     68 
     69 class SymbolName {
     70  public:
     71   explicit SymbolName(const char* name)
     72       : name_(name), has_elf_hash_(false), has_gnu_hash_(false),
     73         elf_hash_(0), gnu_hash_(0) { }
     74 
     75   const char* get_name() {
     76     return name_;
     77   }
     78 
     79   uint32_t elf_hash();
     80   uint32_t gnu_hash();
     81 
     82  private:
     83   const char* name_;
     84   bool has_elf_hash_;
     85   bool has_gnu_hash_;
     86   uint32_t elf_hash_;
     87   uint32_t gnu_hash_;
     88 
     89   DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolName);
     90 };
     91 
     92 struct version_info {
     93   constexpr version_info() : elf_hash(0), name(nullptr), target_si(nullptr) {}
     94 
     95   uint32_t elf_hash;
     96   const char* name;
     97   const soinfo* target_si;
     98 };
     99 
    100 // TODO(dimitry): remove reference from soinfo member functions to this class.
    101 class VersionTracker;
    102 
    103 #if defined(__work_around_b_24465209__)
    104 #define SOINFO_NAME_LEN 128
    105 #endif
    106 
    107 struct soinfo {
    108 #if defined(__work_around_b_24465209__)
    109  private:
    110   char old_name_[SOINFO_NAME_LEN];
    111 #endif
    112  public:
    113   const ElfW(Phdr)* phdr;
    114   size_t phnum;
    115 #if defined(__work_around_b_24465209__)
    116   ElfW(Addr) unused0; // DO NOT USE, maintained for compatibility.
    117 #endif
    118   ElfW(Addr) base;
    119   size_t size;
    120 
    121 #if defined(__work_around_b_24465209__)
    122   uint32_t unused1;  // DO NOT USE, maintained for compatibility.
    123 #endif
    124 
    125   ElfW(Dyn)* dynamic;
    126 
    127 #if defined(__work_around_b_24465209__)
    128   uint32_t unused2; // DO NOT USE, maintained for compatibility
    129   uint32_t unused3; // DO NOT USE, maintained for compatibility
    130 #endif
    131 
    132   soinfo* next;
    133  private:
    134   uint32_t flags_;
    135 
    136   const char* strtab_;
    137   ElfW(Sym)* symtab_;
    138 
    139   size_t nbucket_;
    140   size_t nchain_;
    141   uint32_t* bucket_;
    142   uint32_t* chain_;
    143 
    144 #if defined(__mips__) || !defined(__LP64__)
    145   // This is only used by mips and mips64, but needs to be here for
    146   // all 32-bit architectures to preserve binary compatibility.
    147   ElfW(Addr)** plt_got_;
    148 #endif
    149 
    150 #if defined(USE_RELA)
    151   ElfW(Rela)* plt_rela_;
    152   size_t plt_rela_count_;
    153 
    154   ElfW(Rela)* rela_;
    155   size_t rela_count_;
    156 #else
    157   ElfW(Rel)* plt_rel_;
    158   size_t plt_rel_count_;
    159 
    160   ElfW(Rel)* rel_;
    161   size_t rel_count_;
    162 #endif
    163 
    164   linker_ctor_function_t* preinit_array_;
    165   size_t preinit_array_count_;
    166 
    167   linker_ctor_function_t* init_array_;
    168   size_t init_array_count_;
    169   linker_dtor_function_t* fini_array_;
    170   size_t fini_array_count_;
    171 
    172   linker_ctor_function_t init_func_;
    173   linker_dtor_function_t fini_func_;
    174 
    175 #if defined(__arm__)
    176  public:
    177   // ARM EABI section used for stack unwinding.
    178   uint32_t* ARM_exidx;
    179   size_t ARM_exidx_count;
    180  private:
    181 #elif defined(__mips__)
    182   uint32_t mips_symtabno_;
    183   uint32_t mips_local_gotno_;
    184   uint32_t mips_gotsym_;
    185   bool mips_relocate_got(const VersionTracker& version_tracker,
    186                          const soinfo_list_t& global_group,
    187                          const soinfo_list_t& local_group);
    188 #if !defined(__LP64__)
    189   bool mips_check_and_adjust_fp_modes();
    190 #endif
    191 #endif
    192   size_t ref_count_;
    193  public:
    194   link_map link_map_head;
    195 
    196   bool constructors_called;
    197 
    198   // When you read a virtual address from the ELF file, add this
    199   // value to get the corresponding address in the process' address space.
    200   ElfW(Addr) load_bias;
    201 
    202 #if !defined(__LP64__)
    203   bool has_text_relocations;
    204 #endif
    205   bool has_DT_SYMBOLIC;
    206 
    207  public:
    208   soinfo(android_namespace_t* ns, const char* name, const struct stat* file_stat,
    209          off64_t file_offset, int rtld_flags);
    210   ~soinfo();
    211 
    212   void call_constructors();
    213   void call_destructors();
    214   void call_pre_init_constructors();
    215   bool prelink_image();
    216   bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
    217                   const android_dlextinfo* extinfo);
    218   bool protect_relro();
    219 
    220   void add_child(soinfo* child);
    221   void remove_all_links();
    222 
    223   ino_t get_st_ino() const;
    224   dev_t get_st_dev() const;
    225   off64_t get_file_offset() const;
    226 
    227   uint32_t get_rtld_flags() const;
    228   uint32_t get_dt_flags_1() const;
    229   void set_dt_flags_1(uint32_t dt_flags_1);
    230 
    231   soinfo_list_t& get_children();
    232   const soinfo_list_t& get_children() const;
    233 
    234   soinfo_list_t& get_parents();
    235 
    236   bool find_symbol_by_name(SymbolName& symbol_name,
    237                            const version_info* vi,
    238                            const ElfW(Sym)** symbol) const;
    239 
    240   ElfW(Sym)* find_symbol_by_address(const void* addr);
    241   ElfW(Addr) resolve_symbol_address(const ElfW(Sym)* s) const;
    242 
    243   const char* get_string(ElfW(Word) index) const;
    244   bool can_unload() const;
    245   bool is_gnu_hash() const;
    246 
    247   bool inline has_min_version(uint32_t min_version __unused) const {
    248 #if defined(__work_around_b_24465209__)
    249     return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
    250 #else
    251     return true;
    252 #endif
    253   }
    254 
    255   bool is_linked() const;
    256   bool is_linker() const;
    257   bool is_main_executable() const;
    258 
    259   void set_linked();
    260   void set_linker_flag();
    261   void set_main_executable();
    262   void set_nodelete();
    263   void set_tls_nodelete();
    264   void unset_tls_nodelete();
    265 
    266   size_t increment_ref_count();
    267   size_t decrement_ref_count();
    268   size_t get_ref_count() const;
    269 
    270   soinfo* get_local_group_root() const;
    271 
    272   void set_soname(const char* soname);
    273   const char* get_soname() const;
    274   const char* get_realpath() const;
    275   const ElfW(Versym)* get_versym(size_t n) const;
    276   ElfW(Addr) get_verneed_ptr() const;
    277   size_t get_verneed_cnt() const;
    278   ElfW(Addr) get_verdef_ptr() const;
    279   size_t get_verdef_cnt() const;
    280 
    281   uint32_t get_target_sdk_version() const;
    282 
    283   void set_dt_runpath(const char *);
    284   const std::vector<std::string>& get_dt_runpath() const;
    285   android_namespace_t* get_primary_namespace();
    286   void add_secondary_namespace(android_namespace_t* secondary_ns);
    287   android_namespace_list_t& get_secondary_namespaces();
    288 
    289   void set_mapped_by_caller(bool reserved_map);
    290   bool is_mapped_by_caller() const;
    291 
    292   uintptr_t get_handle() const;
    293   void generate_handle();
    294   void* to_handle();
    295 
    296  private:
    297   bool is_image_linked() const;
    298   void set_image_linked();
    299 
    300   bool elf_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
    301   ElfW(Sym)* elf_addr_lookup(const void* addr);
    302   bool gnu_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
    303   ElfW(Sym)* gnu_addr_lookup(const void* addr);
    304 
    305   bool lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
    306                            const char* sym_name, const version_info** vi);
    307 
    308   template<typename ElfRelIteratorT>
    309   bool relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
    310                 const soinfo_list_t& global_group, const soinfo_list_t& local_group);
    311   bool relocate_relr();
    312   void apply_relr_reloc(ElfW(Addr) offset);
    313 
    314  private:
    315   // This part of the structure is only available
    316   // when FLAG_NEW_SOINFO is set in this->flags.
    317   uint32_t version_;
    318 
    319   // version >= 0
    320   dev_t st_dev_;
    321   ino_t st_ino_;
    322 
    323   // dependency graph
    324   soinfo_list_t children_;
    325   soinfo_list_t parents_;
    326 
    327   // version >= 1
    328   off64_t file_offset_;
    329   uint32_t rtld_flags_;
    330   uint32_t dt_flags_1_;
    331   size_t strtab_size_;
    332 
    333   // version >= 2
    334 
    335   size_t gnu_nbucket_;
    336   uint32_t* gnu_bucket_;
    337   uint32_t* gnu_chain_;
    338   uint32_t gnu_maskwords_;
    339   uint32_t gnu_shift2_;
    340   ElfW(Addr)* gnu_bloom_filter_;
    341 
    342   soinfo* local_group_root_;
    343 
    344   uint8_t* android_relocs_;
    345   size_t android_relocs_size_;
    346 
    347   const char* soname_;
    348   std::string realpath_;
    349 
    350   const ElfW(Versym)* versym_;
    351 
    352   ElfW(Addr) verdef_ptr_;
    353   size_t verdef_cnt_;
    354 
    355   ElfW(Addr) verneed_ptr_;
    356   size_t verneed_cnt_;
    357 
    358   uint32_t target_sdk_version_;
    359 
    360   // version >= 3
    361   std::vector<std::string> dt_runpath_;
    362   android_namespace_t* primary_namespace_;
    363   android_namespace_list_t secondary_namespaces_;
    364   uintptr_t handle_;
    365 
    366   friend soinfo* get_libdl_info(const char* linker_path,
    367                                 const soinfo& linker_si,
    368                                 const link_map& linker_map);
    369 
    370   // version >= 4
    371   ElfW(Relr)* relr_;
    372   size_t relr_count_;
    373 };
    374 
    375 // This function is used by dlvsym() to calculate hash of sym_ver
    376 uint32_t calculate_elf_hash(const char* name);
    377 
    378 const char* fix_dt_needed(const char* dt_needed, const char* sopath);
    379 
    380 template<typename F>
    381 void for_each_dt_needed(const soinfo* si, F action) {
    382   for (const ElfW(Dyn)* d = si->dynamic; d->d_tag != DT_NULL; ++d) {
    383     if (d->d_tag == DT_NEEDED) {
    384       action(fix_dt_needed(si->get_string(d->d_un.d_val), si->get_realpath()));
    385     }
    386   }
    387 }
    388