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 "platform/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 #define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE | DF_1_PIE | DF_1_ORIGIN) 55 56 // Class used construct version dependency graph. 57 class VersionTracker { 58 public: 59 VersionTracker() = default; 60 bool init(const soinfo* si_from); 61 62 const version_info* get_version_info(ElfW(Versym) source_symver) const; 63 private: 64 bool init_verneed(const soinfo* si_from); 65 bool init_verdef(const soinfo* si_from); 66 void add_version_info(size_t source_index, ElfW(Word) elf_hash, 67 const char* ver_name, const soinfo* target_si); 68 69 std::vector<version_info> version_infos; 70 71 DISALLOW_COPY_AND_ASSIGN(VersionTracker); 72 }; 73 74 soinfo* get_libdl_info(const soinfo& linker_si); 75 76 soinfo* find_containing_library(const void* p); 77 78 int open_executable(const char* path, off64_t* file_offset, std::string* realpath); 79 80 void do_android_get_LD_LIBRARY_PATH(char*, size_t); 81 void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path); 82 void* do_dlopen(const char* name, 83 int flags, 84 const android_dlextinfo* extinfo, 85 const void* caller_addr); 86 87 int do_dlclose(void* handle); 88 89 int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data); 90 91 #if defined(__arm__) 92 _Unwind_Ptr do_dl_unwind_find_exidx(_Unwind_Ptr pc, int* pcount); 93 #endif 94 95 bool do_dlsym(void* handle, const char* sym_name, 96 const char* sym_ver, 97 const void* caller_addr, 98 void** symbol); 99 100 int do_dladdr(const void* addr, Dl_info* info); 101 102 void set_application_target_sdk_version(int target); 103 int get_application_target_sdk_version(); 104 105 bool get_transparent_hugepages_supported(); 106 107 void set_16kb_appcompat_mode(bool enable_app_compat); 108 bool get_16kb_appcompat_mode(); 109 110 enum { 111 /* A regular namespace is the namespace with a custom search path that does 112 * not impose any restrictions on the location of native libraries. 113 */ 114 ANDROID_NAMESPACE_TYPE_REGULAR = 0, 115 116 /* An isolated namespace requires all the libraries to be on the search path 117 * or under permitted_when_isolated_path. The search path is the union of 118 * ld_library_path and default_library_path. 119 */ 120 ANDROID_NAMESPACE_TYPE_ISOLATED = 1, 121 122 /* The shared namespace clones the list of libraries of the caller namespace upon creation 123 * which means that they are shared between namespaces - the caller namespace and the new one 124 * will use the same copy of a library if it was loaded prior to android_create_namespace call. 125 * 126 * Note that libraries loaded after the namespace is created will not be shared. 127 * 128 * Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor 129 * permitted_path from the caller's namespace. 130 */ 131 ANDROID_NAMESPACE_TYPE_SHARED = 2, 132 133 /* This flag instructs linker to enable exempt-list workaround for the namespace. 134 * See http://b/26394120 for details. 135 */ 136 ANDROID_NAMESPACE_TYPE_EXEMPT_LIST_ENABLED = 0x08000000, 137 138 /* This flag instructs linker to use this namespace as the anonymous 139 * namespace. There can be only one anonymous namespace in a process. If there 140 * already an anonymous namespace in the process, using this flag when 141 * creating a new namespace causes an error 142 */ 143 ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS = 0x10000000, 144 145 ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED | 146 ANDROID_NAMESPACE_TYPE_ISOLATED, 147 }; 148 149 bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path); 150 android_namespace_t* create_namespace(const void* caller_addr, 151 const char* name, 152 const char* ld_library_path, 153 const char* default_library_path, 154 uint64_t type, 155 const char* permitted_when_isolated_path, 156 android_namespace_t* parent_namespace); 157 158 bool link_namespaces(android_namespace_t* namespace_from, 159 android_namespace_t* namespace_to, 160 const char* shared_lib_sonames); 161 162 bool link_namespaces_all_libs(android_namespace_t* namespace_from, 163 android_namespace_t* namespace_to); 164 165 android_namespace_t* get_exported_namespace(const char* name); 166 167 void increment_dso_handle_reference_counter(void* dso_handle); 168 void decrement_dso_handle_reference_counter(void* dso_handle); 169 170 void purge_unused_memory(); 171 172 struct address_space_params { 173 void* start_addr = nullptr; 174 size_t reserved_size = 0; 175 bool must_use_address = false; 176 }; 177 178 int get_application_target_sdk_version(); 179 ElfW(Versym) find_verdef_version_index(const soinfo* si, const version_info* vi); 180 bool validate_verdef_section(const soinfo* si); 181 bool relocate_relr(const ElfW(Relr) * begin, const ElfW(Relr) * end, ElfW(Addr) load_bias, 182 bool has_memtag_globals); 183 184 struct platform_properties { 185 #if defined(__aarch64__) 186 bool bti_supported = false; 187 #endif 188 }; 189