1*795d594fSAndroid Build Coastguard Worker /* 2*795d594fSAndroid Build Coastguard Worker * Copyright (C) 2016 The Android Open Source Project 3*795d594fSAndroid Build Coastguard Worker * 4*795d594fSAndroid Build Coastguard Worker * Licensed under the Apache License, Version 2.0 (the "License"); 5*795d594fSAndroid Build Coastguard Worker * you may not use this file except in compliance with the License. 6*795d594fSAndroid Build Coastguard Worker * You may obtain a copy of the License at 7*795d594fSAndroid Build Coastguard Worker * 8*795d594fSAndroid Build Coastguard Worker * http://www.apache.org/licenses/LICENSE-2.0 9*795d594fSAndroid Build Coastguard Worker * 10*795d594fSAndroid Build Coastguard Worker * Unless required by applicable law or agreed to in writing, software 11*795d594fSAndroid Build Coastguard Worker * distributed under the License is distributed on an "AS IS" BASIS, 12*795d594fSAndroid Build Coastguard Worker * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied. 13*795d594fSAndroid Build Coastguard Worker * See the License for the specific language governing permissions and 14*795d594fSAndroid Build Coastguard Worker * limitations under the License. 15*795d594fSAndroid Build Coastguard Worker */ 16*795d594fSAndroid Build Coastguard Worker 17*795d594fSAndroid Build Coastguard Worker #ifndef ART_LIBNATIVELOADER_INCLUDE_NATIVELOADER_DLEXT_NAMESPACES_H_ 18*795d594fSAndroid Build Coastguard Worker #define ART_LIBNATIVELOADER_INCLUDE_NATIVELOADER_DLEXT_NAMESPACES_H_ 19*795d594fSAndroid Build Coastguard Worker 20*795d594fSAndroid Build Coastguard Worker #include <android/dlext.h> 21*795d594fSAndroid Build Coastguard Worker #include <stdbool.h> 22*795d594fSAndroid Build Coastguard Worker 23*795d594fSAndroid Build Coastguard Worker __BEGIN_DECLS 24*795d594fSAndroid Build Coastguard Worker 25*795d594fSAndroid Build Coastguard Worker enum { 26*795d594fSAndroid Build Coastguard Worker /* A regular namespace is the namespace with a custom search path that does 27*795d594fSAndroid Build Coastguard Worker * not impose any restrictions on the location of native libraries. 28*795d594fSAndroid Build Coastguard Worker */ 29*795d594fSAndroid Build Coastguard Worker ANDROID_NAMESPACE_TYPE_REGULAR = 0, 30*795d594fSAndroid Build Coastguard Worker 31*795d594fSAndroid Build Coastguard Worker /* An isolated namespace requires all the libraries to be on the search path 32*795d594fSAndroid Build Coastguard Worker * or under permitted_when_isolated_path. The search path is the union of 33*795d594fSAndroid Build Coastguard Worker * ld_library_path and default_library_path. 34*795d594fSAndroid Build Coastguard Worker */ 35*795d594fSAndroid Build Coastguard Worker ANDROID_NAMESPACE_TYPE_ISOLATED = 1, 36*795d594fSAndroid Build Coastguard Worker 37*795d594fSAndroid Build Coastguard Worker /* The shared namespace clones the list of libraries of the caller namespace upon creation 38*795d594fSAndroid Build Coastguard Worker * which means that they are shared between namespaces - the caller namespace and the new one 39*795d594fSAndroid Build Coastguard Worker * will use the same copy of a library if it was loaded prior to android_create_namespace call. 40*795d594fSAndroid Build Coastguard Worker * 41*795d594fSAndroid Build Coastguard Worker * Note that libraries loaded after the namespace is created will not be shared. 42*795d594fSAndroid Build Coastguard Worker * 43*795d594fSAndroid Build Coastguard Worker * Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor 44*795d594fSAndroid Build Coastguard Worker * permitted_path from the caller's namespace. 45*795d594fSAndroid Build Coastguard Worker */ 46*795d594fSAndroid Build Coastguard Worker ANDROID_NAMESPACE_TYPE_SHARED = 2, 47*795d594fSAndroid Build Coastguard Worker 48*795d594fSAndroid Build Coastguard Worker /* This flag instructs linker to enable exempt-list workaround for the namespace. 49*795d594fSAndroid Build Coastguard Worker * See http://b/26394120 for details. 50*795d594fSAndroid Build Coastguard Worker */ 51*795d594fSAndroid Build Coastguard Worker ANDROID_NAMESPACE_TYPE_EXEMPT_LIST_ENABLED = 0x08000000, 52*795d594fSAndroid Build Coastguard Worker 53*795d594fSAndroid Build Coastguard Worker /* This flag instructs linker to use this namespace as the anonymous 54*795d594fSAndroid Build Coastguard Worker * namespace. The anonymous namespace is used in the case when linker cannot 55*795d594fSAndroid Build Coastguard Worker * identify the caller of dlopen/dlsym. This happens for the code not loaded 56*795d594fSAndroid Build Coastguard Worker * by dynamic linker; for example calls from the mono-compiled code. There can 57*795d594fSAndroid Build Coastguard Worker * be only one anonymous namespace in a process. If there already is an 58*795d594fSAndroid Build Coastguard Worker * anonymous namespace in the process, using this flag when creating a new 59*795d594fSAndroid Build Coastguard Worker * namespace causes an error. 60*795d594fSAndroid Build Coastguard Worker */ 61*795d594fSAndroid Build Coastguard Worker ANDROID_NAMESPACE_TYPE_ALSO_USED_AS_ANONYMOUS = 0x10000000, 62*795d594fSAndroid Build Coastguard Worker 63*795d594fSAndroid Build Coastguard Worker ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = 64*795d594fSAndroid Build Coastguard Worker ANDROID_NAMESPACE_TYPE_SHARED | ANDROID_NAMESPACE_TYPE_ISOLATED, 65*795d594fSAndroid Build Coastguard Worker }; 66*795d594fSAndroid Build Coastguard Worker 67*795d594fSAndroid Build Coastguard Worker /* 68*795d594fSAndroid Build Coastguard Worker * Creates new linker namespace. 69*795d594fSAndroid Build Coastguard Worker * ld_library_path and default_library_path represent the search path 70*795d594fSAndroid Build Coastguard Worker * for the libraries in the namespace. 71*795d594fSAndroid Build Coastguard Worker * 72*795d594fSAndroid Build Coastguard Worker * The libraries in the namespace are searched by folowing order: 73*795d594fSAndroid Build Coastguard Worker * 1. ld_library_path (Think of this as namespace-local LD_LIBRARY_PATH) 74*795d594fSAndroid Build Coastguard Worker * 2. In directories specified by DT_RUNPATH of the "needed by" binary. 75*795d594fSAndroid Build Coastguard Worker * 3. deault_library_path (This of this as namespace-local default library path) 76*795d594fSAndroid Build Coastguard Worker * 77*795d594fSAndroid Build Coastguard Worker * When type is ANDROID_NAMESPACE_TYPE_ISOLATED the resulting namespace requires all of 78*795d594fSAndroid Build Coastguard Worker * the libraries to be on the search path or under the permitted_when_isolated_path; 79*795d594fSAndroid Build Coastguard Worker * the search_path is ld_library_path:default_library_path. Note that the 80*795d594fSAndroid Build Coastguard Worker * permitted_when_isolated_path path is not part of the search_path and 81*795d594fSAndroid Build Coastguard Worker * does not affect the search order. It is a way to allow loading libraries from specific 82*795d594fSAndroid Build Coastguard Worker * locations when using absolute path. 83*795d594fSAndroid Build Coastguard Worker * If a library or any of its dependencies are outside of the permitted_when_isolated_path 84*795d594fSAndroid Build Coastguard Worker * and search_path, and it is not part of the public namespace dlopen will fail. 85*795d594fSAndroid Build Coastguard Worker */ 86*795d594fSAndroid Build Coastguard Worker extern struct android_namespace_t* android_create_namespace( 87*795d594fSAndroid Build Coastguard Worker const char* name, const char* ld_library_path, const char* default_library_path, uint64_t type, 88*795d594fSAndroid Build Coastguard Worker const char* permitted_when_isolated_path, struct android_namespace_t* parent); 89*795d594fSAndroid Build Coastguard Worker 90*795d594fSAndroid Build Coastguard Worker /* 91*795d594fSAndroid Build Coastguard Worker * Creates a link between namespaces. Every link has list of sonames of 92*795d594fSAndroid Build Coastguard Worker * shared libraries. These are the libraries which are accessible from 93*795d594fSAndroid Build Coastguard Worker * namespace 'from' but loaded within namespace 'to' context. 94*795d594fSAndroid Build Coastguard Worker * When to namespace is nullptr this function establishes a link between 95*795d594fSAndroid Build Coastguard Worker * 'from' namespace and the default namespace. 96*795d594fSAndroid Build Coastguard Worker * 97*795d594fSAndroid Build Coastguard Worker * The lookup order of the libraries in namespaces with links is following: 98*795d594fSAndroid Build Coastguard Worker * 1. Look inside current namespace using 'this' namespace search path. 99*795d594fSAndroid Build Coastguard Worker * 2. Look in linked namespaces 100*795d594fSAndroid Build Coastguard Worker * 2.1. Perform soname check - if library soname is not in the list of shared 101*795d594fSAndroid Build Coastguard Worker * libraries sonames skip this link, otherwise 102*795d594fSAndroid Build Coastguard Worker * 2.2. Search library using linked namespace search path. Note that this 103*795d594fSAndroid Build Coastguard Worker * step will not go deeper into linked namespaces for this library but 104*795d594fSAndroid Build Coastguard Worker * will do so for DT_NEEDED libraries. 105*795d594fSAndroid Build Coastguard Worker */ 106*795d594fSAndroid Build Coastguard Worker extern bool android_link_namespaces(struct android_namespace_t* from, 107*795d594fSAndroid Build Coastguard Worker struct android_namespace_t* to, 108*795d594fSAndroid Build Coastguard Worker const char* shared_libs_sonames); 109*795d594fSAndroid Build Coastguard Worker 110*795d594fSAndroid Build Coastguard Worker extern struct android_namespace_t* android_get_exported_namespace(const char* name); 111*795d594fSAndroid Build Coastguard Worker 112*795d594fSAndroid Build Coastguard Worker __END_DECLS 113*795d594fSAndroid Build Coastguard Worker 114*795d594fSAndroid Build Coastguard Worker #endif // ART_LIBNATIVELOADER_INCLUDE_NATIVELOADER_DLEXT_NAMESPACES_H_ 115