blob: a506dc103a2c73344e23ab8b03664c3610caf901 [file] [log] [blame]
The Android Open Source Project1dc9e472009-03-03 19:28:35 -08001/*
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 Hughes5470c182016-07-22 11:36:17 -070028
Elliott Hugheseb4cf412024-03-12 23:45:00 +000029#pragma once
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080030
Evgenii Stepanov0a3637d2016-07-06 13:20:59 -070031#include <stdint.h>
The Android Open Source Project1dc9e472009-03-03 19:28:35 -080032#include <sys/cdefs.h>
33
34__BEGIN_DECLS
35
Elliott Hugheseb4cf412024-03-12 23:45:00 +000036/**
37 * dladdr() returns information using this structure.
38 */
Matt Fischere2a8b1f2009-12-31 12:17:40 -060039typedef struct {
Elliott Hugheseb4cf412024-03-12 23:45:00 +000040 /** Pathname of the shared object that contains the given address. */
zijunzhao447c3462023-03-01 01:32:08 +000041 const char* _Nullable dli_fname;
Elliott Hugheseb4cf412024-03-12 23:45:00 +000042 /** Address at which the shared object is loaded. */
zijunzhao447c3462023-03-01 01:32:08 +000043 void* _Nullable dli_fbase;
Elliott Hugheseb4cf412024-03-12 23:45:00 +000044 /** Name of the nearest symbol with an address lower than the given address. */
zijunzhao447c3462023-03-01 01:32:08 +000045 const char* _Nullable dli_sname;
Elliott Hugheseb4cf412024-03-12 23:45:00 +000046 /** Exact address of the symbol named in `dli_sname`. */
zijunzhao447c3462023-03-01 01:32:08 +000047 void* _Nullable dli_saddr;
Matt Fischere2a8b1f2009-12-31 12:17:40 -060048} Dl_info;
49
Elliott Hugheseb4cf412024-03-12 23:45:00 +000050/**
51 * [dlopen(3)](http://man7.org/linux/man-pages/man3/dlopen.3.html)
52 * loads the given shared library.
53 *
54 * Returns a pointer to an opaque handle for use with other <dlfcn.h> functions
55 * on success, and returns NULL on failure, in which case dlerror() can be used
56 * to retrieve the specific error.
57 */
zijunzhao447c3462023-03-01 01:32:08 +000058void* _Nullable dlopen(const char* _Nullable __filename, int __flag);
Steven Morelandf61b2b12023-11-01 00:16:31 +000059
60/**
61 * [dlclose(3)](http://man7.org/linux/man-pages/man3/dlclose.3.html)
62 * decrements the reference count for the given shared library (and
63 * any libraries brought in by that library's DT_NEEDED entries).
64 *
65 * If a library's reference count hits zero, it may be unloaded.
66 * Code that relies on this is not portable, and may not work on
67 * future versions of Android.
68 *
69 * dlclose() is dangerous because function pointers may or may not
70 * be rendered invalid, global data may or may not be rendered invalid,
71 * and memory may or may not leak. Code with global constructors is
72 * especially problematic. Instead of dlclose, prefer to leave the
73 * library open or, if cleanup is necessary, dlopen() the library in
74 * a child process which can later be killed by the parent or call
75 * exit() itself.
76 *
Elliott Hugheseb4cf412024-03-12 23:45:00 +000077 * Note also that dlclose() interacts badly with thread local variables
78 * with non-trivial destructors, with the
79 * (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].
80 *
Steven Morelandf61b2b12023-11-01 00:16:31 +000081 * Returns 0 on success, and returns -1 on failure, in which case
82 * dlerror() can be used to retrieve the specific error.
83 */
zijunzhao447c3462023-03-01 01:32:08 +000084int dlclose(void* _Nonnull __handle);
Steven Morelandf61b2b12023-11-01 00:16:31 +000085
Elliott Hugheseb4cf412024-03-12 23:45:00 +000086/**
87 * [dlerror(3)](http://man7.org/linux/man-pages/man3/dlerror.3.html)
88 * returns a human-readable error message describing the most recent
89 * failure from one of the <dlfcn.h> functions on the calling thread.
90 *
91 * This function also clears the error, so a second call (or a call
92 * before any failure) will return NULL.
93 *
94 * Returns a pointer to an error on success, and returns NULL if no
95 * error is pending.
96 */
zijunzhao447c3462023-03-01 01:32:08 +000097char* _Nullable dlerror(void);
Steven Morelandf61b2b12023-11-01 00:16:31 +000098
Elliott Hugheseb4cf412024-03-12 23:45:00 +000099/**
100 * [dlsym(3)](http://man7.org/linux/man-pages/man3/dlsym.3.html)
101 * returns a pointer to the symbol with the given name in the shared
102 * library represented by the given handle.
103 *
104 * Returns the address of the symbol on success, and returns NULL on failure,
105 * in which case dlerror() can be used to retrieve the specific error.
106 */
zijunzhao447c3462023-03-01 01:32:08 +0000107void* _Nullable dlsym(void* __BIONIC_COMPLICATED_NULLNESS __handle, const char* _Nullable __symbol);
Elliott Hugheseb4cf412024-03-12 23:45:00 +0000108
109/**
110 * [dlvsym(3)](http://man7.org/linux/man-pages/man3/dlvsym.3.html)
111 * returns a pointer to the symbol with the given name and version in the shared
112 * library represented by the given handle.
113 *
114 * Returns the address of the symbol on success, and returns NULL on failure,
115 * in which case dlerror() can be used to retrieve the specific error.
116 */
zijunzhao447c3462023-03-01 01:32:08 +0000117void* _Nullable dlvsym(void* __BIONIC_COMPLICATED_NULLNESS __handle, const char* _Nullable __symbol, const char* _Nullable __version) __INTRODUCED_IN(24);
Elliott Hugheseb4cf412024-03-12 23:45:00 +0000118
119/**
120 * [dladdr(3)](http://man7.org/linux/man-pages/man3/dladdr.3.html)
121 * returns information about the symbol at the given address.
122 *
123 * Returns non-zero on success, and returns 0 on failure. Note that unlike
124 * the other <dlfcn.h> functions, in this case dlerror() will _not_ have
125 * more information.
126 */
zijunzhao447c3462023-03-01 01:32:08 +0000127int dladdr(const void* _Nonnull __addr, Dl_info* _Nonnull __info);
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800128
Elliott Hugheseb4cf412024-03-12 23:45:00 +0000129/**
130 * A dlsym()/dlvsym() handle that returns the first symbol found in any
131 * shared library using the default search order.
132 */
133#define RTLD_DEFAULT __BIONIC_CAST(reinterpret_cast, void*, 0)
134
135/**
136 * A dlsym()/dlvsym() handle that returns the first symbol found in any
137 * shared library that appears _after_ the object containing the caller.
138 */
139#define RTLD_NEXT __BIONIC_CAST(reinterpret_cast, void*, -1L)
140
141/**
142 * A dlopen() flag to not make symbols from this library available to later
143 * libraries. See also RTLD_GLOBAL.
144 */
Elliott Hughes8ad40932017-06-15 15:12:29 -0700145#define RTLD_LOCAL 0
Elliott Hugheseb4cf412024-03-12 23:45:00 +0000146
147/** Not supported on Android; Android always uses RTLD_NOW. */
Elliott Hughes8ad40932017-06-15 15:12:29 -0700148#define RTLD_LAZY 0x00001
Elliott Hugheseb4cf412024-03-12 23:45:00 +0000149
150/** A dlopen() flag to resolve all undefined symbols before dlopen() returns. */
Elliott Hughes8ad40932017-06-15 15:12:29 -0700151#define RTLD_NOW 0x00002
Elliott Hugheseb4cf412024-03-12 23:45:00 +0000152
153/**
154 * A dlopen() flag to not actually load the given library;
155 * used to test whether the library is already loaded.
156 */
Elliott Hughes8ad40932017-06-15 15:12:29 -0700157#define RTLD_NOLOAD 0x00004
Elliott Hugheseb4cf412024-03-12 23:45:00 +0000158
159/**
160 * A dlopen() flag to make symbols from this library available to later
161 * libraries. See also RTLD_LOCAL.
162 */
Elliott Hughes8ad40932017-06-15 15:12:29 -0700163#define RTLD_GLOBAL 0x00100
Elliott Hugheseb4cf412024-03-12 23:45:00 +0000164
165/**
166 * A dlopen() flag to ignore later dlclose() calls on this library.
167 */
Elliott Hughes8ad40932017-06-15 15:12:29 -0700168#define RTLD_NODELETE 0x01000
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800169
Elliott Hugheseb4cf412024-03-12 23:45:00 +0000170/* LP32 has historical ABI breakage. */
Elliott Hughes8ad40932017-06-15 15:12:29 -0700171#if !defined(__LP64__)
Elliott Hugheseb4cf412024-03-12 23:45:00 +0000172#undef RTLD_DEFAULT
173#define RTLD_DEFAULT __BIONIC_CAST(reinterpret_cast, void*, 0xffffffff)
174#undef RTLD_NEXT
175#define RTLD_NEXT __BIONIC_CAST(reinterpret_cast, void*, 0xfffffffe)
Elliott Hughes8ad40932017-06-15 15:12:29 -0700176#undef RTLD_NOW
177#define RTLD_NOW 0x00000
178#undef RTLD_GLOBAL
179#define RTLD_GLOBAL 0x00002
Dmitriy Ivanovb648a8a2014-05-19 15:06:58 -0700180#endif
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800181
The Android Open Source Project1dc9e472009-03-03 19:28:35 -0800182__END_DECLS