The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 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 | */ |
Elliott Hughes | 5470c18 | 2016-07-22 11:36:17 -0700 | [diff] [blame] | 28 | |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 29 | #pragma once |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 30 | |
| 31 | #include <sys/cdefs.h> |
| 32 | |
Elliott Hughes | 414dd2d | 2024-10-16 14:48:30 +0000 | [diff] [blame] | 33 | #include <stdint.h> |
| 34 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 35 | __BEGIN_DECLS |
| 36 | |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 37 | /** |
| 38 | * dladdr() returns information using this structure. |
| 39 | */ |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 40 | typedef struct { |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 41 | /** Pathname of the shared object that contains the given address. */ |
zijunzhao | 447c346 | 2023-03-01 01:32:08 +0000 | [diff] [blame] | 42 | const char* _Nullable dli_fname; |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 43 | /** Address at which the shared object is loaded. */ |
zijunzhao | 447c346 | 2023-03-01 01:32:08 +0000 | [diff] [blame] | 44 | void* _Nullable dli_fbase; |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 45 | /** Name of the nearest symbol with an address lower than the given address. */ |
zijunzhao | 447c346 | 2023-03-01 01:32:08 +0000 | [diff] [blame] | 46 | const char* _Nullable dli_sname; |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 47 | /** Exact address of the symbol named in `dli_sname`. */ |
zijunzhao | 447c346 | 2023-03-01 01:32:08 +0000 | [diff] [blame] | 48 | void* _Nullable dli_saddr; |
Matt Fischer | e2a8b1f | 2009-12-31 12:17:40 -0600 | [diff] [blame] | 49 | } Dl_info; |
| 50 | |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 51 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 52 | * [dlopen(3)](https://man7.org/linux/man-pages/man3/dlopen.3.html) |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 53 | * loads the given shared library. |
| 54 | * |
| 55 | * Returns a pointer to an opaque handle for use with other <dlfcn.h> functions |
| 56 | * on success, and returns NULL on failure, in which case dlerror() can be used |
| 57 | * to retrieve the specific error. |
| 58 | */ |
zijunzhao | 447c346 | 2023-03-01 01:32:08 +0000 | [diff] [blame] | 59 | void* _Nullable dlopen(const char* _Nullable __filename, int __flag); |
Steven Moreland | f61b2b1 | 2023-11-01 00:16:31 +0000 | [diff] [blame] | 60 | |
| 61 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 62 | * [dlclose(3)](https://man7.org/linux/man-pages/man3/dlclose.3.html) |
Steven Moreland | f61b2b1 | 2023-11-01 00:16:31 +0000 | [diff] [blame] | 63 | * decrements the reference count for the given shared library (and |
| 64 | * any libraries brought in by that library's DT_NEEDED entries). |
| 65 | * |
| 66 | * If a library's reference count hits zero, it may be unloaded. |
| 67 | * Code that relies on this is not portable, and may not work on |
| 68 | * future versions of Android. |
| 69 | * |
| 70 | * dlclose() is dangerous because function pointers may or may not |
| 71 | * be rendered invalid, global data may or may not be rendered invalid, |
| 72 | * and memory may or may not leak. Code with global constructors is |
| 73 | * especially problematic. Instead of dlclose, prefer to leave the |
| 74 | * library open or, if cleanup is necessary, dlopen() the library in |
| 75 | * a child process which can later be killed by the parent or call |
| 76 | * exit() itself. |
| 77 | * |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 78 | * Note also that dlclose() interacts badly with thread local variables |
| 79 | * with non-trivial destructors, with the |
| 80 | * (exact behavior varying by API level)[https://android.googlesource.com/platform/bionic/+/main/android-changes-for-ndk-developers.md#dlclose-interacts-badly-with-thread-local-variables-with-non_trivial-destructors]. |
| 81 | * |
Steven Moreland | f61b2b1 | 2023-11-01 00:16:31 +0000 | [diff] [blame] | 82 | * Returns 0 on success, and returns -1 on failure, in which case |
| 83 | * dlerror() can be used to retrieve the specific error. |
| 84 | */ |
zijunzhao | 447c346 | 2023-03-01 01:32:08 +0000 | [diff] [blame] | 85 | int dlclose(void* _Nonnull __handle); |
Steven Moreland | f61b2b1 | 2023-11-01 00:16:31 +0000 | [diff] [blame] | 86 | |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 87 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 88 | * [dlerror(3)](https://man7.org/linux/man-pages/man3/dlerror.3.html) |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 89 | * returns a human-readable error message describing the most recent |
| 90 | * failure from one of the <dlfcn.h> functions on the calling thread. |
| 91 | * |
| 92 | * This function also clears the error, so a second call (or a call |
| 93 | * before any failure) will return NULL. |
| 94 | * |
| 95 | * Returns a pointer to an error on success, and returns NULL if no |
| 96 | * error is pending. |
| 97 | */ |
zijunzhao | 447c346 | 2023-03-01 01:32:08 +0000 | [diff] [blame] | 98 | char* _Nullable dlerror(void); |
Steven Moreland | f61b2b1 | 2023-11-01 00:16:31 +0000 | [diff] [blame] | 99 | |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 100 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 101 | * [dlsym(3)](https://man7.org/linux/man-pages/man3/dlsym.3.html) |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 102 | * returns a pointer to the symbol with the given name in the shared |
Elliott Hughes | edc73c5 | 2024-03-26 16:46:29 +0000 | [diff] [blame] | 103 | * library represented by the given handle. The handle may have been |
| 104 | * returned from dlopen(), or can be RTLD_DEFAULT or RTLD_NEXT. |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 105 | * |
| 106 | * Returns the address of the symbol on success, and returns NULL on failure, |
| 107 | * in which case dlerror() can be used to retrieve the specific error. |
| 108 | */ |
zijunzhao | 447c346 | 2023-03-01 01:32:08 +0000 | [diff] [blame] | 109 | void* _Nullable dlsym(void* __BIONIC_COMPLICATED_NULLNESS __handle, const char* _Nullable __symbol); |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 110 | |
| 111 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 112 | * [dlvsym(3)](https://man7.org/linux/man-pages/man3/dlvsym.3.html) |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 113 | * returns a pointer to the symbol with the given name and version in the shared |
Elliott Hughes | edc73c5 | 2024-03-26 16:46:29 +0000 | [diff] [blame] | 114 | * library represented by the given handle. The handle may have been |
| 115 | * returned from dlopen(), or can be RTLD_DEFAULT or RTLD_NEXT. |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 116 | * |
| 117 | * Returns the address of the symbol on success, and returns NULL on failure, |
| 118 | * in which case dlerror() can be used to retrieve the specific error. |
| 119 | */ |
Dan Albert | 02ce401 | 2024-10-25 19:13:49 +0000 | [diff] [blame^] | 120 | |
| 121 | #if __BIONIC_AVAILABILITY_GUARD(24) |
zijunzhao | 447c346 | 2023-03-01 01:32:08 +0000 | [diff] [blame] | 122 | void* _Nullable dlvsym(void* __BIONIC_COMPLICATED_NULLNESS __handle, const char* _Nullable __symbol, const char* _Nullable __version) __INTRODUCED_IN(24); |
Dan Albert | 02ce401 | 2024-10-25 19:13:49 +0000 | [diff] [blame^] | 123 | #endif /* __BIONIC_AVAILABILITY_GUARD(24) */ |
| 124 | |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 125 | |
| 126 | /** |
Elliott Hughes | bbd39aa | 2024-08-13 20:59:16 +0000 | [diff] [blame] | 127 | * [dladdr(3)](https://man7.org/linux/man-pages/man3/dladdr.3.html) |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 128 | * returns information about the symbol at the given address. |
| 129 | * |
| 130 | * Returns non-zero on success, and returns 0 on failure. Note that unlike |
| 131 | * the other <dlfcn.h> functions, in this case dlerror() will _not_ have |
| 132 | * more information. |
| 133 | */ |
zijunzhao | 447c346 | 2023-03-01 01:32:08 +0000 | [diff] [blame] | 134 | int dladdr(const void* _Nonnull __addr, Dl_info* _Nonnull __info); |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 135 | |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 136 | /** |
| 137 | * A dlsym()/dlvsym() handle that returns the first symbol found in any |
| 138 | * shared library using the default search order. |
| 139 | */ |
| 140 | #define RTLD_DEFAULT __BIONIC_CAST(reinterpret_cast, void*, 0) |
| 141 | |
| 142 | /** |
| 143 | * A dlsym()/dlvsym() handle that returns the first symbol found in any |
| 144 | * shared library that appears _after_ the object containing the caller. |
| 145 | */ |
| 146 | #define RTLD_NEXT __BIONIC_CAST(reinterpret_cast, void*, -1L) |
| 147 | |
| 148 | /** |
| 149 | * A dlopen() flag to not make symbols from this library available to later |
| 150 | * libraries. See also RTLD_GLOBAL. |
| 151 | */ |
Elliott Hughes | 8ad4093 | 2017-06-15 15:12:29 -0700 | [diff] [blame] | 152 | #define RTLD_LOCAL 0 |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 153 | |
Elliott Hughes | 82e48ae | 2024-07-15 15:53:33 +0000 | [diff] [blame] | 154 | /** |
| 155 | * Not supported on Android. Android always uses RTLD_NOW for security reasons. |
| 156 | * Resolving all undefined symbols before dlopen() returns means that RELRO |
| 157 | * protections can be applied to the PLT before dlopen() returns. |
| 158 | */ |
Elliott Hughes | 8ad4093 | 2017-06-15 15:12:29 -0700 | [diff] [blame] | 159 | #define RTLD_LAZY 0x00001 |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 160 | |
| 161 | /** A dlopen() flag to resolve all undefined symbols before dlopen() returns. */ |
Elliott Hughes | 8ad4093 | 2017-06-15 15:12:29 -0700 | [diff] [blame] | 162 | #define RTLD_NOW 0x00002 |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 163 | |
| 164 | /** |
| 165 | * A dlopen() flag to not actually load the given library; |
| 166 | * used to test whether the library is already loaded. |
| 167 | */ |
Elliott Hughes | 8ad4093 | 2017-06-15 15:12:29 -0700 | [diff] [blame] | 168 | #define RTLD_NOLOAD 0x00004 |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 169 | |
| 170 | /** |
| 171 | * A dlopen() flag to make symbols from this library available to later |
| 172 | * libraries. See also RTLD_LOCAL. |
| 173 | */ |
Elliott Hughes | 8ad4093 | 2017-06-15 15:12:29 -0700 | [diff] [blame] | 174 | #define RTLD_GLOBAL 0x00100 |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 175 | |
| 176 | /** |
| 177 | * A dlopen() flag to ignore later dlclose() calls on this library. |
| 178 | */ |
Elliott Hughes | 8ad4093 | 2017-06-15 15:12:29 -0700 | [diff] [blame] | 179 | #define RTLD_NODELETE 0x01000 |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 180 | |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 181 | /* LP32 has historical ABI breakage. */ |
Elliott Hughes | 8ad4093 | 2017-06-15 15:12:29 -0700 | [diff] [blame] | 182 | #if !defined(__LP64__) |
Elliott Hughes | eb4cf41 | 2024-03-12 23:45:00 +0000 | [diff] [blame] | 183 | #undef RTLD_DEFAULT |
| 184 | #define RTLD_DEFAULT __BIONIC_CAST(reinterpret_cast, void*, 0xffffffff) |
| 185 | #undef RTLD_NEXT |
| 186 | #define RTLD_NEXT __BIONIC_CAST(reinterpret_cast, void*, 0xfffffffe) |
Elliott Hughes | 8ad4093 | 2017-06-15 15:12:29 -0700 | [diff] [blame] | 187 | #undef RTLD_NOW |
| 188 | #define RTLD_NOW 0x00000 |
| 189 | #undef RTLD_GLOBAL |
| 190 | #define RTLD_GLOBAL 0x00002 |
Dmitriy Ivanov | b648a8a | 2014-05-19 15:06:58 -0700 | [diff] [blame] | 191 | #endif |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 192 | |
The Android Open Source Project | 1dc9e47 | 2009-03-03 19:28:35 -0800 | [diff] [blame] | 193 | __END_DECLS |