Handle valid symbols that are NULL correctly. (#385388, Felix Kater)
[glib.git] / gmodule / gmodule-dl.c
blobce3377767213d3ca419724b657ad3cdb47707165
1 /* GMODULE - GLIB wrapper code for dynamic module loading
2 * Copyright (C) 1998, 2000 Tim Janik
4 * This library is free software; you can redistribute it and/or
5 * modify it under the terms of the GNU Lesser General Public
6 * License as published by the Free Software Foundation; either
7 * version 2 of the License, or (at your option) any later version.
9 * This library is distributed in the hope that it will be useful,
10 * but WITHOUT ANY WARRANTY; without even the implied warranty of
11 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
12 * Lesser General Public License for more details.
14 * You should have received a copy of the GNU Lesser General Public
15 * License along with this library; if not, write to the
16 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
17 * Boston, MA 02111-1307, USA.
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/.
27 /*
28 * MT safe
31 #include <dlfcn.h>
33 /* Perl includes <nlist.h> and <link.h> instead of <dlfcn.h> on some systmes? */
36 /* dlerror() is not implemented on all systems
38 #ifndef G_MODULE_HAVE_DLERROR
39 # ifdef __NetBSD__
40 # define dlerror() g_strerror (errno)
41 # else /* !__NetBSD__ */
42 /* could we rely on errno's state here? */
43 # define dlerror() "unknown dl-error"
44 # endif /* !__NetBSD__ */
45 #endif /* G_MODULE_HAVE_DLERROR */
47 /* some flags are missing on some systems, so we provide
48 * harmless defaults.
49 * The Perl sources say, RTLD_LAZY needs to be defined as (1),
50 * at least for Solaris 1.
52 * Mandatory:
53 * RTLD_LAZY - resolve undefined symbols as code from the dynamic library
54 * is executed.
55 * RTLD_NOW - resolve all undefined symbols before dlopen returns, and fail
56 * if this cannot be done.
57 * Optionally:
58 * RTLD_GLOBAL - the external symbols defined in the library will be made
59 * available to subsequently loaded libraries.
61 #ifndef RTLD_LAZY
62 #define RTLD_LAZY 1
63 #endif /* RTLD_LAZY */
64 #ifndef RTLD_NOW
65 #define RTLD_NOW 0
66 #endif /* RTLD_NOW */
67 /* some systems (OSF1 V5.0) have broken RTLD_GLOBAL linkage */
68 #ifdef G_MODULE_BROKEN_RTLD_GLOBAL
69 #undef RTLD_GLOBAL
70 #endif /* G_MODULE_BROKEN_RTLD_GLOBAL */
71 #ifndef RTLD_GLOBAL
72 #define RTLD_GLOBAL 0
73 #endif /* RTLD_GLOBAL */
76 /* --- functions --- */
77 static gchar*
78 fetch_dlerror (gboolean replace_null)
80 gchar *msg = dlerror ();
82 /* make sure we always return an error message != NULL, if
83 * expected to do so. */
85 if (!msg && replace_null)
86 return "unknown dl-error";
88 return msg;
91 static gpointer
92 _g_module_open (const gchar *file_name,
93 gboolean bind_lazy,
94 gboolean bind_local)
96 gpointer handle;
98 handle = dlopen (file_name,
99 (bind_local ? 0 : RTLD_GLOBAL) | (bind_lazy ? RTLD_LAZY : RTLD_NOW));
100 if (!handle)
101 g_module_set_error (fetch_dlerror (TRUE));
103 return handle;
106 static gpointer
107 _g_module_self (void)
109 gpointer handle;
111 /* to query symbols from the program itself, special link options
112 * are required on some systems.
115 handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY);
116 if (!handle)
117 g_module_set_error (fetch_dlerror (TRUE));
119 return handle;
122 static void
123 _g_module_close (gpointer handle,
124 gboolean is_unref)
126 /* are there any systems out there that have dlopen()/dlclose()
127 * without a reference count implementation?
129 is_unref |= 1;
131 if (is_unref)
133 if (dlclose (handle) != 0)
134 g_module_set_error (fetch_dlerror (TRUE));
138 static gpointer
139 _g_module_symbol (gpointer handle,
140 const gchar *symbol_name)
142 gpointer p;
143 gchar *msg;
145 fetch_dlerror (FALSE);
146 p = dlsym (handle, symbol_name);
147 msg = fetch_dlerror (FALSE);
148 if (msg)
149 g_module_set_error (msg);
151 return p;
154 static gchar*
155 _g_module_build_path (const gchar *directory,
156 const gchar *module_name)
158 if (directory && *directory) {
159 if (strncmp (module_name, "lib", 3) == 0)
160 return g_strconcat (directory, "/", module_name, NULL);
161 else
162 return g_strconcat (directory, "/lib", module_name, "." G_MODULE_SUFFIX, NULL);
163 } else if (strncmp (module_name, "lib", 3) == 0)
164 return g_strdup (module_name);
165 else
166 return g_strconcat ("lib", module_name, "." G_MODULE_SUFFIX, NULL);