Bump version
[platform/upstream/glib.git] / gmodule / gmodule-beos.c
1 /* GMODULE - GLIB wrapper code for dynamic module loading
2  * Copyright (C) 1998, 2000 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 Lesser 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  * Lesser General Public License for more details.
16  *
17  * You should have received a copy of the GNU Lesser 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  * load_add_on() apparently does not support lazy or local binding.  Need
53  * to confirm that the actual behavior is non-lazy/local.  --ds
54  */
55
56 #include <Errors.h>
57 #include <stdio.h>
58
59 /* --- functions --- */
60 static gpointer
61 _g_module_open (const gchar *file_name,
62                 gboolean     bind_lazy,
63                 gboolean     bind_local)
64 {
65   image_id handle;
66   
67   handle = load_add_on (file_name);
68   if (handle < B_OK)
69     {
70       gchar *msg = g_strdup_printf ("failed to load_add_on(%s): %s",
71                                     file_name,
72                                     strerror (handle));
73       
74       g_module_set_error (msg);
75       g_free (msg);
76       
77       return NULL;
78     }
79   
80   return (gpointer) handle;
81 }
82
83 static gpointer
84 _g_module_self (void)
85 {
86   image_info info;
87   int32 cookie = 0;
88   status_t status;
89   
90   /* Is it always the first one?  I'm guessing yes. */
91   status = get_next_image_info (0, &cookie, &info);
92   if (status == B_OK)
93     return (gpointer) info.id;
94   else
95     {
96       gchar *msg = g_strdup_printf ("failed to get_next_image_info(self): %s",
97                                     strerror (status));
98       
99       g_module_set_error (msg);
100       g_free (msg);
101       
102       return NULL;
103     }
104 }
105
106 static void
107 _g_module_close (gpointer handle,
108                  gboolean is_unref)
109 {
110   image_info info;
111   gchar *name;
112   
113   if (unload_add_on ((image_id) handle) != B_OK)
114     {
115       gchar *msg;
116       
117       /* Try and get the name of the image. */
118       if (get_image_info ((image_id) handle, &info) != B_OK)
119         name = g_strdup ("unknown");
120       else
121         name = g_strdup (info.name);
122       
123       msg = g_strdup_printf ("failed to unload_add_on(%s): %s", name, strerror (status));
124       g_module_set_error (msg);
125       g_free (msg);
126       g_free (name);
127     }
128 }
129
130 static gpointer
131 _g_module_symbol (gpointer     handle,
132                   const gchar *symbol_name)
133 {
134   image_id id;
135   status_t status;
136   image_info info;
137   int32 type, name_len;
138   void *p;
139   gchar *msg, name[256];
140   gint n, l;
141   
142   id = (image_id) handle;
143   
144   status = get_image_info (id, &info);
145   if (status != B_OK)
146     {
147       msg = g_strdup_printf ("failed to get_image_info(): %s", strerror (status));
148       g_module_set_error (msg);
149       g_free (msg);
150       
151       return NULL;
152     }
153   
154   l = strlen (symbol_name);
155   name_len = 256;
156   type = B_SYMBOL_TYPE_ANY;
157   n = 0;
158   status = get_nth_image_symbol (id, n, name, &name_len, &type, &p);
159   while (status == B_OK)
160     {
161       if (p && strncmp (name, symbol_name, l) == 0)
162         return p;
163       
164       if (strcmp (name, "_end") == 0)
165         {
166           msg = g_strdup_printf ("unmatched symbol name `%s'", symbol_name);
167           g_module_set_error (msg);
168           g_free (msg);
169           
170           return NULL;
171         }
172       
173       name_len = 256;
174       type = B_SYMBOL_TYPE_ANY;
175       n++;
176       status = get_nth_image_symbol (id, n, name, &name_len, &type, &p);
177     }
178   
179   msg = g_strdup_printf ("failed to get_image_symbol(%s): %s", symbol_name, strerror (status));
180   g_module_set_error (msg);
181   g_free (msg);
182   
183   return NULL;
184 }
185
186 static gchar*
187 _g_module_build_path (const gchar *directory,
188                       const gchar *module_name)
189 {
190   g_warning ("_g_module_build_path() untested for BeOS!");
191   
192   if (directory && *directory)
193     {
194       if (strncmp (module_name, "lib", 3) == 0)
195         return g_strconcat (directory, "/", module_name, NULL);
196       else
197         return g_strconcat (directory, "/lib", module_name, "." G_MODULE_SUFFIX, NULL);
198     }
199   else if (strncmp (module_name, "lib", 3) == 0)
200     return g_strdup (module_name);
201   else
202     return g_strconcat ("lib", module_name, "." G_MODULE_SUFFIX, NULL);
203 }