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 "linked_list.h"
     42 #include "linker_common_types.h"
     43 #include "linker_logger.h"
     44 #include "linker_soinfo.h"
     45 
     46 #include <string>
     47 #include <vector>
     48 
     49 #if defined(__LP64__)
     50 #define ELFW(what) ELF64_ ## what
     51 #else
     52 #define ELFW(what) ELF32_ ## what
     53 #endif
     54 
     55 // mips64 interprets Elf64_Rel structures' r_info field differently.
     56 // bionic (like other C libraries) has macros that assume regular ELF files,
     57 // but the dynamic linker needs to be able to load mips64 ELF files.
     58 #if defined(__mips__) && defined(__LP64__)
     59 #undef ELF64_R_SYM
     60 #undef ELF64_R_TYPE
     61 #undef ELF64_R_INFO
     62 #define ELF64_R_SYM(info)   (((info) >> 0) & 0xffffffff)
     63 #define ELF64_R_SSYM(info)  (((info) >> 32) & 0xff)
     64 #define ELF64_R_TYPE3(info) (((info) >> 40) & 0xff)
     65 #define ELF64_R_TYPE2(info) (((info) >> 48) & 0xff)
     66 #define ELF64_R_TYPE(info)  (((info) >> 56) & 0xff)
     67 #endif
     68 
     69 #define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE | DF_1_PIE)
     70 
     71 // Class used construct version dependency graph.
     72 class VersionTracker {
     73  public:
     74   VersionTracker() = default;
     75   bool init(const soinfo* si_from);
     76 
     77   const version_info* get_version_info(ElfW(Versym) source_symver) const;
     78  private:
     79   bool init_verneed(const soinfo* si_from);
     80   bool init_verdef(const soinfo* si_from);
     81   void add_version_info(size_t source_index, ElfW(Word) elf_hash,
     82       const char* ver_name, const soinfo* target_si);
     83 
     84   std::vector<version_info> version_infos;
     85 
     86   DISALLOW_COPY_AND_ASSIGN(VersionTracker);
     87 };
     88 
     89 bool soinfo_do_lookup(soinfo* si_from, const char* name, const version_info* vi,
     90                       soinfo** si_found_in, const soinfo_list_t& global_group,
     91                       const soinfo_list_t& local_group, const ElfW(Sym)** symbol);
     92 
     93 enum RelocationKind {
     94   kRelocAbsolute = 0,
     95   kRelocRelative,
     96   kRelocCopy,
     97   kRelocSymbol,
     98   kRelocMax
     99 };
    100 
    101 void count_relocation(RelocationKind kind);
    102 
    103 soinfo* get_libdl_info(const char* linker_path, const link_map& linker_map);
    104 
    105 soinfo* find_containing_library(const void* p);
    106 
    107 void do_android_get_LD_LIBRARY_PATH(char*, size_t);
    108 void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path);
    109 void* do_dlopen(const char* name,
    110                 int flags,
    111                 const android_dlextinfo* extinfo,
    112                 const void* caller_addr);
    113 
    114 int do_dlclose(void* handle);
    115 
    116 int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data);
    117 
    118 #if defined(__arm__)
    119 _Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount);
    120 #endif
    121 
    122 bool do_dlsym(void* handle, const char* sym_name,
    123               const char* sym_ver,
    124               const void* caller_addr,
    125               void** symbol);
    126 
    127 int do_dladdr(const void* addr, Dl_info* info);
    128 
    129 // void ___cfi_slowpath(uint64_t CallSiteTypeId, void *Ptr, void *Ret);
    130 // void ___cfi_slowpath_diag(uint64_t CallSiteTypeId, void *Ptr, void *DiagData, void *Ret);
    131 void ___cfi_fail(uint64_t CallSiteTypeId, void* Ptr, void *DiagData, void *Ret);
    132 
    133 void set_application_target_sdk_version(uint32_t target);
    134 uint32_t get_application_target_sdk_version();
    135 
    136 enum {
    137   /* A regular namespace is the namespace with a custom search path that does
    138    * not impose any restrictions on the location of native libraries.
    139    */
    140   ANDROID_NAMESPACE_TYPE_REGULAR = 0,
    141 
    142   /* An isolated namespace requires all the libraries to be on the search path
    143    * or under permitted_when_isolated_path. The search path is the union of
    144    * ld_library_path and default_library_path.
    145    */
    146   ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
    147 
    148   /* The shared namespace clones the list of libraries of the caller namespace upon creation
    149    * which means that they are shared between namespaces - the caller namespace and the new one
    150    * will use the same copy of a library if it was loaded prior to android_create_namespace call.
    151    *
    152    * Note that libraries loaded after the namespace is created will not be shared.
    153    *
    154    * Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor
    155    * permitted_path from the caller's namespace.
    156    */
    157   ANDROID_NAMESPACE_TYPE_SHARED = 2,
    158 
    159   /* This flag instructs linker to enable grey-list workaround for the namespace.
    160    * See http://b/26394120 for details.
    161    */
    162   ANDROID_NAMESPACE_TYPE_GREYLIST_ENABLED = 0x08000000,
    163 
    164   ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED |
    165                                            ANDROID_NAMESPACE_TYPE_ISOLATED,
    166 };
    167 
    168 bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path);
    169 android_namespace_t* create_namespace(const void* caller_addr,
    170                                       const char* name,
    171                                       const char* ld_library_path,
    172                                       const char* default_library_path,
    173                                       uint64_t type,
    174                                       const char* permitted_when_isolated_path,
    175                                       android_namespace_t* parent_namespace);
    176 
    177 bool link_namespaces(android_namespace_t* namespace_from,
    178                      android_namespace_t* namespace_to,
    179                      const char* shared_lib_sonames);
    180 
    181 android_namespace_t* get_exported_namespace(const char* name);
    182 
    183 #endif
    184