minor changes to internal interface.
[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 #include <dl.h>
20
21
22 /* some flags are missing on some systems, so we provide
23  * harmless defaults.
24  *
25  * Mandatory:
26  * BIND_IMMEDIATE  - Resolve symbol references when the library is loaded.
27  * BIND_DEFERRED   - Delay code symbol resolution until actual reference.
28  *
29  * Optionally:
30  * BIND_FIRST      - Place the library at the head of the symbol search order.
31  * BIND_NONFATAL   - The default BIND_IMMEDIATE behavior is to treat all unsatisfied
32  *                   symbols as fatal.  This flag allows binding of unsatisfied code
33  *                   symbols to be deferred until use.
34  *                   [Perl: For certain libraries, like DCE, deferred binding often
35  *                   causes run time problems.  Adding BIND_NONFATAL to BIND_IMMEDIATE
36  *                   still allows unresolved references in situations like this.]
37  * BIND_NOSTART    - Do not call the initializer for the shared library when the
38  *                   library is loaded, nor on a future call to shl_unload().
39  * BIND_VERBOSE    - Print verbose messages concerning possible unsatisfied symbols.
40  *
41  * hp9000s700/hp9000s800:
42  * BIND_RESTRICTED - Restrict symbols visible by the library to those present at
43  *                   library load time.
44  * DYNAMIC_PATH    - Allow the loader to dynamically search for the library specified
45  *                   by the path argument.
46  */
47 #ifndef DYNAMIC_PATH
48 #define DYNAMIC_PATH    0
49 #endif  /* DYNAMIC_PATH */
50 #ifndef BIND_RESTRICTED
51 #define BIND_RESTRICTED 0
52 #endif  /* BIND_RESTRICTED */
53
54 #define OPT_BIND_FLAGS  (BIND_NONFATAL | BIND_VERBOSE)
55
56
57 /* --- functions --- */
58 static gpointer
59 _g_module_open (const gchar    *file_name,
60                 gboolean        bind_lazy)
61 {
62   shl_t shl_handle;
63   
64   shl_handle = shl_load (file_name,
65                          (bind_lazy ? BIND_DEFERRED : BIND_IMMEDIATE) | OPT_BIND_FLAGS, 0);
66   if (!shl_handle)
67     {
68       /* the hp-docs say we should better abort() if errno==ENOSYM ;( */
69       g_module_set_error (g_strerror (errno));
70     }
71   
72   return (gpointer) shl_handle;
73 }
74
75 static gpointer
76 _g_module_self (void)
77 {
78   shl_t shl_handle;
79   
80   shl_handle = PROG_HANDLE;
81   if (!shl_handle)
82     g_module_set_error (g_strerror (errno));
83   
84   return shl_handle;
85 }
86
87 static void
88 _g_module_close (gpointer         handle,
89                  gboolean         is_unref)
90 {
91   if (!is_unref)
92     {
93       if (shl_unload ((shl_t) handle) != 0)
94         g_module_set_error (g_strerror (errno));
95     }
96 }
97
98 static gpointer
99 _g_module_symbol (gpointer        handle,
100                   const gchar    *symbol_name)
101 {
102   gpointer p = NULL;
103   
104   /* should we restrict lookups to TYPE_PROCEDURE?
105    */
106   if (shl_findsym ((shl_t*) &handle, symbol_name, TYPE_UNDEFINED, &p) != 0 ||
107       handle == NULL || p == NULL)
108     {
109       /* the hp-docs say we should better abort() if errno==ENOSYM ;( */
110       g_module_set_error (g_strerror (errno));
111     }
112   
113   return p;
114 }