Port to BeOS by myself and Richard Offer.
[platform/upstream/glib.git] / gmodule / gmodule-beos.c
1 /* GMODULE - GLIB wrapper code for dynamic module loading
2  * Copyright (C) 1998 Tim Janik  
3  *
4  * BeOS GMODULE implementation
5  * Copyright (C) 1999 Richard Offer and Shawn T. Amundson (amundson@gtk.org)
6  *
7  * This library is free software; you can redistribute it and/or
8  * modify it under the terms of the GNU Library General Public
9  * License as published by the Free Software Foundation; either
10  * version 2 of the License, or (at your option) any later version.
11  *
12  * This library is distributed in the hope that it will be useful,
13  * but WITHOUT ANY WARRANTY; without even the implied warranty of
14  * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the GNU
15  * Library General Public License for more details.
16  *
17  * You should have received a copy of the GNU Library General Public
18  * License along with this library; if not, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 /* 
24  * MT safe
25  */
26
27 #include <be/kernel/image.h> /* image (aka DSO) handling functions... */
28
29 /*
30  * The BeOS doesn't use the same symantics as Unix's dlopen....
31  * 
32  */
33 #ifndef RTLD_GLOBAL
34 #define RTLD_GLOBAL     0
35 #endif  /* RTLD_GLOBAL */
36 #ifndef RTLD_LAZY
37 #define RTLD_LAZY       1
38 #endif  /* RTLD_LAZY */
39 #ifndef RTLD_NOW
40 #define RTLD_NOW        0
41 #endif  /* RTLD_NOW */
42
43
44 /*
45  * Points to Ponder
46  * 
47  * You can load the same DSO more than once, in which case you'll have 
48  * different image_id's. While this means that we don't have to worry about 
49  * reference counts, it could lead to problems in the future....
50  * richard.
51  */
52
53 #include <Errors.h>
54 #include <stdio.h>
55
56 /* --- functions --- */
57 static gpointer
58 _g_module_open (const gchar    *file_name,
59                 gboolean        bind_lazy)
60 {
61   image_id handle;
62   
63   handle = load_add_on (file_name);
64   if (handle < B_OK) {
65     g_module_set_error (g_strdup_printf("failed to load_add_on(%s), reason: %s", 
66                         (gchar *) file_name, strerror(handle)));
67     return NULL;
68   }
69
70   return (gpointer) handle;
71 }
72
73 static gpointer
74 _g_module_self (void)
75 {
76   image_info info;
77   int32 cookie = 0;
78   status_t status;
79
80   /* Is it always the first one?  I'm guessing yes. */
81   if ((status = get_next_image_info(0, &cookie, &info)) == B_OK)
82     return (gpointer) info.id;
83   else
84     {
85       g_module_set_error (g_strdup_printf("get_next_image_info() for self failed, reason: %s", strerror(status)));
86       return NULL;
87     }
88 }
89
90 static void
91 _g_module_close (gpointer         handle,
92                  gboolean         is_unref)
93 {
94    image_info info;
95    gchar *name;
96
97    if (unload_add_on((image_id) handle) != B_OK)
98      {
99        /* Try and get the name of the image. */
100        if (get_image_info((image_id) handle, &info) != B_OK)
101          name = g_strdup("(unknown)");
102        else
103          name = g_strdup (info.name);
104
105        g_module_set_error (g_strdup_printf("failed to unload_add_on(%s)", 
106                            name));
107        g_free (name);
108      }
109 }
110
111 static gpointer
112 _g_module_symbol (gpointer        handle,
113                   const gchar    *symbol_name)
114 {
115   image_id id;
116   gpointer p;
117   status_t status;
118   image_info info;
119   gchar name[256];
120   int32 name_len;
121   int32 type;
122   int32 n;
123
124   id = (image_id) handle;
125
126   if ((status = get_image_info(id, &info)) != B_OK)
127     {
128       g_module_set_error (g_strdup_printf("failed get_image_info(), reason: %s", strerror(status)));
129       return NULL;
130     }
131
132   name_len = 256;
133   type = B_SYMBOL_TYPE_ANY;
134   n = 0;
135   while ((status = get_nth_image_symbol(id, n, name, &name_len, &type, (void **)&p)) == B_OK)
136     {
137       if (!strncmp (name, symbol_name, strlen(symbol_name)))
138         {
139           return p;
140         }
141
142       if (!strcmp (name, "_end"))
143         {
144           g_module_set_error (g_strdup_printf("g_module_symbol(): no symbol matching '%s'", symbol_name));
145           return NULL;
146         }
147
148       name_len = 256;
149       type = B_SYMBOL_TYPE_ANY;
150       n++;
151     }
152
153   g_module_set_error (g_strdup_printf("failed get_image_symbol(%s), reason: %s", symbol_name, strerror(status)));
154   return NULL;
155 }
156
157 static gchar*
158 _g_module_build_path (const gchar *directory,
159                       const gchar *module_name)
160 {
161   printf("WARNING: _g_module_build_path() untested!\n");
162   if (directory && *directory) {
163     if (strncmp (module_name, "lib", 3) == 0)
164       return g_strconcat (directory, "/", module_name, NULL);
165     else
166       return g_strconcat (directory, "/lib", module_name, ".so", NULL);
167   } else if (strncmp (module_name, "lib", 3) == 0)
168     return g_strdup (module_name);
169   else
170     return g_strconcat ("lib", module_name, ".so", NULL);
171 }