initial import of gmodule.
[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 #ifndef DYNAMIC_PATH
26 #define DYNAMIC_PATH    0
27 #endif  /* DYNAMIC_PATH */
28
29 /* should we have BIND_TOGETHER here as well? */
30 #define CONST_BIND_FLAGS        (BIND_NONFATAL | BIND_VERBOSE)
31
32
33 /* --- functions --- */
34 static gpointer
35 _g_module_open (const gchar    *file_name,
36                 gboolean        bind_lazy)
37 {
38   gpointer handle;
39
40   handle = shl_load (file_name, CONST_BIND_FLAGS | (bind_lazy ? BIND_DEFERRED : BIND_IMMEDIATE), 0);
41   if (!handle)
42     g_module_set_error (g_strerror (errno));
43
44   return handle;
45 }
46
47 static gpointer
48 _g_module_self (void)
49 {
50   gpointer handle;
51
52   handle = PROG_HANDLE;
53   if (!handle)
54     g_module_set_error (g_strerror (errno));
55
56   return handle;
57 }
58
59 static void
60 _g_module_close (gpointer        *handle_p,
61                  gboolean         is_unref)
62 {
63   if (!is_unref)
64     {
65       if (shl_unload (*handle_p) != 0)
66         g_module_set_error (g_strerror (errno));
67     }
68 }
69
70 static gpointer
71 _g_module_symbol (gpointer       *handle_p,
72                   const gchar    *symbol_name)
73 {
74   gpointer p = NULL;
75
76   /* should we restrict lookups to TYPE_PROCEDURE?
77    */
78   if (shl_findsym (handle_p, symbol_name, TYPE_UNDEFINED, &p) != 0 || p == NULL)
79     g_module_set_error (g_strerror (errno));
80   
81   return p;
82 }