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/.
34 /* Perl includes <nlist.h> and <link.h> instead of <dlfcn.h> on some systmes? */
37 /* dlerror() is not implemented on all systems
39 #ifndef G_MODULE_HAVE_DLERROR
41 # define dlerror() g_strerror (errno)
42 # else /* !__NetBSD__ */
43 /* could we rely on errno's state here? */
44 # define dlerror() "unknown dl-error"
45 # endif /* !__NetBSD__ */
46 #endif /* G_MODULE_HAVE_DLERROR */
48 /* some flags are missing on some systems, so we provide
50 * The Perl sources say, RTLD_LAZY needs to be defined as (1),
51 * at least for Solaris 1.
54 * RTLD_LAZY - resolve undefined symbols as code from the dynamic library
56 * RTLD_NOW - resolve all undefined symbols before dlopen returns, and fail
57 * if this cannot be done.
59 * RTLD_GLOBAL - the external symbols defined in the library will be made
60 * available to subsequently loaded libraries.
64 #endif /* RTLD_LAZY */
68 /* some systems (OSF1 V5.0) have broken RTLD_GLOBAL linkage */
69 #ifdef G_MODULE_BROKEN_RTLD_GLOBAL
71 #endif /* G_MODULE_BROKEN_RTLD_GLOBAL */
74 #endif /* RTLD_GLOBAL */
77 /* --- functions --- */
79 fetch_dlerror (gboolean replace_null
)
81 gchar
*msg
= dlerror ();
83 /* make sure we always return an error message != NULL, if
84 * expected to do so. */
86 if (!msg
&& replace_null
)
87 return "unknown dl-error";
93 _g_module_open (const gchar
*file_name
,
99 handle
= dlopen (file_name
,
100 (bind_local
? 0 : RTLD_GLOBAL
) | (bind_lazy
? RTLD_LAZY
: RTLD_NOW
));
102 g_module_set_error (fetch_dlerror (TRUE
));
108 _g_module_self (void)
112 /* to query symbols from the program itself, special link options
113 * are required on some systems.
116 handle
= dlopen (NULL
, RTLD_GLOBAL
| RTLD_LAZY
);
118 g_module_set_error (fetch_dlerror (TRUE
));
124 _g_module_close (gpointer handle
,
127 /* are there any systems out there that have dlopen()/dlclose()
128 * without a reference count implementation?
134 if (dlclose (handle
) != 0)
135 g_module_set_error (fetch_dlerror (TRUE
));
140 _g_module_symbol (gpointer handle
,
141 const gchar
*symbol_name
)
146 fetch_dlerror (FALSE
);
147 p
= dlsym (handle
, symbol_name
);
148 msg
= fetch_dlerror (FALSE
);
150 g_module_set_error (msg
);
156 _g_module_build_path (const gchar
*directory
,
157 const gchar
*module_name
)
159 if (directory
&& *directory
) {
160 if (strncmp (module_name
, "lib", 3) == 0)
161 return g_strconcat (directory
, "/", module_name
, NULL
);
163 return g_strconcat (directory
, "/lib", module_name
, "." G_MODULE_SUFFIX
, NULL
);
164 } else if (strncmp (module_name
, "lib", 3) == 0)
165 return g_strdup (module_name
);
167 return g_strconcat ("lib", module_name
, "." G_MODULE_SUFFIX
, NULL
);