2.9.0
[platform/upstream/glib.git] / gmodule / gmodule-dld.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 <dl.h>
32
33
34 /* some flags are missing on some systems, so we provide
35  * harmless defaults.
36  *
37  * Mandatory:
38  * BIND_IMMEDIATE  - Resolve symbol references when the library is loaded.
39  * BIND_DEFERRED   - Delay code symbol resolution until actual reference.
40  *
41  * Optionally:
42  * BIND_FIRST      - Place the library at the head of the symbol search order.
43  * BIND_NONFATAL   - The default BIND_IMMEDIATE behavior is to treat all unsatisfied
44  *                   symbols as fatal.  This flag allows binding of unsatisfied code
45  *                   symbols to be deferred until use.
46  *                   [Perl: For certain libraries, like DCE, deferred binding often
47  *                   causes run time problems.  Adding BIND_NONFATAL to BIND_IMMEDIATE
48  *                   still allows unresolved references in situations like this.]
49  * BIND_NOSTART    - Do not call the initializer for the shared library when the
50  *                   library is loaded, nor on a future call to shl_unload().
51  * BIND_VERBOSE    - Print verbose messages concerning possible unsatisfied symbols.
52  *
53  * hp9000s700/hp9000s800:
54  * BIND_RESTRICTED - Restrict symbols visible by the library to those present at
55  *                   library load time.
56  * DYNAMIC_PATH    - Allow the loader to dynamically search for the library specified
57  *                   by the path argument.
58  */
59 #ifndef DYNAMIC_PATH
60 #define DYNAMIC_PATH    0
61 #endif  /* DYNAMIC_PATH */
62 #ifndef BIND_RESTRICTED
63 #define BIND_RESTRICTED 0
64 #endif  /* BIND_RESTRICTED */
65
66 #define OPT_BIND_FLAGS  (BIND_NONFATAL | BIND_VERBOSE)
67
68
69 /* --- functions --- */
70
71 /*
72  * shl_load() does not appear to support making symbols invisible to
73  * the global namespace.  However, the default is to put the library
74  * last in the search order, which is approximately what we want,
75  * since it will cause symbols that conflict with existing symbols to
76  * be invisible.  It is unclear if BIND_FIRST should be used when
77  * bind_local==0, since it may cause the loaded symbols to be used
78  * preferentially to the application's symbols, which is Almost
79  * Always Wrong.  --ds
80  */
81 static gpointer
82 _g_module_open (const gchar *file_name,
83                 gboolean     bind_lazy,
84                 gboolean     bind_local)
85 {
86   shl_t shl_handle;
87   
88   shl_handle = shl_load (file_name,
89                          (bind_lazy ? BIND_DEFERRED : BIND_IMMEDIATE) | OPT_BIND_FLAGS, 0);
90   if (!shl_handle)
91     {
92       /* the hp-docs say we should better abort() if errno==ENOSYM ;( */
93       g_module_set_error (g_strerror (errno));
94     }
95   
96   return (gpointer) shl_handle;
97 }
98
99 static gpointer
100 _g_module_self (void)
101 {
102   shl_t shl_handle;
103   
104   shl_handle = PROG_HANDLE;
105   if (!shl_handle)
106     g_module_set_error (g_strerror (errno));
107   
108   return shl_handle;
109 }
110
111 static void
112 _g_module_close (gpointer handle,
113                  gboolean is_unref)
114 {
115   if (!is_unref)
116     {
117       if (shl_unload ((shl_t) handle) != 0)
118         g_module_set_error (g_strerror (errno));
119     }
120 }
121
122 static gpointer
123 _g_module_symbol (gpointer     handle,
124                   const gchar *symbol_name)
125 {
126   gpointer p = NULL;
127   
128   /* should we restrict lookups to TYPE_PROCEDURE?
129    */
130   if (handle == PROG_HANDLE)
131     {
132       /* PROG_HANDLE will only lookup symbols in the program itself, not honouring
133        * libraries. passing NULL as a handle will also try to lookup the symbol
134        * in currently loaded libraries. fix pointed out and supplied by:
135        * David Gero <dgero@nortelnetworks.com>
136        */
137       handle = NULL;
138     }
139   if (shl_findsym ((shl_t*) &handle, symbol_name, TYPE_UNDEFINED, &p) != 0 ||
140       handle == NULL || p == NULL)
141     {
142       /* the hp-docs say we should better abort() if errno==ENOSYM ;( */
143       g_module_set_error (g_strerror (errno));
144     }
145   
146   return p;
147 }
148
149 static gchar*
150 _g_module_build_path (const gchar *directory,
151                       const gchar *module_name)
152 {
153   if (directory && *directory)
154     if (strncmp (module_name, "lib", 3) == 0)
155       return g_strconcat (directory, "/", module_name, NULL);
156     else
157       return g_strconcat (directory, "/lib", module_name, ".sl", NULL);
158   else if (strncmp (module_name, "lib", 3) == 0)
159     return g_strdup (module_name);
160   else
161     return g_strconcat ("lib", module_name, ".sl", NULL);
162 }