blob: d0200c494cb7b7271092bf49c72be83b258ac55f [file] [log] [blame]
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00001/*
2 * Copyright (C) 2014 The Android Open Source Project
Dimitry Ivanov41fd2952016-05-09 17:37:39 -07003 *
Torne (Richard Coles)012cb452014-02-06 14:34:21 +00004 * 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
Elliott Hughes5046e5f2018-01-25 15:48:32 -080026/**
Dan Albertda1994c2018-04-13 14:32:37 -070027 * @addtogroup libdl Dynamic Linker
28 * @{
29 */
30
31/**
Elliott Hughes5046e5f2018-01-25 15:48:32 -080032 * \file
33 * Advanced dynamic library opening support. Most users will want to use
34 * the standard [dlopen(3)](http://man7.org/linux/man-pages/man3/dlopen.3.html)
35 * functionality in `<dlfcn.h>` instead.
36 */
37
Torne (Richard Coles)012cb452014-02-06 14:34:21 +000038__BEGIN_DECLS
39
Elliott Hughes5046e5f2018-01-25 15:48:32 -080040/** Bitfield definitions for `android_dlextinfo::flags`. */
Torne (Richard Coles)012cb452014-02-06 14:34:21 +000041enum {
Elliott Hughes5046e5f2018-01-25 15:48:32 -080042 /**
43 * When set, the `reserved_addr` and `reserved_size` fields must point to an
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000044 * already-reserved region of address space which will be used to load the
Elliott Hughes5046e5f2018-01-25 15:48:32 -080045 * library if it fits.
46 *
47 * If the reserved region is not large enough, loading will fail.
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000048 */
49 ANDROID_DLEXT_RESERVED_ADDRESS = 0x1,
50
Elliott Hughes5046e5f2018-01-25 15:48:32 -080051 /**
52 * Like `ANDROID_DLEXT_RESERVED_ADDRESS`, but if the reserved region is not large enough,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000053 * the linker will choose an available address instead.
54 */
55 ANDROID_DLEXT_RESERVED_ADDRESS_HINT = 0x2,
56
Elliott Hughes5046e5f2018-01-25 15:48:32 -080057 /**
58 * When set, write the GNU RELRO section of the mapped library to `relro_fd`
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000059 * after relocation has been performed, to allow it to be reused by another
60 * process loading the same library at the same address. This implies
Elliott Hughes5046e5f2018-01-25 15:48:32 -080061 * `ANDROID_DLEXT_USE_RELRO`.
62 *
63 * This is mainly useful for the system WebView implementation.
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000064 */
65 ANDROID_DLEXT_WRITE_RELRO = 0x4,
66
Elliott Hughes5046e5f2018-01-25 15:48:32 -080067 /**
68 * When set, compare the GNU RELRO section of the mapped library to `relro_fd`
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000069 * after relocation has been performed, and replace any relocated pages that
70 * are identical with a version mapped from the file.
Elliott Hughes5046e5f2018-01-25 15:48:32 -080071 *
72 * This is mainly useful for the system WebView implementation.
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000073 */
74 ANDROID_DLEXT_USE_RELRO = 0x8,
75
Elliott Hughes5046e5f2018-01-25 15:48:32 -080076 /**
77 * Use `library_fd` instead of opening the file by name.
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070078 * The filename parameter is still used to identify the library.
79 */
80 ANDROID_DLEXT_USE_LIBRARY_FD = 0x10,
81
Elliott Hughes5046e5f2018-01-25 15:48:32 -080082 /**
83 * If opening a library using `library_fd` read it starting at `library_fd_offset`.
84 * This is mainly useful for loading a library stored within another file (such as uncompressed
85 * inside a ZIP archive).
86 * This flag is only valid when `ANDROID_DLEXT_USE_LIBRARY_FD` is set.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070087 */
Dmitriy Ivanova6c12792014-10-21 12:09:18 -070088 ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET = 0x20,
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070089
Elliott Hughes5046e5f2018-01-25 15:48:32 -080090 /**
91 * When set, do not use `stat(2)` to check if the library has already been loaded.
Dmitriy Ivanov9b821362015-04-02 16:03:56 -070092 *
93 * This flag allows forced loading of the library in the case when for some
94 * reason multiple ELF files share the same filename (because the already-loaded
95 * library has been removed and overwritten, for example).
96 *
Elliott Hughes5046e5f2018-01-25 15:48:32 -080097 * Note that if the library has the same `DT_SONAME` as an old one and some other
98 * library has the soname in its `DT_NEEDED` list, the first one will be used to resolve any
Dmitriy Ivanov9b821362015-04-02 16:03:56 -070099 * dependencies.
100 */
101 ANDROID_DLEXT_FORCE_LOAD = 0x40,
102
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800103 /**
104 * When set, if the minimum `p_vaddr` of the ELF file's `PT_LOAD` segments is non-zero,
Dmitriy Ivanov8a116282015-06-05 22:16:23 -0700105 * the dynamic linker will load it at that address.
106 *
107 * This flag is for ART internal use only.
108 */
109 ANDROID_DLEXT_FORCE_FIXED_VADDR = 0x80,
110
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800111 /**
112 * Instructs dlopen to load the library at the address specified by reserved_addr.
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700113 *
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800114 * The difference between `ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS` and
115 * `ANDROID_DLEXT_RESERVED_ADDRESS` is that for `ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS` the linker
116 * reserves memory at `reserved_addr` whereas for `ANDROID_DLEXT_RESERVED_ADDRESS` the linker
117 * relies on the caller to reserve the memory.
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700118 *
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800119 * This flag can be used with `ANDROID_DLEXT_FORCE_FIXED_VADDR`. When
120 * `ANDROID_DLEXT_FORCE_FIXED_VADDR` is set and `load_bias` is not 0 (`load_bias` is the
121 * minimum `p_vaddr` of all `PT_LOAD` segments) this flag is ignored because the linker has to
122 * pick one address over the other and this way is more convenient for ART.
123 * Note that `ANDROID_DLEXT_FORCE_FIXED_VADDR` does not generate an error when the minimum
124 * `p_vaddr` is 0.
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700125 *
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800126 * Cannot be used with `ANDROID_DLEXT_RESERVED_ADDRESS` or `ANDROID_DLEXT_RESERVED_ADDRESS_HINT`.
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700127 *
128 * This flag is for ART internal use only.
129 */
130 ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS = 0x100,
131
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800132 /**
133 * This flag used to load library in a different namespace. The namespace is
134 * specified in `library_namespace`.
135 *
136 * This flag is for internal use only (since there is no NDK API for namespaces).
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700137 */
138 ANDROID_DLEXT_USE_NAMESPACE = 0x200,
139
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800140 /** Mask of valid bits. */
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000141 ANDROID_DLEXT_VALID_FLAG_BITS = ANDROID_DLEXT_RESERVED_ADDRESS |
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000142 ANDROID_DLEXT_RESERVED_ADDRESS_HINT |
143 ANDROID_DLEXT_WRITE_RELRO |
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700144 ANDROID_DLEXT_USE_RELRO |
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700145 ANDROID_DLEXT_USE_LIBRARY_FD |
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700146 ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET |
Dmitriy Ivanov8a116282015-06-05 22:16:23 -0700147 ANDROID_DLEXT_FORCE_LOAD |
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700148 ANDROID_DLEXT_FORCE_FIXED_VADDR |
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700149 ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS |
150 ANDROID_DLEXT_USE_NAMESPACE,
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000151};
152
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700153struct android_namespace_t;
154
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800155/** Used to pass Android-specific arguments to `android_dlopen_ext`. */
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000156typedef struct {
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800157 /** A bitmask of `ANDROID_DLEXT_` enum values. */
Dmitriy Ivanov3a8646f2014-07-08 11:21:56 -0700158 uint64_t flags;
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800159
160 /** Used by `ANDROID_DLEXT_RESERVED_ADDRESS` and `ANDROID_DLEXT_RESERVED_ADDRESS_HINT`. */
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000161 void* reserved_addr;
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800162 /** Used by `ANDROID_DLEXT_RESERVED_ADDRESS` and `ANDROID_DLEXT_RESERVED_ADDRESS_HINT`. */
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000163 size_t reserved_size;
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800164
165 /** Used by `ANDROID_DLEXT_WRITE_RELRO` and `ANDROID_DLEXT_USE_RELRO`. */
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000166 int relro_fd;
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800167
168 /** Used by `ANDROID_DLEXT_USE_LIBRARY_FD`. */
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700169 int library_fd;
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800170 /** Used by `ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET` */
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700171 off64_t library_fd_offset;
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800172
173 /** Used by `ANDROID_DLEXT_USE_NAMESPACE`. */
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700174 struct android_namespace_t* library_namespace;
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000175} android_dlextinfo;
176
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800177/**
178 * Opens the given library. The `__filename` and `__flags` arguments are
179 * the same as for [dlopen(3)](http://man7.org/linux/man-pages/man3/dlopen.3.html),
180 * with the Android-specific flags supplied via the `flags` member of `__info`.
181 */
Elliott Hughesfaa74342017-08-11 17:34:44 -0700182void* android_dlopen_ext(const char* __filename, int __flags, const android_dlextinfo* __info)
Josh Gao14adff12016-04-29 12:00:55 -0700183 __INTRODUCED_IN(21);
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000184
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000185__END_DECLS
186
Dan Albertda1994c2018-04-13 14:32:37 -0700187/** @} */
188
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800189#endif