1 /* GMODULE - GLIB wrapper code for dynamic module loading
2 * Copyright (C) 1998, 2000 Tim Janik
4 * SPDX-License-Identifier: LGPL-2.1-or-later
6 * This library is free software; you can redistribute it and/or
7 * modify it under the terms of the GNU Lesser General Public
8 * License as published by the Free Software Foundation; either
9 * version 2.1 of the License, or (at your option) any later version.
11 * This library is distributed in the hope that it will be useful,
12 * but WITHOUT ANY WARRANTY; without even the implied warranty of
13 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
14 * Lesser General Public License for more details.
16 * You should have received a copy of the GNU Lesser General Public
17 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
21 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
22 * file for a list of people on the GLib Team. See the ChangeLog
23 * files for a list of changes. These files are distributed with
24 * GLib at ftp://ftp.gtk.org/pub/gtk/.
35 /* Perl includes <nlist.h> and <link.h> instead of <dlfcn.h> on some systems? */
38 /* dlerror() is not implemented on all systems
40 #ifndef G_MODULE_HAVE_DLERROR
42 # define dlerror() g_strerror (errno)
43 # else /* !__NetBSD__ */
44 /* could we rely on errno's state here? */
45 # define dlerror() "unknown dl-error"
46 # endif /* !__NetBSD__ */
47 #endif /* G_MODULE_HAVE_DLERROR */
49 /* some flags are missing on some systems, so we provide
51 * The Perl sources say, RTLD_LAZY needs to be defined as (1),
52 * at least for Solaris 1.
55 * RTLD_LAZY - resolve undefined symbols as code from the dynamic library
57 * RTLD_NOW - resolve all undefined symbols before dlopen returns, and fail
58 * if this cannot be done.
60 * RTLD_GLOBAL - the external symbols defined in the library will be made
61 * available to subsequently loaded libraries.
63 #ifndef HAVE_RTLD_LAZY
65 #endif /* RTLD_LAZY */
69 /* some systems (OSF1 V5.0) have broken RTLD_GLOBAL linkage */
70 #ifdef G_MODULE_BROKEN_RTLD_GLOBAL
72 #undef HAVE_RTLD_GLOBAL
73 #endif /* G_MODULE_BROKEN_RTLD_GLOBAL */
74 #ifndef HAVE_RTLD_GLOBAL
76 #endif /* RTLD_GLOBAL */
79 /* According to POSIX.1-2001, dlerror() is not necessarily thread-safe
80 * (see https://pubs.opengroup.org/onlinepubs/009695399/), and so must be
81 * called within the same locked section as the dlopen()/dlsym() call which
82 * may have caused an error.
84 * However, some libc implementations, such as glibc, implement dlerror() using
85 * thread-local storage, so are thread-safe. As of early 2021:
86 * - glibc is thread-safe: https://github.com/bminor/glibc/blob/HEAD/dlfcn/libc_dlerror_result.c
87 * - uclibc-ng is not thread-safe: https://cgit.uclibc-ng.org/cgi/cgit/uclibc-ng.git/tree/ldso/libdl/libdl.c?id=132decd2a043d0ccf799f42bf89f3ae0c11e95d5#n1075
88 * - Other libc implementations have not been checked, and no problems have
89 * been reported with them in 10 years, so default to assuming that they
90 * don’t need additional thread-safety from GLib
92 #if defined(__UCLIBC__)
93 G_LOCK_DEFINE_STATIC (errors);
95 #define DLERROR_IS_THREADSAFE 1
101 #ifndef DLERROR_IS_THREADSAFE
107 unlock_dlerror (void)
109 #ifndef DLERROR_IS_THREADSAFE
114 /* This should be called with lock_dlerror() held */
116 fetch_dlerror (gboolean replace_null)
118 const gchar *msg = dlerror ();
120 /* make sure we always return an error message != NULL, if
121 * expected to do so. */
123 if (!msg && replace_null)
124 return "unknown dl-error";
130 _g_module_open (const gchar *file_name,
138 handle = dlopen (file_name,
139 (bind_local ? 0 : RTLD_GLOBAL) | (bind_lazy ? RTLD_LAZY : RTLD_NOW));
142 const gchar *message = fetch_dlerror (TRUE);
144 g_module_set_error (message);
145 g_set_error_literal (error, G_MODULE_ERROR, G_MODULE_ERROR_FAILED, message);
154 _g_module_self (void)
158 /* to query symbols from the program itself, special link options
159 * are required on some systems.
162 /* On Android 32 bit (i.e. not __LP64__), dlopen(NULL)
163 * does not work reliable and generally no symbols are found
164 * at all. RTLD_DEFAULT works though.
165 * On Android 64 bit, dlopen(NULL) seems to work but dlsym(handle)
166 * always returns 'undefined symbol'. Only if RTLD_DEFAULT or
167 * NULL is given, dlsym returns an appropriate pointer.
170 #if defined(__BIONIC__) || defined(__NetBSD__)
171 handle = RTLD_DEFAULT;
173 handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY);
176 g_module_set_error (fetch_dlerror (TRUE));
183 _g_module_close (gpointer handle)
185 #if defined(__BIONIC__) || defined(__NetBSD__)
186 if (handle != RTLD_DEFAULT)
190 if (dlclose (handle) != 0)
191 g_module_set_error (fetch_dlerror (TRUE));
197 _g_module_symbol (gpointer handle,
198 const gchar *symbol_name)
204 fetch_dlerror (FALSE);
205 p = dlsym (handle, symbol_name);
206 msg = fetch_dlerror (FALSE);
208 g_module_set_error (msg);