• Home
  • Line#
  • Scopes#
  • Navigate#
  • Raw
  • Download
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/+/master/"
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 ___cfi_slowpath(uint64_t CallSiteTypeId, void *Ptr, void *Ret);
107 // void ___cfi_slowpath_diag(uint64_t CallSiteTypeId, void *Ptr, void *DiagData, void *Ret);
108 void ___cfi_fail(uint64_t CallSiteTypeId, void* Ptr, void *DiagData, void *Ret);
109 
110 void set_application_target_sdk_version(int target);
111 int get_application_target_sdk_version();
112 
113 bool get_transparent_hugepages_supported();
114 
115 enum {
116   /* A regular namespace is the namespace with a custom search path that does
117    * not impose any restrictions on the location of native libraries.
118    */
119   ANDROID_NAMESPACE_TYPE_REGULAR = 0,
120 
121   /* An isolated namespace requires all the libraries to be on the search path
122    * or under permitted_when_isolated_path. The search path is the union of
123    * ld_library_path and default_library_path.
124    */
125   ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
126 
127   /* The shared namespace clones the list of libraries of the caller namespace upon creation
128    * which means that they are shared between namespaces - the caller namespace and the new one
129    * will use the same copy of a library if it was loaded prior to android_create_namespace call.
130    *
131    * Note that libraries loaded after the namespace is created will not be shared.
132    *
133    * Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor
134    * permitted_path from the caller's namespace.
135    */
136   ANDROID_NAMESPACE_TYPE_SHARED = 2,
137 
138   /* This flag instructs linker to enable exempt-list workaround for the namespace.
139    * See http://b/26394120 for details.
140    */
141   ANDROID_NAMESPACE_TYPE_EXEMPT_LIST_ENABLED = 0x08000000,
142 
143   /* This flag instructs linker to use this namespace as the anonymous
144    * namespace. There can be only one anonymous namespace in a process. If there
145    * already an anonymous namespace in the process, using this flag when
146    * creating a new namespace causes an error
147    */
148   ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS = 0x10000000,
149 
150   ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED |
151                                            ANDROID_NAMESPACE_TYPE_ISOLATED,
152 };
153 
154 bool init_anonymous_namespace(const char* shared_lib_sonames, const char* library_search_path);
155 android_namespace_t* create_namespace(const void* caller_addr,
156                                       const char* name,
157                                       const char* ld_library_path,
158                                       const char* default_library_path,
159                                       uint64_t type,
160                                       const char* permitted_when_isolated_path,
161                                       android_namespace_t* parent_namespace);
162 
163 bool link_namespaces(android_namespace_t* namespace_from,
164                      android_namespace_t* namespace_to,
165                      const char* shared_lib_sonames);
166 
167 bool link_namespaces_all_libs(android_namespace_t* namespace_from,
168                               android_namespace_t* namespace_to);
169 
170 android_namespace_t* get_exported_namespace(const char* name);
171 
172 void increment_dso_handle_reference_counter(void* dso_handle);
173 void decrement_dso_handle_reference_counter(void* dso_handle);
174 
175 void purge_unused_memory();
176 
177 struct address_space_params {
178   void* start_addr = nullptr;
179   size_t reserved_size = 0;
180   bool must_use_address = false;
181 };
182 
183 int get_application_target_sdk_version();
184 ElfW(Versym) find_verdef_version_index(const soinfo* si, const version_info* vi);
185 bool validate_verdef_section(const soinfo* si);
186 
187 struct platform_properties {
188 #if defined(__aarch64__)
189   bool bti_supported = false;
190 #endif
191 };
192