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 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,
133 
134   /* This flag instructs linker to enable exempt-list workaround for the namespace.
135    * See http://b/26394120 for details.
136    */
137   ANDROID_NAMESPACE_TYPE_EXEMPT_LIST_ENABLED = 0x08000000,
138 
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 
146   ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED |
147                                            ANDROID_NAMESPACE_TYPE_ISOLATED,
148 };
149 
150 bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path);
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);
158 
159 bool link_namespaces(android_namespace_t* namespace_from,
160                      android_namespace_t* namespace_to,
161                      const char* shared_lib_sonames);
162 
163 bool link_namespaces_all_libs(android_namespace_t* namespace_from,
164                               android_namespace_t* namespace_to);
165 
166 android_namespace_t* get_exported_namespace(const char* name);
167 
168 void increment_dso_handle_reference_counter(void* dso_handle);
169 void decrement_dso_handle_reference_counter(void* dso_handle);
170 
171 void purge_unused_memory();
172 
173 struct address_space_params {
174   void* start_addr = nullptr;
175   size_t reserved_size = 0;
176   bool must_use_address = false;
177 };
178 
179 int get_application_target_sdk_version();
180 ElfW(Versym) find_verdef_version_index(const soinfo* si, const version_info* vi);
181 bool validate_verdef_section(const soinfo* si);
182 bool relocate_relr(const ElfW(Relr)* begin, const ElfW(Relr)* end, ElfW(Addr) load_bias);
183 
184 struct platform_properties {
185 #if defined(__aarch64__)
186   bool bti_supported = false;
187 #endif
188 };
189