| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 |  | 
| Elliott Hughes | cbc80ba | 2018-02-13 14:26:29 -0800 | [diff] [blame] | 29 | #pragma once | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 30 |  | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 31 | #include <dlfcn.h> | 
| Elliott Hughes | afab3ff | 2015-07-28 14:58:37 -0700 | [diff] [blame] | 32 | #include <android/dlext.h> | 
| Nick Kralevich | 9ec0f03 | 2012-02-28 10:40:00 -0800 | [diff] [blame] | 33 | #include <elf.h> | 
| Dmitriy Ivanov | 0d15094 | 2014-08-22 12:25:04 -0700 | [diff] [blame] | 34 | #include <inttypes.h> | 
| Pavel Chupin | b7beb69 | 2012-08-17 12:53:29 +0400 | [diff] [blame] | 35 | #include <link.h> | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 36 | #include <sys/stat.h> | 
| Elliott Hughes | afab3ff | 2015-07-28 14:58:37 -0700 | [diff] [blame] | 37 | #include <unistd.h> | 
| Elliott Hughes | 4688279 | 2012-08-03 16:49:39 -0700 | [diff] [blame] | 38 |  | 
| Elliott Hughes | cdb52fc | 2019-12-12 15:26:14 -0800 | [diff] [blame] | 39 | #include "platform/bionic/page.h" | 
| Dmitriy Ivanov | d59e500 | 2014-05-09 09:10:14 -0700 | [diff] [blame] | 40 | #include "linked_list.h" | 
| Dimitry Ivanov | b943f30 | 2016-08-03 16:00:10 -0700 | [diff] [blame] | 41 | #include "linker_common_types.h" | 
| Dimitry Ivanov | b996d60 | 2016-07-11 18:11:39 -0700 | [diff] [blame] | 42 | #include "linker_logger.h" | 
| Dimitry Ivanov | 48ec288 | 2016-08-04 11:50:36 -0700 | [diff] [blame] | 43 | #include "linker_soinfo.h" | 
| Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 44 |  | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 45 | #include <string> | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 46 | #include <vector> | 
| Dmitriy Ivanov | aae859c | 2015-03-31 11:14:03 -0700 | [diff] [blame] | 47 |  | 
| Elliott Hughes | 0266ae5 | 2014-02-10 17:46:57 -0800 | [diff] [blame] | 48 | #if defined(__LP64__) | 
|  | 49 | #define ELFW(what) ELF64_ ## what | 
|  | 50 | #else | 
|  | 51 | #define ELFW(what) ELF32_ ## what | 
|  | 52 | #endif | 
| Elliott Hughes | 650be4e | 2013-03-05 18:47:58 -0800 | [diff] [blame] | 53 |  | 
| Peter Collingbourne | 7aa3abc | 2022-03-21 19:02:26 -0700 | [diff] [blame] | 54 | #define SUPPORTED_DT_FLAGS_1 (DF_1_NOW | DF_1_GLOBAL | DF_1_NODELETE | DF_1_PIE | DF_1_ORIGIN) | 
| Dmitriy Ivanov | d225a5e | 2014-08-28 14:12:12 -0700 | [diff] [blame] | 55 |  | 
| Dmitriy Ivanov | 2a81536 | 2015-04-09 13:42:33 -0700 | [diff] [blame] | 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 |  | 
| Ryan Prichard | 146620b | 2020-03-26 19:55:05 -0700 | [diff] [blame] | 74 | static constexpr const char* kBionicChangesUrl = | 
| Elliott Hughes | 9c06d16 | 2023-10-04 23:36:14 +0000 | [diff] [blame] | 75 | "https://android.googlesource.com/platform/bionic/+/main/" | 
| Ryan Prichard | 146620b | 2020-03-26 19:55:05 -0700 | [diff] [blame] | 76 | "android-changes-for-ndk-developers.md"; | 
|  | 77 |  | 
| Ryan Prichard | cf9ed12 | 2019-06-04 20:56:56 -0700 | [diff] [blame] | 78 | soinfo* get_libdl_info(const soinfo& linker_si); | 
| The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 79 |  | 
| Evgenii Stepanov | 0a3637d | 2016-07-06 13:20:59 -0700 | [diff] [blame] | 80 | soinfo* find_containing_library(const void* p); | 
|  | 81 |  | 
| Ryan Prichard | 8f639a4 | 2018-10-01 23:10:05 -0700 | [diff] [blame] | 82 | int open_executable(const char* path, off64_t* file_offset, std::string* realpath); | 
|  | 83 |  | 
| Elliott Hughes | a4aafd1 | 2014-01-13 16:37:47 -0800 | [diff] [blame] | 84 | void do_android_get_LD_LIBRARY_PATH(char*, size_t); | 
| Elliott Hughes | cade4c3 | 2012-12-20 14:42:14 -0800 | [diff] [blame] | 85 | void do_android_update_LD_LIBRARY_PATH(const char* ld_library_path); | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 86 | void* do_dlopen(const char* name, | 
|  | 87 | int flags, | 
|  | 88 | const android_dlextinfo* extinfo, | 
|  | 89 | const void* caller_addr); | 
|  | 90 |  | 
| Dimitry Ivanov | d88e1f3 | 2016-03-24 15:30:30 -0700 | [diff] [blame] | 91 | int do_dlclose(void* handle); | 
| David 'Digit' Turner | 1608416 | 2012-06-12 16:25:37 +0200 | [diff] [blame] | 92 |  | 
| Dmitriy Ivanov | 7271caf | 2015-06-29 14:48:25 -0700 | [diff] [blame] | 93 | int do_dl_iterate_phdr(int (*cb)(dl_phdr_info* info, size_t size, void* data), void* data); | 
|  | 94 |  | 
| Dimitry Ivanov | d9e427c | 2016-11-22 16:55:25 -0800 | [diff] [blame] | 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); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 103 |  | 
| Dimitry Ivanov | 4a2c5aa | 2015-12-10 16:08:14 -0800 | [diff] [blame] | 104 | int do_dladdr(const void* addr, Dl_info* info); | 
| Elliott Hughes | d23736e | 2012-11-01 15:16:56 -0700 | [diff] [blame] | 105 |  | 
| Elliott Hughes | ff1428a | 2018-11-12 16:01:37 -0800 | [diff] [blame] | 106 | void set_application_target_sdk_version(int target); | 
|  | 107 | int get_application_target_sdk_version(); | 
| Dmitriy Ivanov | 79fd668 | 2015-05-21 17:43:49 -0700 | [diff] [blame] | 108 |  | 
| Collin Fijalkovich | 47d27aa | 2021-03-24 10:17:39 -0700 | [diff] [blame] | 109 | bool get_transparent_hugepages_supported(); | 
|  | 110 |  | 
| Dimitry Ivanov | 41fd295 | 2016-05-09 17:37:39 -0700 | [diff] [blame] | 111 | enum { | 
|  | 112 | /* A regular namespace is the namespace with a custom search path that does | 
|  | 113 | * not impose any restrictions on the location of native libraries. | 
|  | 114 | */ | 
|  | 115 | ANDROID_NAMESPACE_TYPE_REGULAR = 0, | 
|  | 116 |  | 
|  | 117 | /* An isolated namespace requires all the libraries to be on the search path | 
|  | 118 | * or under permitted_when_isolated_path. The search path is the union of | 
|  | 119 | * ld_library_path and default_library_path. | 
|  | 120 | */ | 
|  | 121 | ANDROID_NAMESPACE_TYPE_ISOLATED = 1, | 
|  | 122 |  | 
|  | 123 | /* The shared namespace clones the list of libraries of the caller namespace upon creation | 
|  | 124 | * which means that they are shared between namespaces - the caller namespace and the new one | 
|  | 125 | * will use the same copy of a library if it was loaded prior to android_create_namespace call. | 
|  | 126 | * | 
|  | 127 | * Note that libraries loaded after the namespace is created will not be shared. | 
|  | 128 | * | 
|  | 129 | * Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor | 
|  | 130 | * permitted_path from the caller's namespace. | 
|  | 131 | */ | 
|  | 132 | ANDROID_NAMESPACE_TYPE_SHARED = 2, | 
| Jiyong Park | 37b91af | 2017-05-05 22:07:05 +0900 | [diff] [blame] | 133 |  | 
| Ryan Prichard | aff9a34 | 2020-08-03 15:29:12 -0700 | [diff] [blame] | 134 | /* This flag instructs linker to enable exempt-list workaround for the namespace. | 
| Jiyong Park | 37b91af | 2017-05-05 22:07:05 +0900 | [diff] [blame] | 135 | * See http://b/26394120 for details. | 
|  | 136 | */ | 
| Ryan Prichard | aff9a34 | 2020-08-03 15:29:12 -0700 | [diff] [blame] | 137 | ANDROID_NAMESPACE_TYPE_EXEMPT_LIST_ENABLED = 0x08000000, | 
| Jiyong Park | 37b91af | 2017-05-05 22:07:05 +0900 | [diff] [blame] | 138 |  | 
| Jiyong Park | 25bedfd | 2019-05-16 21:00:39 +0900 | [diff] [blame] | 139 | /* This flag instructs linker to use this namespace as the anonymous | 
|  | 140 | * namespace. There can be only one anonymous namespace in a process. If there | 
|  | 141 | * already an anonymous namespace in the process, using this flag when | 
|  | 142 | * creating a new namespace causes an error | 
|  | 143 | */ | 
|  | 144 | ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS = 0x10000000, | 
|  | 145 |  | 
| Dimitry Ivanov | 41fd295 | 2016-05-09 17:37:39 -0700 | [diff] [blame] | 146 | ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED | | 
|  | 147 | ANDROID_NAMESPACE_TYPE_ISOLATED, | 
|  | 148 | }; | 
|  | 149 |  | 
| Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 150 | bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path); | 
| Dimitry Ivanov | fc2da53 | 2016-05-12 15:20:21 -0700 | [diff] [blame] | 151 | android_namespace_t* create_namespace(const void* caller_addr, | 
|  | 152 | const char* name, | 
|  | 153 | const char* ld_library_path, | 
|  | 154 | const char* default_library_path, | 
|  | 155 | uint64_t type, | 
|  | 156 | const char* permitted_when_isolated_path, | 
|  | 157 | android_namespace_t* parent_namespace); | 
| Dmitriy Ivanov | 42d5fcb | 2015-10-29 17:01:24 -0700 | [diff] [blame] | 158 |  | 
| Dimitry Ivanov | 7a34b9d | 2017-02-03 14:07:34 -0800 | [diff] [blame] | 159 | bool link_namespaces(android_namespace_t* namespace_from, | 
|  | 160 | android_namespace_t* namespace_to, | 
|  | 161 | const char* shared_lib_sonames); | 
|  | 162 |  | 
| Logan Chien | 9ee4591 | 2018-01-18 12:05:09 +0800 | [diff] [blame] | 163 | bool link_namespaces_all_libs(android_namespace_t* namespace_from, | 
|  | 164 | android_namespace_t* namespace_to); | 
|  | 165 |  | 
| Jiyong Park | 01de74e | 2017-04-03 23:10:37 +0900 | [diff] [blame] | 166 | android_namespace_t* get_exported_namespace(const char* name); | 
|  | 167 |  | 
| dimitry | 06016f2 | 2018-01-05 11:39:28 +0100 | [diff] [blame] | 168 | void increment_dso_handle_reference_counter(void* dso_handle); | 
|  | 169 | void decrement_dso_handle_reference_counter(void* dso_handle); | 
| Vic Yang | bb7e123 | 2019-01-29 20:23:16 -0800 | [diff] [blame] | 170 |  | 
|  | 171 | void purge_unused_memory(); | 
| Torne (Richard Coles) | efbe9a5 | 2018-10-17 15:59:38 -0400 | [diff] [blame] | 172 |  | 
|  | 173 | struct address_space_params { | 
|  | 174 | void* start_addr = nullptr; | 
|  | 175 | size_t reserved_size = 0; | 
|  | 176 | bool must_use_address = false; | 
|  | 177 | }; | 
| Ryan Prichard | 0e12cce | 2020-01-02 14:59:11 -0800 | [diff] [blame] | 178 |  | 
| Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 179 | int get_application_target_sdk_version(); | 
| Ryan Prichard | 0e12cce | 2020-01-02 14:59:11 -0800 | [diff] [blame] | 180 | ElfW(Versym) find_verdef_version_index(const soinfo* si, const version_info* vi); | 
| Ryan Prichard | 339ecef | 2020-01-02 16:36:06 -0800 | [diff] [blame] | 181 | bool validate_verdef_section(const soinfo* si); | 
| Ryan Prichard | 2a901e6 | 2024-04-04 03:05:35 -0700 | [diff] [blame] | 182 | bool relocate_relr(const ElfW(Relr)* begin, const ElfW(Relr)* end, ElfW(Addr) load_bias); | 
| Tamas Petz | 8d55d18 | 2020-02-24 14:15:25 +0100 | [diff] [blame] | 183 |  | 
|  | 184 | struct platform_properties { | 
|  | 185 | #if defined(__aarch64__) | 
|  | 186 | bool bti_supported = false; | 
|  | 187 | #endif | 
|  | 188 | }; |