blob: 05a27f3f6d7dc6da2b07d5acd53dacee40873506 [file] [log] [blame]
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
3
4 * Licensed under the Apache License, Version 2.0 (the "License");
5 * you may not use this file except in compliance with the License.
6 * You may obtain a copy of the License at
7 *
8 * http://www.apache.org/licenses/LICENSE-2.0
9 *
10 * Unless required by applicable law or agreed to in writing, software
11 * distributed under the License is distributed on an "AS IS" BASIS,
12 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
13 * See the License for the specific language governing permissions and
14 * limitations under the License.
15 */
16
17#ifndef __ANDROID_DLEXT_H__
18#define __ANDROID_DLEXT_H__
19
Josh Gao2675a9e2016-04-07 11:21:47 -070020#include <stdbool.h>
Torne (Richard Coles)012cb452014-02-06 14:34:21 +000021#include <stddef.h>
David 'Digit' Turneraad1a392014-11-18 12:21:55 +010022#include <stdint.h>
Torne (Richard Coles)012cb452014-02-06 14:34:21 +000023#include <sys/cdefs.h>
David 'Digit' Turneraad1a392014-11-18 12:21:55 +010024#include <sys/types.h> /* for off64_t */
Torne (Richard Coles)012cb452014-02-06 14:34:21 +000025
26__BEGIN_DECLS
27
28/* bitfield definitions for android_dlextinfo.flags */
29enum {
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000030 /* When set, the reserved_addr and reserved_size fields must point to an
31 * already-reserved region of address space which will be used to load the
32 * library if it fits. If the reserved region is not large enough, the load
33 * will fail.
34 */
35 ANDROID_DLEXT_RESERVED_ADDRESS = 0x1,
36
37 /* As DLEXT_RESERVED_ADDRESS, but if the reserved region is not large enough,
38 * the linker will choose an available address instead.
39 */
40 ANDROID_DLEXT_RESERVED_ADDRESS_HINT = 0x2,
41
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000042 /* When set, write the GNU RELRO section of the mapped library to relro_fd
43 * after relocation has been performed, to allow it to be reused by another
44 * process loading the same library at the same address. This implies
45 * ANDROID_DLEXT_USE_RELRO.
46 */
47 ANDROID_DLEXT_WRITE_RELRO = 0x4,
48
49 /* When set, compare the GNU RELRO section of the mapped library to relro_fd
50 * after relocation has been performed, and replace any relocated pages that
51 * are identical with a version mapped from the file.
52 */
53 ANDROID_DLEXT_USE_RELRO = 0x8,
54
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070055 /* Instruct dlopen to use library_fd instead of opening file by name.
56 * The filename parameter is still used to identify the library.
57 */
58 ANDROID_DLEXT_USE_LIBRARY_FD = 0x10,
59
Dmitriy Ivanova6c12792014-10-21 12:09:18 -070060 /* If opening a library using library_fd read it starting at library_fd_offset.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070061 * This flag is only valid when ANDROID_DLEXT_USE_LIBRARY_FD is set.
62 */
Dmitriy Ivanova6c12792014-10-21 12:09:18 -070063 ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET = 0x20,
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070064
Dmitriy Ivanov9b821362015-04-02 16:03:56 -070065 /* When set, do not check if the library has already been loaded by file stat(2)s.
66 *
67 * This flag allows forced loading of the library in the case when for some
68 * reason multiple ELF files share the same filename (because the already-loaded
69 * library has been removed and overwritten, for example).
70 *
71 * Note that if the library has the same dt_soname as an old one and some other
72 * library has the soname in DT_NEEDED list, the first one will be used to resolve any
73 * dependencies.
74 */
75 ANDROID_DLEXT_FORCE_LOAD = 0x40,
76
Dmitriy Ivanov8a116282015-06-05 22:16:23 -070077 /* When set, if the minimum p_vaddr of the ELF file's PT_LOAD segments is non-zero,
78 * the dynamic linker will load it at that address.
79 *
80 * This flag is for ART internal use only.
81 */
82 ANDROID_DLEXT_FORCE_FIXED_VADDR = 0x80,
83
Dmitriy Ivanov126af752015-10-07 16:34:20 -070084 /* Instructs dlopen to load the library at the address specified by reserved_addr.
85 *
86 * The difference between ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS and ANDROID_DLEXT_RESERVED_ADDRESS
87 * is that for ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS the linker reserves memory at reserved_addr
88 * whereas for ANDROID_DLEXT_RESERVED_ADDRESS the linker relies on the caller to reserve the memory.
89 *
90 * This flag can be used with ANDROID_DLEXT_FORCE_FIXED_VADDR; when ANDROID_DLEXT_FORCE_FIXED_VADDR
91 * is set and load_bias is not 0 (load_bias is min(p_vaddr) of PT_LOAD segments) this flag is ignored.
92 * This is implemented this way because the linker has to pick one address over the other and this
93 * way is more convenient for art. Note that ANDROID_DLEXT_FORCE_FIXED_VADDR does not generate
94 * an error when min(p_vaddr) is 0.
95 *
96 * Cannot be used with ANDROID_DLEXT_RESERVED_ADDRESS or ANDROID_DLEXT_RESERVED_ADDRESS_HINT.
97 *
98 * This flag is for ART internal use only.
99 */
100 ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS = 0x100,
101
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700102 /* This flag used to load library in a different namespace. The namespace is
103 * specified in library_namespace.
104 */
105 ANDROID_DLEXT_USE_NAMESPACE = 0x200,
106
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000107 /* Mask of valid bits */
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000108 ANDROID_DLEXT_VALID_FLAG_BITS = ANDROID_DLEXT_RESERVED_ADDRESS |
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000109 ANDROID_DLEXT_RESERVED_ADDRESS_HINT |
110 ANDROID_DLEXT_WRITE_RELRO |
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700111 ANDROID_DLEXT_USE_RELRO |
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700112 ANDROID_DLEXT_USE_LIBRARY_FD |
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700113 ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET |
Dmitriy Ivanov8a116282015-06-05 22:16:23 -0700114 ANDROID_DLEXT_FORCE_LOAD |
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700115 ANDROID_DLEXT_FORCE_FIXED_VADDR |
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700116 ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS |
117 ANDROID_DLEXT_USE_NAMESPACE,
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000118};
119
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700120struct android_namespace_t;
121
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000122typedef struct {
Dmitriy Ivanov3a8646f2014-07-08 11:21:56 -0700123 uint64_t flags;
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000124 void* reserved_addr;
125 size_t reserved_size;
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000126 int relro_fd;
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700127 int library_fd;
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700128 off64_t library_fd_offset;
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700129 struct android_namespace_t* library_namespace;
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000130} android_dlextinfo;
131
Josh Gao14adff12016-04-29 12:00:55 -0700132extern void* android_dlopen_ext(const char* filename, int flag, const android_dlextinfo* extinfo)
133 __INTRODUCED_IN(21);
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000134
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700135/*
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800136 * Initializes public and anonymous namespaces. The public_ns_sonames is the list of sonames
137 * to be included into public namespace separated by colon. Example: "libc.so:libm.so:libdl.so".
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700138 * The libraries in this list should be loaded prior to this call.
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800139 *
140 * The anon_ns_library_path is the search path for anonymous namespace. The anonymous namespace
141 * is used in the case when linker cannot identify the caller of dlopen/dlsym. This happens
142 * for the code not loaded by dynamic linker; for example calls from the mono-compiled code.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700143 */
Dmitriy Ivanov1ffec1c2015-11-23 11:26:35 -0800144extern bool android_init_namespaces(const char* public_ns_sonames,
145 const char* anon_ns_library_path);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700146
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800147
148enum {
149 /* A regular namespace is the namespace with a custom search path that does
150 * not impose any restrictions on the location of native libraries.
151 */
152 ANDROID_NAMESPACE_TYPE_REGULAR = 0,
153
154 /* An isolated namespace requires all the libraries to be on the search path
155 * or under permitted_when_isolated_path. The search path is the union of
156 * ld_library_path and default_library_path.
157 */
158 ANDROID_NAMESPACE_TYPE_ISOLATED = 1,
159
160 /* The shared namespace clones the list of libraries of the caller namespace upon creation
161 * which means that they are shared between namespaces - the caller namespace and the new one
162 * will use the same copy of a library if it was loaded prior to android_create_namespace call.
163 *
164 * Note that libraries loaded after the namespace is created will not be shared.
165 *
166 * Shared namespaces can be isolated or regular. Note that they do not inherit the search path nor
167 * permitted_path from the caller's namespace.
168 */
169 ANDROID_NAMESPACE_TYPE_SHARED = 2,
170 ANDROID_NAMESPACE_TYPE_SHARED_ISOLATED = ANDROID_NAMESPACE_TYPE_SHARED |
171 ANDROID_NAMESPACE_TYPE_ISOLATED,
172};
173
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700174/*
175 * Creates new linker namespace.
176 * ld_library_path and default_library_path represent the search path
177 * for the libraries in the namespace.
178 *
179 * The libraries in the namespace are searched by folowing order:
180 * 1. ld_library_path (Think of this as namespace-local LD_LIBRARY_PATH)
181 * 2. In directories specified by DT_RUNPATH of the "needed by" binary.
182 * 3. deault_library_path (This of this as namespace-local default library path)
183 *
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800184 * When type is ANDROID_NAMESPACE_TYPE_ISOLATED the resulting namespace requires all of
185 * the libraries to be on the search path or under the permitted_when_isolated_path;
186 * the search_path is ld_library_path:default_library_path. Note that the
187 * permitted_when_isolated_path path is not part of the search_path and
188 * does not affect the search order. It is a way to allow loading libraries from specific
189 * locations when using absolute path.
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800190 * If a library or any of its dependencies are outside of the permitted_when_isolated_path
191 * and search_path, and it is not part of the public namespace dlopen will fail.
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700192 */
193extern struct android_namespace_t* android_create_namespace(const char* name,
194 const char* ld_library_path,
195 const char* default_library_path,
Dimitry Ivanov7331fe12015-12-14 14:11:17 -0800196 uint64_t type,
Dimitry Ivanov284ae352015-12-08 10:47:13 -0800197 const char* permitted_when_isolated_path);
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700198
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000199__END_DECLS
200
201#endif /* __ANDROID_DLEXT_H__ */