blob: 842ceeaee7217cb74d010987b3751f685237c1ca [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
Elliott Hughesc2622a52024-05-16 19:02:36 +000017#pragma once
Torne (Richard Coles)012cb452014-02-06 14:34:21 +000018
Josh Gao2675a9e2016-04-07 11:21:47 -070019#include <stdbool.h>
Torne (Richard Coles)012cb452014-02-06 14:34:21 +000020#include <stddef.h>
David 'Digit' Turneraad1a392014-11-18 12:21:55 +010021#include <stdint.h>
Torne (Richard Coles)012cb452014-02-06 14:34:21 +000022#include <sys/cdefs.h>
David 'Digit' Turneraad1a392014-11-18 12:21:55 +010023#include <sys/types.h> /* for off64_t */
Torne (Richard Coles)012cb452014-02-06 14:34:21 +000024
Elliott Hughes5046e5f2018-01-25 15:48:32 -080025/**
Dan Albertda1994c2018-04-13 14:32:37 -070026 * @addtogroup libdl Dynamic Linker
27 * @{
28 */
29
30/**
Elliott Hughes5046e5f2018-01-25 15:48:32 -080031 * \file
32 * Advanced dynamic library opening support. Most users will want to use
Elliott Hughesbbd39aa2024-08-13 20:59:16 +000033 * the standard [dlopen(3)](https://man7.org/linux/man-pages/man3/dlopen.3.html)
Elliott Hughes5046e5f2018-01-25 15:48:32 -080034 * functionality in `<dlfcn.h>` instead.
35 */
36
Torne (Richard Coles)012cb452014-02-06 14:34:21 +000037__BEGIN_DECLS
38
Elliott Hughes5046e5f2018-01-25 15:48:32 -080039/** Bitfield definitions for `android_dlextinfo::flags`. */
Torne (Richard Coles)012cb452014-02-06 14:34:21 +000040enum {
Elliott Hughes5046e5f2018-01-25 15:48:32 -080041 /**
42 * When set, the `reserved_addr` and `reserved_size` fields must point to an
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000043 * already-reserved region of address space which will be used to load the
Elliott Hughes5046e5f2018-01-25 15:48:32 -080044 * library if it fits.
45 *
46 * If the reserved region is not large enough, loading will fail.
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000047 */
48 ANDROID_DLEXT_RESERVED_ADDRESS = 0x1,
49
Elliott Hughes5046e5f2018-01-25 15:48:32 -080050 /**
51 * Like `ANDROID_DLEXT_RESERVED_ADDRESS`, but if the reserved region is not large enough,
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +000052 * the linker will choose an available address instead.
53 */
54 ANDROID_DLEXT_RESERVED_ADDRESS_HINT = 0x2,
55
Elliott Hughes5046e5f2018-01-25 15:48:32 -080056 /**
57 * When set, write the GNU RELRO section of the mapped library to `relro_fd`
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000058 * after relocation has been performed, to allow it to be reused by another
59 * process loading the same library at the same address. This implies
Elliott Hughes5046e5f2018-01-25 15:48:32 -080060 * `ANDROID_DLEXT_USE_RELRO`.
61 *
62 * This is mainly useful for the system WebView implementation.
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000063 */
64 ANDROID_DLEXT_WRITE_RELRO = 0x4,
65
Elliott Hughes5046e5f2018-01-25 15:48:32 -080066 /**
67 * When set, compare the GNU RELRO section of the mapped library to `relro_fd`
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000068 * after relocation has been performed, and replace any relocated pages that
69 * are identical with a version mapped from the file.
Elliott Hughes5046e5f2018-01-25 15:48:32 -080070 *
71 * This is mainly useful for the system WebView implementation.
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +000072 */
73 ANDROID_DLEXT_USE_RELRO = 0x8,
74
Elliott Hughes5046e5f2018-01-25 15:48:32 -080075 /**
76 * Use `library_fd` instead of opening the file by name.
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -070077 * The filename parameter is still used to identify the library.
78 */
79 ANDROID_DLEXT_USE_LIBRARY_FD = 0x10,
80
Elliott Hughes5046e5f2018-01-25 15:48:32 -080081 /**
82 * If opening a library using `library_fd` read it starting at `library_fd_offset`.
83 * This is mainly useful for loading a library stored within another file (such as uncompressed
84 * inside a ZIP archive).
85 * This flag is only valid when `ANDROID_DLEXT_USE_LIBRARY_FD` is set.
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070086 */
Dmitriy Ivanova6c12792014-10-21 12:09:18 -070087 ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET = 0x20,
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -070088
Elliott Hughes5046e5f2018-01-25 15:48:32 -080089 /**
90 * When set, do not use `stat(2)` to check if the library has already been loaded.
Dmitriy Ivanov9b821362015-04-02 16:03:56 -070091 *
92 * This flag allows forced loading of the library in the case when for some
93 * reason multiple ELF files share the same filename (because the already-loaded
94 * library has been removed and overwritten, for example).
95 *
Elliott Hughes5046e5f2018-01-25 15:48:32 -080096 * Note that if the library has the same `DT_SONAME` as an old one and some other
97 * 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 -070098 * dependencies.
99 */
100 ANDROID_DLEXT_FORCE_LOAD = 0x40,
101
Elliott Hughes8178c412018-11-05 13:34:36 -0800102 // Historically we had two other options for ART.
Elliott Hughesc2622a52024-05-16 19:02:36 +0000103 // They were last available in API level 28.
Elliott Hughes8178c412018-11-05 13:34:36 -0800104 // Reuse these bits last!
105 // ANDROID_DLEXT_FORCE_FIXED_VADDR = 0x80
106 // ANDROID_DLEXT_LOAD_AT_FIXED_ADDRESS = 0x100
Dmitriy Ivanov126af752015-10-07 16:34:20 -0700107
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800108 /**
109 * This flag used to load library in a different namespace. The namespace is
110 * specified in `library_namespace`.
111 *
112 * This flag is for internal use only (since there is no NDK API for namespaces).
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700113 */
114 ANDROID_DLEXT_USE_NAMESPACE = 0x200,
115
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400116 /**
Elliott Hughesc2622a52024-05-16 19:02:36 +0000117 * Instructs dlopen() to apply `ANDROID_DLEXT_RESERVED_ADDRESS`,
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400118 * `ANDROID_DLEXT_RESERVED_ADDRESS_HINT`, `ANDROID_DLEXT_WRITE_RELRO` and
119 * `ANDROID_DLEXT_USE_RELRO` to any libraries loaded as dependencies of the
120 * main library as well.
121 *
122 * This means that if the main library depends on one or more not-already-loaded libraries, they
123 * will be loaded consecutively into the region starting at `reserved_addr`, and `reserved_size`
124 * must be large enough to contain all of the libraries. The libraries will be loaded in the
125 * deterministic order constructed from the DT_NEEDED entries, rather than the more secure random
126 * order used by default.
127 *
128 * Each library's GNU RELRO sections will be written out to `relro_fd` in the same order they were
129 * loaded. This will mean that the resulting file is dependent on which of the libraries were
130 * already loaded, as only the newly loaded libraries will be included, not any already-loaded
131 * dependencies. The caller should ensure that the set of libraries newly loaded is consistent
132 * for this to be effective.
133 *
134 * This is mainly useful for the system WebView implementation.
135 */
136 ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE = 0x400,
137
138
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800139 /** Mask of valid bits. */
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000140 ANDROID_DLEXT_VALID_FLAG_BITS = ANDROID_DLEXT_RESERVED_ADDRESS |
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000141 ANDROID_DLEXT_RESERVED_ADDRESS_HINT |
142 ANDROID_DLEXT_WRITE_RELRO |
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700143 ANDROID_DLEXT_USE_RELRO |
Dmitriy Ivanov07e5bc12014-10-03 17:52:44 -0700144 ANDROID_DLEXT_USE_LIBRARY_FD |
Dmitriy Ivanov9b821362015-04-02 16:03:56 -0700145 ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET |
Dmitriy Ivanov8a116282015-06-05 22:16:23 -0700146 ANDROID_DLEXT_FORCE_LOAD |
Torne (Richard Coles)efbe9a52018-10-17 15:59:38 -0400147 ANDROID_DLEXT_USE_NAMESPACE |
148 ANDROID_DLEXT_RESERVED_ADDRESS_RECURSIVE,
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000149};
150
Dmitriy Ivanov42d5fcb2015-10-29 17:01:24 -0700151struct android_namespace_t;
152
Elliott Hughesc2622a52024-05-16 19:02:36 +0000153/** Used to pass Android-specific arguments to android_dlopen_ext(). */
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000154typedef struct {
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800155 /** A bitmask of `ANDROID_DLEXT_` enum values. */
Dmitriy Ivanov3a8646f2014-07-08 11:21:56 -0700156 uint64_t flags;
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800157
158 /** Used by `ANDROID_DLEXT_RESERVED_ADDRESS` and `ANDROID_DLEXT_RESERVED_ADDRESS_HINT`. */
zijunzhaoac6d59c2023-04-12 23:26:46 +0000159 void* _Nullable reserved_addr;
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800160 /** Used by `ANDROID_DLEXT_RESERVED_ADDRESS` and `ANDROID_DLEXT_RESERVED_ADDRESS_HINT`. */
Torne (Richard Coles)12bbb912014-02-06 14:34:21 +0000161 size_t reserved_size;
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800162
163 /** Used by `ANDROID_DLEXT_WRITE_RELRO` and `ANDROID_DLEXT_USE_RELRO`. */
Torne (Richard Coles)183ad9d2014-02-27 13:18:00 +0000164 int relro_fd;
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800165
166 /** Used by `ANDROID_DLEXT_USE_LIBRARY_FD`. */
Dmitriy Ivanov04dc91a2014-07-01 14:10:16 -0700167 int library_fd;
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800168 /** Used by `ANDROID_DLEXT_USE_LIBRARY_FD_OFFSET` */
Dmitriy Ivanova6c12792014-10-21 12:09:18 -0700169 off64_t library_fd_offset;
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800170
171 /** Used by `ANDROID_DLEXT_USE_NAMESPACE`. */
zijunzhaoac6d59c2023-04-12 23:26:46 +0000172 struct android_namespace_t* _Nullable library_namespace;
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000173} android_dlextinfo;
174
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800175/**
176 * Opens the given library. The `__filename` and `__flags` arguments are
Elliott Hughesbbd39aa2024-08-13 20:59:16 +0000177 * the same as for [dlopen(3)](https://man7.org/linux/man-pages/man3/dlopen.3.html),
Elliott Hughes5046e5f2018-01-25 15:48:32 -0800178 * with the Android-specific flags supplied via the `flags` member of `__info`.
179 */
Elliott Hughes655e4302023-06-16 12:39:33 -0700180void* _Nullable android_dlopen_ext(const char* _Nullable __filename, int __flags, const android_dlextinfo* _Nullable __info);
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000181
Torne (Richard Coles)012cb452014-02-06 14:34:21 +0000182__END_DECLS
183
Dan Albertda1994c2018-04-13 14:32:37 -0700184/** @} */