Home | History | Annotate | Download | only in linker
      1 /*
      2  * Copyright (C) 2008 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 #ifndef _LINKER_H_
     30 #define _LINKER_H_
     31 
     32 #include <dlfcn.h>
     33 #include <android/dlext.h>
     34 #include <elf.h>
     35 #include <inttypes.h>
     36 #include <link.h>
     37 #include <sys/stat.h>
     38 #include <unistd.h>
     39 
     40 #include "private/bionic_page.h"
     41 #include "private/libc_logging.h"
     42 #include "linked_list.h"
     43 
     44 #include <string>
     45 #include <vector>
     46 
     47 #define DL_ERR(fmt, x...) \
     48     do { \
     49       __libc_format_buffer(linker_get_error_buffer(), linker_get_error_buffer_size(), fmt, ##x); \
     50       /* If LD_DEBUG is set high enough, log every dlerror(3) message. */ \
     51       DEBUG("%s\n", linker_get_error_buffer()); \
     52     } while (false)
     53 
     54 #define DL_WARN(fmt, x...) \
     55     do { \
     56       __libc_format_log(ANDROID_LOG_WARN, "linker", fmt, ##x); \
     57       __libc_format_fd(2, "WARNING: linker: "); \
     58       __libc_format_fd(2, fmt, ##x); \
     59       __libc_format_fd(2, "\n"); \
     60     } while (false)
     61 
     62 #define DL_ERR_AND_LOG(fmt, x...) \
     63   do { \
     64     DL_ERR(fmt, x); \
     65     PRINT(fmt, x); \
     66   } while (false)
     67 
     68 #if defined(__LP64__)
     69 #define ELFW(what) ELF64_ ## what
     70 #else
     71 #define ELFW(what) ELF32_ ## what
     72 #endif
     73 
     74 // mips64 interprets Elf64_Rel structures' r_info field differently.
     75 // bionic (like other C libraries) has macros that assume regular ELF files,
     76 // but the dynamic linker needs to be able to load mips64 ELF files.
     77 #if defined(__mips__) && defined(__LP64__)
     78 #undef ELF64_R_SYM
     79 #undef ELF64_R_TYPE
     80 #undef ELF64_R_INFO
     81 #define ELF64_R_SYM(info)   (((info) >> 0) & 0xffffffff)
     82 #define ELF64_R_SSYM(info)  (((info) >> 32) & 0xff)
     83 #define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff)
     84 #define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff)
     85 #define ELF64_R_TYPE(info)  (((info) >> 56) & 0xff)
     86 #endif
     87 
     88 #define FLAG_LINKED           0x00000001
     89 #define FLAG_EXE              0x00000004 // The main executable
     90 #define FLAG_LINKER           0x00000010 // The linker itself
     91 #define FLAG_GNU_HASH         0x00000040 // uses gnu hash
     92 #define FLAG_MAPPED_BY_CALLER 0x00000080 // the map is reserved by the caller
     93                                          // and should not be unmapped
     94 #define FLAG_NEW_SOINFO       0x40000000 // new soinfo format
     95 
     96 #define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE)
     97 
     98 #define SOINFO_VERSION 3
     99 
    100 #if defined(__work_around_b_24465209__)
    101 #define SOINFO_NAME_LEN 128
    102 #endif
    103 
    104 typedef void (*linker_function_t)();
    105 
    106 // Android uses RELA for aarch64 and x86_64. mips64 still uses REL.
    107 #if defined(__aarch64__) || defined(__x86_64__)
    108 #define USE_RELA 1
    109 #endif
    110 
    111 struct soinfo;
    112 
    113 class SoinfoListAllocator {
    114  public:
    115   static LinkedListEntry<soinfo>* alloc();
    116   static void free(LinkedListEntry<soinfo>* entry);
    117 
    118  private:
    119   // unconstructable
    120   DISALLOW_IMPLICIT_CONSTRUCTORS(SoinfoListAllocator);
    121 };
    122 
    123 class NamespaceListAllocator {
    124  public:
    125   static LinkedListEntry<android_namespace_t>* alloc();
    126   static void free(LinkedListEntry<android_namespace_t>* entry);
    127 
    128  private:
    129   // unconstructable
    130   DISALLOW_IMPLICIT_CONSTRUCTORS(NamespaceListAllocator);
    131 };
    132 
    133 class SymbolName {
    134  public:
    135   explicit SymbolName(const char* name)
    136       : name_(name), has_elf_hash_(false), has_gnu_hash_(false),
    137         elf_hash_(0), gnu_hash_(0) { }
    138 
    139   const char* get_name() {
    140     return name_;
    141   }
    142 
    143   uint32_t elf_hash();
    144   uint32_t gnu_hash();
    145 
    146  private:
    147   const char* name_;
    148   bool has_elf_hash_;
    149   bool has_gnu_hash_;
    150   uint32_t elf_hash_;
    151   uint32_t gnu_hash_;
    152 
    153   DISALLOW_IMPLICIT_CONSTRUCTORS(SymbolName);
    154 };
    155 
    156 struct version_info {
    157   constexpr version_info() : elf_hash(0), name(nullptr), target_si(nullptr) {}
    158 
    159   uint32_t elf_hash;
    160   const char* name;
    161   const soinfo* target_si;
    162 };
    163 
    164 // Class used construct version dependency graph.
    165 class VersionTracker {
    166  public:
    167   VersionTracker() = default;
    168   bool init(const soinfo* si_from);
    169 
    170   const version_info* get_version_info(ElfW(Versym) source_symver) const;
    171  private:
    172   bool init_verneed(const soinfo* si_from);
    173   bool init_verdef(const soinfo* si_from);
    174   void add_version_info(size_t source_index, ElfW(Word) elf_hash,
    175       const char* ver_name, const soinfo* target_si);
    176 
    177   std::vector<version_info> version_infos;
    178 
    179   DISALLOW_COPY_AND_ASSIGN(VersionTracker);
    180 };
    181 
    182 struct soinfo {
    183  public:
    184   typedef LinkedList<soinfo, SoinfoListAllocator> soinfo_list_t;
    185   typedef LinkedList<android_namespace_t, NamespaceListAllocator> android_namespace_list_t;
    186 #if defined(__work_around_b_24465209__)
    187  private:
    188   char old_name_[SOINFO_NAME_LEN];
    189 #endif
    190  public:
    191   const ElfW(Phdr)* phdr;
    192   size_t phnum;
    193   ElfW(Addr) entry;
    194   ElfW(Addr) base;
    195   size_t size;
    196 
    197 #if defined(__work_around_b_24465209__)
    198   uint32_t unused1;  // DO NOT USE, maintained for compatibility.
    199 #endif
    200 
    201   ElfW(Dyn)* dynamic;
    202 
    203 #if defined(__work_around_b_24465209__)
    204   uint32_t unused2; // DO NOT USE, maintained for compatibility
    205   uint32_t unused3; // DO NOT USE, maintained for compatibility
    206 #endif
    207 
    208   soinfo* next;
    209  private:
    210   uint32_t flags_;
    211 
    212   const char* strtab_;
    213   ElfW(Sym)* symtab_;
    214 
    215   size_t nbucket_;
    216   size_t nchain_;
    217   uint32_t* bucket_;
    218   uint32_t* chain_;
    219 
    220 #if defined(__mips__) || !defined(__LP64__)
    221   // This is only used by mips and mips64, but needs to be here for
    222   // all 32-bit architectures to preserve binary compatibility.
    223   ElfW(Addr)** plt_got_;
    224 #endif
    225 
    226 #if defined(USE_RELA)
    227   ElfW(Rela)* plt_rela_;
    228   size_t plt_rela_count_;
    229 
    230   ElfW(Rela)* rela_;
    231   size_t rela_count_;
    232 #else
    233   ElfW(Rel)* plt_rel_;
    234   size_t plt_rel_count_;
    235 
    236   ElfW(Rel)* rel_;
    237   size_t rel_count_;
    238 #endif
    239 
    240   linker_function_t* preinit_array_;
    241   size_t preinit_array_count_;
    242 
    243   linker_function_t* init_array_;
    244   size_t init_array_count_;
    245   linker_function_t* fini_array_;
    246   size_t fini_array_count_;
    247 
    248   linker_function_t init_func_;
    249   linker_function_t fini_func_;
    250 
    251 #if defined(__arm__)
    252  public:
    253   // ARM EABI section used for stack unwinding.
    254   uint32_t* ARM_exidx;
    255   size_t ARM_exidx_count;
    256  private:
    257 #elif defined(__mips__)
    258   uint32_t mips_symtabno_;
    259   uint32_t mips_local_gotno_;
    260   uint32_t mips_gotsym_;
    261   bool mips_relocate_got(const VersionTracker& version_tracker,
    262                          const soinfo_list_t& global_group,
    263                          const soinfo_list_t& local_group);
    264 #if !defined(__LP64__)
    265   bool mips_check_and_adjust_fp_modes();
    266 #endif
    267 #endif
    268   size_t ref_count_;
    269  public:
    270   link_map link_map_head;
    271 
    272   bool constructors_called;
    273 
    274   // When you read a virtual address from the ELF file, add this
    275   // value to get the corresponding address in the process' address space.
    276   ElfW(Addr) load_bias;
    277 
    278 #if !defined(__LP64__)
    279   bool has_text_relocations;
    280 #endif
    281   bool has_DT_SYMBOLIC;
    282 
    283  public:
    284   soinfo(android_namespace_t* ns, const char* name, const struct stat* file_stat,
    285          off64_t file_offset, int rtld_flags);
    286   ~soinfo();
    287 
    288   void call_constructors();
    289   void call_destructors();
    290   void call_pre_init_constructors();
    291   bool prelink_image();
    292   bool link_image(const soinfo_list_t& global_group, const soinfo_list_t& local_group,
    293                   const android_dlextinfo* extinfo);
    294   bool protect_relro();
    295 
    296   void add_child(soinfo* child);
    297   void remove_all_links();
    298 
    299   ino_t get_st_ino() const;
    300   dev_t get_st_dev() const;
    301   off64_t get_file_offset() const;
    302 
    303   uint32_t get_rtld_flags() const;
    304   uint32_t get_dt_flags_1() const;
    305   void set_dt_flags_1(uint32_t dt_flags_1);
    306 
    307   soinfo_list_t& get_children();
    308   const soinfo_list_t& get_children() const;
    309 
    310   soinfo_list_t& get_parents();
    311 
    312   bool find_symbol_by_name(SymbolName& symbol_name,
    313                            const version_info* vi,
    314                            const ElfW(Sym)** symbol) const;
    315 
    316   ElfW(Sym)* find_symbol_by_address(const void* addr);
    317   ElfW(Addr) resolve_symbol_address(const ElfW(Sym)* s) const;
    318 
    319   const char* get_string(ElfW(Word) index) const;
    320   bool can_unload() const;
    321   bool is_gnu_hash() const;
    322 
    323   bool inline has_min_version(uint32_t min_version __unused) const {
    324 #if defined(__work_around_b_24465209__)
    325     return (flags_ & FLAG_NEW_SOINFO) != 0 && version_ >= min_version;
    326 #else
    327     return true;
    328 #endif
    329   }
    330 
    331   bool is_linked() const;
    332   bool is_linker() const;
    333   bool is_main_executable() const;
    334 
    335   void set_linked();
    336   void set_linker_flag();
    337   void set_main_executable();
    338   void set_nodelete();
    339 
    340   void increment_ref_count();
    341   size_t decrement_ref_count();
    342 
    343   soinfo* get_local_group_root() const;
    344 
    345   void set_soname(const char* soname);
    346   const char* get_soname() const;
    347   const char* get_realpath() const;
    348   const ElfW(Versym)* get_versym(size_t n) const;
    349   ElfW(Addr) get_verneed_ptr() const;
    350   size_t get_verneed_cnt() const;
    351   ElfW(Addr) get_verdef_ptr() const;
    352   size_t get_verdef_cnt() const;
    353 
    354   bool find_verdef_version_index(const version_info* vi, ElfW(Versym)* versym) const;
    355 
    356   uint32_t get_target_sdk_version() const;
    357 
    358   void set_dt_runpath(const char *);
    359   const std::vector<std::string>& get_dt_runpath() const;
    360   android_namespace_t* get_primary_namespace();
    361   void add_secondary_namespace(android_namespace_t* secondary_ns);
    362 
    363   void set_mapped_by_caller(bool reserved_map);
    364   bool is_mapped_by_caller() const;
    365 
    366   uintptr_t get_handle() const;
    367   void generate_handle();
    368   void* to_handle();
    369 
    370  private:
    371   bool elf_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
    372   ElfW(Sym)* elf_addr_lookup(const void* addr);
    373   bool gnu_lookup(SymbolName& symbol_name, const version_info* vi, uint32_t* symbol_index) const;
    374   ElfW(Sym)* gnu_addr_lookup(const void* addr);
    375 
    376   bool lookup_version_info(const VersionTracker& version_tracker, ElfW(Word) sym,
    377                            const char* sym_name, const version_info** vi);
    378 
    379   void call_array(const char* array_name, linker_function_t* functions, size_t count, bool reverse);
    380   void call_function(const char* function_name, linker_function_t function);
    381   template<typename ElfRelIteratorT>
    382   bool relocate(const VersionTracker& version_tracker, ElfRelIteratorT&& rel_iterator,
    383                 const soinfo_list_t& global_group, const soinfo_list_t& local_group);
    384 
    385  private:
    386   // This part of the structure is only available
    387   // when FLAG_NEW_SOINFO is set in this->flags.
    388   uint32_t version_;
    389 
    390   // version >= 0
    391   dev_t st_dev_;
    392   ino_t st_ino_;
    393 
    394   // dependency graph
    395   soinfo_list_t children_;
    396   soinfo_list_t parents_;
    397 
    398   // version >= 1
    399   off64_t file_offset_;
    400   uint32_t rtld_flags_;
    401   uint32_t dt_flags_1_;
    402   size_t strtab_size_;
    403 
    404   // version >= 2
    405 
    406   size_t gnu_nbucket_;
    407   uint32_t* gnu_bucket_;
    408   uint32_t* gnu_chain_;
    409   uint32_t gnu_maskwords_;
    410   uint32_t gnu_shift2_;
    411   ElfW(Addr)* gnu_bloom_filter_;
    412 
    413   soinfo* local_group_root_;
    414 
    415   uint8_t* android_relocs_;
    416   size_t android_relocs_size_;
    417 
    418   const char* soname_;
    419   std::string realpath_;
    420 
    421   const ElfW(Versym)* versym_;
    422 
    423   ElfW(Addr) verdef_ptr_;
    424   size_t verdef_cnt_;
    425 
    426   ElfW(Addr) verneed_ptr_;
    427   size_t verneed_cnt_;
    428 
    429   uint32_t target_sdk_version_;
    430 
    431   // version >= 3
    432   std::vector<std::string> dt_runpath_;
    433   android_namespace_t* primary_namespace_;
    434   android_namespace_list_t secondary_namespaces_;
    435   uintptr_t handle_;
    436 
    437   friend soinfo* get_libdl_info();
    438 };
    439 
    440 bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
    441                       soinfo** si_found_in, const soinfo::soinfo_list_t& global_group,
    442                       const soinfo::soinfo_list_t& local_group, const ElfW(Sym)** symbol);
    443 
    444 enum RelocationKind {
    445   kRelocAbsolute = 0,
    446   kRelocRelative,
    447   kRelocCopy,
    448   kRelocSymbol,
    449   kRelocMax
    450 };
    451 
    452 void count_relocation(RelocationKind kind);
    453 
    454 soinfo* get_libdl_info();
    455 
    456 void do_android_get_LD_LIBRARY_PATH(char*, size_t);
    457 void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
    458 void* do_dlopen(const char* name, int flags, const android_dlextinfo* extinfo, void* caller_addr);
    459 int do_dlclose(void* handle);
    460 
    461 int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data);
    462 
    463 bool do_dlsym(void* handle, const char* sym_name, const char* sym_ver,
    464               void* caller_addr, void** symbol);
    465 
    466 int do_dladdr(const void* addr, Dl_info* info);
    467 
    468 void debuggerd_init();
    469 extern "C" abort_msg_t* g_abort_message;
    470 
    471 char* linker_get_error_buffer();
    472 size_t linker_get_error_buffer_size();
    473 
    474 void set_application_target_sdk_version(uint32_t target);
    475 uint32_t get_application_target_sdk_version();
    476 
    477 enum {
    478   /* A regular namespace is the namespace with a custom search path that does
    479    * not impose any restrictions on the location of native libraries.
    480    */
    481   ANDROID_NAMESPACE_TYPE_REGULAR = 0,
    482 
    483   /* An isolated namespace requires all the libraries to be on the search path
    484    * or under permitted_when_isolated_path. The search path is the union of
    485    * ld_library_path and default_library_path.
    486    */
    487   ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
    488 
    489   /* The shared namespace clones the list of libraries of the caller namespace upon creation
    490    * which means that they are shared between namespaces - the caller namespace and the new one
    491    * will use the same copy of a library if it was loaded prior to android_create_namespace call.
    492    *
    493    * Note that libraries loaded after the namespace is created will not be shared.
    494    *
    495    * Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor
    496    * permitted_path from the caller's namespace.
    497    */
    498   ANDROID_NAMESPACE_TYPE_SHARED = 2,
    499   ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED |
    500                                            ANDROID_NAMESPACE_TYPE_ISOLATED,
    501 };
    502 
    503 bool init_namespaces(const char* public_ns_sonames, const char* anon_ns_library_path);
    504 android_namespace_t* create_namespace(const void* caller_addr,
    505                                       const char* name,
    506                                       const char* ld_library_path,
    507                                       const char* default_library_path,
    508                                       uint64_t type,
    509                                       const char* permitted_when_isolated_path,
    510                                       android_namespace_t* parent_namespace);
    511 
    512 #endif
    513