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