summaryrefslogtreecommitdiff
path: root/gmodule/gmodule-beos.c
blob: 159bc985a0f789fc87999c6b9449d650c34a1bf6 (plain)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* GMODULE - GLIB wrapper code for dynamic module loading
 * Copyright (C) 1998, 2000 Tim Janik  
 *
 * BeOS GMODULE implementation
 * Copyright (C) 1999 Richard Offer and Shawn T. Amundson (amundson@gtk.org)
 *
 * This library is free software; you can redistribute it and/or
 * modify it under the terms of the GNU Lesser General Public
 * License as published by the Free Software Foundation; either
 * version 2 of the License, or (at your option) any later version.
 *
 * This library is distributed in the hope that it will be useful,
 * but WITHOUT ANY WARRANTY; without even the implied warranty of
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
 * Lesser General Public License for more details.
 *
 * You should have received a copy of the GNU Lesser General Public
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */

/* 
 * MT safe
 */

#include <be/kernel/image.h> /* image (aka DSO) handling functions... */

/*
 * The BeOS doesn't use the same symantics as Unix's dlopen....
 * 
 */
#ifndef	RTLD_GLOBAL
#define	RTLD_GLOBAL	0
#endif	/* RTLD_GLOBAL */
#ifndef	RTLD_LAZY
#define	RTLD_LAZY	1
#endif	/* RTLD_LAZY */
#ifndef	RTLD_NOW
#define	RTLD_NOW	0
#endif	/* RTLD_NOW */


/*
 * Points to Ponder
 * 
 * You can load the same DSO more than once, in which case you'll have 
 * different image_id's. While this means that we don't have to worry about 
 * reference counts, it could lead to problems in the future....
 * richard.
 *
 * load_add_on() apparently does not support lazy or local binding.  Need
 * to confirm that the actual behavior is non-lazy/local.  --ds
 */

#include <Errors.h>
#include <stdio.h>

/* --- functions --- */
static gpointer
_g_module_open (const gchar *file_name,
		gboolean     bind_lazy,
		gboolean     bind_local)
{
  image_id handle;
  
  handle = load_add_on (file_name);
  if (handle < B_OK)
    {
      gchar *msg = g_strdup_printf ("failed to load_add_on(%s): %s",
				    file_name,
				    strerror (handle));
      
      g_module_set_error (msg);
      g_free (msg);
      
      return NULL;
    }
  
  return (gpointer) handle;
}

static gpointer
_g_module_self (void)
{
  image_info info;
  int32 cookie = 0;
  status_t status;
  
  /* Is it always the first one?  I'm guessing yes. */
  status = get_next_image_info (0, &cookie, &info);
  if (status == B_OK)
    return (gpointer) info.id;
  else
    {
      gchar *msg = g_strdup_printf ("failed to get_next_image_info(self): %s",
				    strerror (status));
      
      g_module_set_error (msg);
      g_free (msg);
      
      return NULL;
    }
}

static void
_g_module_close (gpointer handle,
		 gboolean is_unref)
{
  image_info info;
  gchar *name;
  
  if (unload_add_on ((image_id) handle) != B_OK)
    {
      gchar *msg;
      
      /* Try and get the name of the image. */
      if (get_image_info ((image_id) handle, &info) != B_OK)
	name = g_strdup ("unknown");
      else
	name = g_strdup (info.name);
      
      msg = g_strdup_printf ("failed to unload_add_on(%s): %s", name, strerror (status));
      g_module_set_error (msg);
      g_free (msg);
      g_free (name);
    }
}

static gpointer
_g_module_symbol (gpointer     handle,
		  const gchar *symbol_name)
{
  image_id id;
  status_t status;
  image_info info;
  int32 type, name_len;
  void *p;
  gchar *msg, name[256];
  gint n, l;
  
  id = (image_id) handle;
  
  status = get_image_info (id, &info);
  if (status != B_OK)
    {
      msg = g_strdup_printf ("failed to get_image_info(): %s", strerror (status));
      g_module_set_error (msg);
      g_free (msg);
      
      return NULL;
    }
  
  l = strlen (symbol_name);
  name_len = 256;
  type = B_SYMBOL_TYPE_ANY;
  n = 0;
  status = get_nth_image_symbol (id, n, name, &name_len, &type, &p);
  while (status == B_OK)
    {
      if (p && strncmp (name, symbol_name, l) == 0)
	return p;
      
      if (strcmp (name, "_end") == 0)
        {
	  msg = g_strdup_printf ("unmatched symbol name `%s'", symbol_name);
          g_module_set_error (msg);
	  g_free (msg);
	  
	  return NULL;
        }
      
      name_len = 256;
      type = B_SYMBOL_TYPE_ANY;
      n++;
      status = get_nth_image_symbol (id, n, name, &name_len, &type, &p);
    }
  
  msg = g_strdup_printf ("failed to get_image_symbol(%s): %s", symbol_name, strerror (status));
  g_module_set_error (msg);
  g_free (msg);
  
  return NULL;
}

static gchar*
_g_module_build_path (const gchar *directory,
		      const gchar *module_name)
{
  g_warning ("_g_module_build_path() untested for BeOS!");
  
  if (directory && *directory)
    {
      if (strncmp (module_name, "lib", 3) == 0)
	return g_strconcat (directory, "/", module_name, NULL);
      else
	return g_strconcat (directory, "/lib", module_name, "." G_MODULE_SUFFIX, NULL);
    }
  else if (strncmp (module_name, "lib", 3) == 0)
    return g_strdup (module_name);
  else
    return g_strconcat ("lib", module_name, "." G_MODULE_SUFFIX, NULL);
}