applied patch from David Schleef <ds@schleef.org> which implements a
[platform/upstream/glib.git] / gmodule / gmodule-os2.c
1 /* GMODULE - GLIB wrapper code for dynamic module loading
2  * Copyright (C) 1998, 2000 Tim Janik
3  *
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.
8  *
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.
13  *
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.
18  */
19
20 /*
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/. 
25  */
26
27 /* 
28  * MT safe
29  */
30
31 #include <dlfcn.h>
32
33 /* Perl includes <nlist.h> and <link.h> instead of <dlfcn.h> on some systmes? */
34
35
36 /* dlerror() is not implemented on all systems
37  */
38 #ifndef G_MODULE_HAVE_DLERROR
39 /* could we rely on errno's state here? */
40 #  define dlerror()     "unknown dl-error"
41 #endif  /* G_MODULE_HAVE_DLERROR */
42
43 /* some flags are missing on some systems, so we provide
44  * harmless defaults.
45  * The Perl sources say, RTLD_LAZY needs to be defined as (1),
46  * at least for Solaris 1.
47  *
48  * Mandatory:
49  * RTLD_LAZY   - resolve undefined symbols as code from the dynamic library
50  *               is executed.
51  * RTLD_NOW    - resolve all undefined symbols before dlopen returns, and fail
52  *               if this cannot be done.
53  * Optionally:
54  * RTLD_GLOBAL - the external symbols defined in the library will be made
55  *               available to subsequently loaded libraries.
56  */
57 #ifndef RTLD_GLOBAL
58 #define RTLD_GLOBAL     0
59 #endif  /* RTLD_GLOBAL */
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
67
68 /* --- functions --- */
69 static gpointer
70 _g_module_open (const gchar    *file_name,
71                 gboolean        bind_lazy,
72                 gboolean        bind_local)
73 {
74   gpointer handle;
75   
76   handle = dlopen (file_name,
77         (bind_local ? 0 : RTLD_GLOBAL) | (bind_lazy ? RTLD_LAZY : RTLD_NOW));
78   if (!handle)
79     g_module_set_error (dlerror ());
80   
81   return handle;
82 }
83
84 static gpointer
85 _g_module_self (void)
86 {
87   gpointer handle;
88   
89   /* to query symbols from the program itself, special link options
90    * are required on some systems.
91    */
92   
93   /* XXX, not supported */
94   handle = NULL;
95   g_module_set_error ("module handle for self not supported");
96   
97   return handle;
98 }
99
100 static void
101 _g_module_close (gpointer handle,
102                  gboolean is_unref)
103 {
104   /* are there any systems out there that have dlopen()/dlclose()
105    * without a reference count implementation?
106    */
107   is_unref |= 1;
108   
109   if (is_unref)
110     {
111       /* XXX, no return code */
112       dlclose (handle);
113     }
114 }
115
116 static gpointer
117 _g_module_symbol (gpointer     handle,
118                   const gchar *symbol_name)
119 {
120   gpointer p;
121   
122   p = dlsym (handle, symbol_name);
123   if (!p)
124     g_module_set_error (dlerror ());
125   
126   return p;
127 }
128
129 static gchar*
130 _g_module_build_path (const gchar *directory,
131                       const gchar *module_name)
132 {
133   gchar *suffix = strrchr(module_name, '.');
134   if (directory && *directory)
135     if (suffix && (stricmp (suffix, ".dll") == 0))
136       return g_strconcat (directory, "/", module_name, NULL);
137     else
138       return g_strconcat (directory, "/", module_name, ".dll", NULL);
139   else if (suffix && (stricmp (suffix, ".dll") == 0))
140     return g_strdup (module_name);
141   else
142     return g_strconcat (module_name, ".dll", NULL);
143 }