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