17d6e010e9aff50414944190bae8aaf6d25e612f
[platform/upstream/glib.git] / gmodule / gmodule-dld.c
1 /* GMODULE - GLIB wrapper code for dynamic module loading
2  * Copyright (C) 1998 Tim Janik
3  *
4  * This library is free software; you can redistribute it and/or
5  * modify it under the terms of the GNU Library 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  * Library General Public License for more details.
13  *
14  * You should have received a copy of the GNU Library 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  * MT safe
22  */
23
24 #include <dl.h>
25
26
27 /* some flags are missing on some systems, so we provide
28  * harmless defaults.
29  *
30  * Mandatory:
31  * BIND_IMMEDIATE  - Resolve symbol references when the library is loaded.
32  * BIND_DEFERRED   - Delay code symbol resolution until actual reference.
33  *
34  * Optionally:
35  * BIND_FIRST      - Place the library at the head of the symbol search order.
36  * BIND_NONFATAL   - The default BIND_IMMEDIATE behavior is to treat all unsatisfied
37  *                   symbols as fatal.  This flag allows binding of unsatisfied code
38  *                   symbols to be deferred until use.
39  *                   [Perl: For certain libraries, like DCE, deferred binding often
40  *                   causes run time problems.  Adding BIND_NONFATAL to BIND_IMMEDIATE
41  *                   still allows unresolved references in situations like this.]
42  * BIND_NOSTART    - Do not call the initializer for the shared library when the
43  *                   library is loaded, nor on a future call to shl_unload().
44  * BIND_VERBOSE    - Print verbose messages concerning possible unsatisfied symbols.
45  *
46  * hp9000s700/hp9000s800:
47  * BIND_RESTRICTED - Restrict symbols visible by the library to those present at
48  *                   library load time.
49  * DYNAMIC_PATH    - Allow the loader to dynamically search for the library specified
50  *                   by the path argument.
51  */
52 #ifndef DYNAMIC_PATH
53 #define DYNAMIC_PATH    0
54 #endif  /* DYNAMIC_PATH */
55 #ifndef BIND_RESTRICTED
56 #define BIND_RESTRICTED 0
57 #endif  /* BIND_RESTRICTED */
58
59 #define OPT_BIND_FLAGS  (BIND_NONFATAL | BIND_VERBOSE)
60
61
62 /* --- functions --- */
63 static gpointer
64 _g_module_open (const gchar    *file_name,
65                 gboolean        bind_lazy)
66 {
67   shl_t shl_handle;
68   
69   shl_handle = shl_load (file_name,
70                          (bind_lazy ? BIND_DEFERRED : BIND_IMMEDIATE) | OPT_BIND_FLAGS, 0);
71   if (!shl_handle)
72     {
73       /* the hp-docs say we should better abort() if errno==ENOSYM ;( */
74       g_module_set_error (g_strerror (errno));
75     }
76   
77   return (gpointer) shl_handle;
78 }
79
80 static gpointer
81 _g_module_self (void)
82 {
83   shl_t shl_handle;
84   
85   shl_handle = PROG_HANDLE;
86   if (!shl_handle)
87     g_module_set_error (g_strerror (errno));
88   
89   return shl_handle;
90 }
91
92 static void
93 _g_module_close (gpointer         handle,
94                  gboolean         is_unref)
95 {
96   if (!is_unref)
97     {
98       if (shl_unload ((shl_t) handle) != 0)
99         g_module_set_error (g_strerror (errno));
100     }
101 }
102
103 static gpointer
104 _g_module_symbol (gpointer        handle,
105                   const gchar    *symbol_name)
106 {
107   gpointer p = NULL;
108   
109   /* should we restrict lookups to TYPE_PROCEDURE?
110    */
111   if (shl_findsym ((shl_t*) &handle, symbol_name, TYPE_UNDEFINED, &p) != 0 ||
112       handle == NULL || p == NULL)
113     {
114       /* the hp-docs say we should better abort() if errno==ENOSYM ;( */
115       g_module_set_error (g_strerror (errno));
116     }
117   
118   return p;
119 }
120
121 static gchar*
122 _g_module_build_path (const gchar *directory,
123                       const gchar *module_name)
124 {
125   if (directory && *directory)
126     if (strncmp (module_name, "lib", 3) == 0)
127       return g_strconcat (directory, "/", module_name, NULL);
128     else
129       return g_strconcat (directory, "/lib", module_name, ".sl", NULL);
130   else if (strncmp (module_name, "lib", 3) == 0)
131     return g_strdup (module_name);
132   else
133     return g_strconcat ("lib", module_name, ".sl", NULL);
134 }