Determine the suffix of the shared librarries for this system. This is
[platform/upstream/glib.git] / gmodule / gmodule-dl.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 #  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 */
46
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.
51  *
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.
60  */
61 #ifndef RTLD_GLOBAL
62 #define RTLD_GLOBAL     0
63 #endif  /* RTLD_GLOBAL */
64 #ifndef RTLD_LAZY
65 #define RTLD_LAZY       1
66 #endif  /* RTLD_LAZY */
67 #ifndef RTLD_NOW
68 #define RTLD_NOW        0
69 #endif  /* RTLD_NOW */
70
71
72 /* --- functions --- */
73 static gchar*
74 fetch_dlerror (gboolean replace_null)
75 {
76   gchar *msg = dlerror ();
77
78   /* make sure we always return an error message != NULL, if
79    * expected to do so. */
80
81   if (!msg && replace_null)
82     return "unknown dl-error";
83
84   return msg;
85 }
86
87 static gpointer
88 _g_module_open (const gchar *file_name,
89                 gboolean     bind_lazy)
90 {
91   gpointer handle;
92   
93   handle = dlopen (file_name, RTLD_GLOBAL | (bind_lazy ? RTLD_LAZY : RTLD_NOW));
94   if (!handle)
95     g_module_set_error (fetch_dlerror (TRUE));
96   
97   return handle;
98 }
99
100 static gpointer
101 _g_module_self (void)
102 {
103   gpointer handle;
104   
105   /* to query symbols from the program itself, special link options
106    * are required on some systems.
107    */
108   
109   handle = dlopen (NULL, RTLD_GLOBAL | RTLD_LAZY);
110   if (!handle)
111     g_module_set_error (fetch_dlerror (TRUE));
112   
113   return handle;
114 }
115
116 static void
117 _g_module_close (gpointer handle,
118                  gboolean is_unref)
119 {
120   /* are there any systems out there that have dlopen()/dlclose()
121    * without a reference count implementation?
122    */
123   is_unref |= 1;
124   
125   if (is_unref)
126     {
127       if (dlclose (handle) != 0)
128         g_module_set_error (fetch_dlerror (TRUE));
129     }
130 }
131
132 static gpointer
133 _g_module_symbol (gpointer     handle,
134                   const gchar *symbol_name)
135 {
136   gpointer p;
137   
138   p = dlsym (handle, symbol_name);
139   if (!p)
140     g_module_set_error (fetch_dlerror (FALSE));
141   
142   return p;
143 }
144
145 static gchar*
146 _g_module_build_path (const gchar *directory,
147                       const gchar *module_name)
148 {
149   if (directory && *directory) {
150     if (strncmp (module_name, "lib", 3) == 0)
151       return g_strconcat (directory, "/", module_name, NULL);
152     else
153       return g_strconcat (directory, "/lib", module_name, ".so", NULL);
154   } else if (strncmp (module_name, "lib", 3) == 0)
155     return g_strdup (module_name);
156   else
157     return g_strconcat ("lib", module_name, ".so", NULL);
158 }