Update German translation
[glib.git] / gmodule / gmodule-dl.c
blob20225df801f4d150f33fe60597e460364c3b8543
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, see <http://www.gnu.org/licenses/>.
19 * Modified by the GLib Team and others 1997-2000. See the AUTHORS
20 * file for a list of people on the GLib Team. See the ChangeLog
21 * files for a list of changes. These files are distributed with
22 * GLib at ftp://ftp.gtk.org/pub/gtk/.
25 /*
26 * MT safe
28 #include "config.h"
30 #include <dlfcn.h>
32 /* Perl includes <nlist.h> and <link.h> instead of <dlfcn.h> on some systmes? */
35 /* dlerror() is not implemented on all systems
37 #ifndef G_MODULE_HAVE_DLERROR
38 # ifdef __NetBSD__
39 # define dlerror() g_strerror (errno)
40 # else /* !__NetBSD__ */
41 /* could we rely on errno's state here? */
42 # define dlerror() "unknown dl-error"
43 # endif /* !__NetBSD__ */
44 #endif /* G_MODULE_HAVE_DLERROR */
46 /* some flags are missing on some systems, so we provide
47 * harmless defaults.
48 * The Perl sources say, RTLD_LAZY needs to be defined as (1),
49 * at least for Solaris 1.
51 * Mandatory:
52 * RTLD_LAZY - resolve undefined symbols as code from the dynamic library
53 * is executed.
54 * RTLD_NOW - resolve all undefined symbols before dlopen returns, and fail
55 * if this cannot be done.
56 * Optionally:
57 * RTLD_GLOBAL - the external symbols defined in the library will be made
58 * available to subsequently loaded libraries.
60 #ifndef RTLD_LAZY
61 #define RTLD_LAZY 1
62 #endif /* RTLD_LAZY */
63 #ifndef RTLD_NOW
64 #define RTLD_NOW 0
65 #endif /* RTLD_NOW */
66 /* some systems (OSF1 V5.0) have broken RTLD_GLOBAL linkage */
67 #ifdef G_MODULE_BROKEN_RTLD_GLOBAL
68 #undef RTLD_GLOBAL
69 #endif /* G_MODULE_BROKEN_RTLD_GLOBAL */
70 #ifndef RTLD_GLOBAL
71 #define RTLD_GLOBAL 0
72 #endif /* RTLD_GLOBAL */
75 /* --- functions --- */
76 static gchar*
77 fetch_dlerror (gboolean replace_null)
79 gchar *msg = dlerror ();
81 /* make sure we always return an error message != NULL, if
82 * expected to do so. */
84 if (!msg && replace_null)
85 return "unknown dl-error";
87 return msg;
90 static gpointer
91 _g_module_open (const gchar *file_name,
92 gboolean bind_lazy,
93 gboolean bind_local)
95 gpointer handle;
97 handle = dlopen (file_name,
98 (bind_local ? 0 : RTLD_GLOBAL) | (bind_lazy ? RTLD_LAZY : RTLD_NOW));
99 if (!handle)
100 g_module_set_error (fetch_dlerror (TRUE));
102 return handle;
105 static gpointer
106 _g_module_self (void)
108 gpointer handle;
110 /* to query symbols from the program itself, special link options
111 * are required on some systems.
114 #ifdef __BIONIC__
115 handle = RTLD_DEFAULT;
116 #else
117 handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY);
118 #endif
119 if (!handle)
120 g_module_set_error (fetch_dlerror (TRUE));
122 return handle;
125 static void
126 _g_module_close (gpointer handle,
127 gboolean is_unref)
129 /* are there any systems out there that have dlopen()/dlclose()
130 * without a reference count implementation?
132 is_unref |= 1;
134 if (is_unref)
136 if (dlclose (handle) != 0)
137 g_module_set_error (fetch_dlerror (TRUE));
141 static gpointer
142 _g_module_symbol (gpointer handle,
143 const gchar *symbol_name)
145 gpointer p;
146 gchar *msg;
148 fetch_dlerror (FALSE);
149 p = dlsym (handle, symbol_name);
150 msg = fetch_dlerror (FALSE);
151 if (msg)
152 g_module_set_error (msg);
154 return p;
157 static gchar*
158 _g_module_build_path (const gchar *directory,
159 const gchar *module_name)
161 if (directory && *directory) {
162 if (strncmp (module_name, "lib", 3) == 0)
163 return g_strconcat (directory, "/", module_name, NULL);
164 else
165 return g_strconcat (directory, "/lib", module_name, "." G_MODULE_SUFFIX, NULL);
166 } else if (strncmp (module_name, "lib", 3) == 0)
167 return g_strdup (module_name);
168 else
169 return g_strconcat ("lib", module_name, "." G_MODULE_SUFFIX, NULL);