gmodule.c 16.6 KB
Newer Older
Tim Janik's avatar
Tim Janik committed
1 2
/* GMODULE - GLIB wrapper code for dynamic module loading
 * Copyright (C) 1998 Tim Janik
Tim Janik's avatar
Tim Janik committed
3 4
 *
 * This library is free software; you can redistribute it and/or
5
 * modify it under the terms of the GNU Lesser General Public
Tim Janik's avatar
Tim Janik committed
6 7 8 9 10
 * 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
11
 * MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.	 See the GNU
12
 * Lesser General Public License for more details.
Tim Janik's avatar
Tim Janik committed
13
 *
14
 * You should have received a copy of the GNU Lesser General Public
Tim Janik's avatar
Tim Janik committed
15 16 17 18
 * License along with this library; if not, write to the
 * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
 * Boston, MA 02111-1307, USA.
 */
19

20
/*
21
 * Modified by the GLib Team and others 1997-2000.  See the AUTHORS
22 23 24 25 26
 * file for a list of people on the GLib Team.  See the ChangeLog
 * files for a list of changes.  These files are distributed with
 * GLib at ftp://ftp.gtk.org/pub/gtk/. 
 */

27 28 29 30
/* 
 * MT safe
 */

31
#include "config.h"
32 33

#include "glib.h"
34
#include "gmodule.h"
Matthias Clasen's avatar
Matthias Clasen committed
35 36 37 38 39 40

#include <errno.h>
#include <string.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <fcntl.h>
41
#ifdef HAVE_UNISTD_H
Matthias Clasen's avatar
Matthias Clasen committed
42
#include <unistd.h>
43
#endif
44
#ifdef G_OS_WIN32
Matthias Clasen's avatar
Matthias Clasen committed
45
#include <io.h>		/* For open() and close() prototypes. */
46
#endif
Tim Janik's avatar
Tim Janik committed
47

48 49
#include "gmoduleconf.h"
#include "gstdio.h"
Matthias Clasen's avatar
Matthias Clasen committed
50

Tim Janik's avatar
Tim Janik committed
51 52
/* We maintain a list of modules, so we can reference count them.
 * That's needed because some platforms don't support refernce counts on
53 54
 * modules e.g. the shl_* implementation of HP-UX
 * (http://www.stat.umn.edu/~luke/xls/projects/dlbasics/dlbasics.html).
Tim Janik's avatar
Tim Janik committed
55
 * Also, the module for the program itself is kept seperatedly for
56
 * faster access and because it has special semantics.
Tim Janik's avatar
Tim Janik committed
57 58 59 60 61 62 63
 */


/* --- structures --- */
struct _GModule
{
  gchar	*file_name;
Tor Lillqvist's avatar
Tor Lillqvist committed
64
#if defined (G_OS_WIN32) && !defined(_WIN64)
65 66
  gchar *cp_file_name;
#endif
Tim Janik's avatar
Tim Janik committed
67
  gpointer handle;
68 69
  guint ref_count : 31;
  guint is_resident : 1;
70
  GModuleUnload unload;
Tim Janik's avatar
Tim Janik committed
71 72 73 74 75 76
  GModule *next;
};


/* --- prototypes --- */
static gpointer		_g_module_open		(const gchar	*file_name,
77 78
						 gboolean	 bind_lazy,
						 gboolean	 bind_local);
79
static void		_g_module_close		(gpointer	 handle,
Tim Janik's avatar
Tim Janik committed
80 81
						 gboolean	 is_unref);
static gpointer		_g_module_self		(void);
82
static gpointer		_g_module_symbol	(gpointer	 handle,
Tim Janik's avatar
Tim Janik committed
83
						 const gchar	*symbol_name);
84 85
static gchar*		_g_module_build_path	(const gchar	*directory,
						 const gchar	*module_name);
Tim Janik's avatar
Tim Janik committed
86 87 88 89 90 91
static inline void	g_module_set_error	(const gchar	*error);
static inline GModule*	g_module_find_by_handle (gpointer	 handle);
static inline GModule*	g_module_find_by_name	(const gchar	*name);


/* --- variables --- */
92 93 94
static GModule	     *modules = NULL;
static GModule	     *main_module = NULL;
static GStaticPrivate module_error_private = G_STATIC_PRIVATE_INIT;
95 96
static gboolean	      module_debug_initialized = FALSE;
static guint	      module_debug_flags = 0;
Tim Janik's avatar
Tim Janik committed
97 98 99 100 101 102 103


/* --- inline functions --- */
static inline GModule*
g_module_find_by_handle (gpointer handle)
{
  GModule *module;
104
  GModule *retval = NULL;
105
  
106
  if (main_module && main_module->handle == handle)
107 108 109 110 111 112 113 114 115 116
    retval = main_module;
  else
    for (module = modules; module; module = module->next)
      if (handle == module->handle)
	{
	  retval = module;
	  break;
	}

  return retval;
Tim Janik's avatar
Tim Janik committed
117 118 119 120 121 122
}

static inline GModule*
g_module_find_by_name (const gchar *name)
{
  GModule *module;
123
  GModule *retval = NULL;
124
  
Tim Janik's avatar
Tim Janik committed
125 126
  for (module = modules; module; module = module->next)
    if (strcmp (name, module->file_name) == 0)
127 128 129 130 131 132
	{
	  retval = module;
	  break;
	}

  return retval;
Tim Janik's avatar
Tim Janik committed
133 134 135
}

static inline void
Darin Adler's avatar
Darin Adler committed
136
g_module_set_error_unduped (gchar *error)
Tim Janik's avatar
Tim Janik committed
137
{
138
  g_static_private_set (&module_error_private, error, g_free);
Tim Janik's avatar
Tim Janik committed
139 140 141
  errno = 0;
}

142 143 144 145 146 147
static inline void
g_module_set_error (const gchar *error)
{
  g_module_set_error_unduped (g_strdup (error));
}

Tim Janik's avatar
Tim Janik committed
148 149

/* --- include platform specifc code --- */
150
#define	SUPPORT_OR_RETURN(rv)	{ g_module_set_error (NULL); }
Tim Janik's avatar
Tim Janik committed
151 152 153 154
#if	(G_MODULE_IMPL == G_MODULE_IMPL_DL)
#include "gmodule-dl.c"
#elif	(G_MODULE_IMPL == G_MODULE_IMPL_DLD)
#include "gmodule-dld.c"
155 156
#elif	(G_MODULE_IMPL == G_MODULE_IMPL_WIN32)
#include "gmodule-win32.c"
157 158
#elif	(G_MODULE_IMPL == G_MODULE_IMPL_DYLD)
#include "gmodule-dyld.c"
159 160
#elif	(G_MODULE_IMPL == G_MODULE_IMPL_AR)
#include "gmodule-ar.c"
Tim Janik's avatar
Tim Janik committed
161
#else
162 163
#undef	SUPPORT_OR_RETURN
#define	SUPPORT_OR_RETURN(rv)	{ g_module_set_error ("dynamic modules are " \
164
                                              "not supported by this system"); return rv; }
165 166
static gpointer
_g_module_open (const gchar	*file_name,
167 168
		gboolean	 bind_lazy,
		gboolean	 bind_local)
169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187
{
  return NULL;
}
static void
_g_module_close	(gpointer	 handle,
		 gboolean	 is_unref)
{
}
static gpointer
_g_module_self (void)
{
  return NULL;
}
static gpointer
_g_module_symbol (gpointer	 handle,
		  const gchar	*symbol_name)
{
  return NULL;
}
188 189 190 191 192 193
static gchar*
_g_module_build_path (const gchar *directory,
		      const gchar *module_name)
{
  return NULL;
}
Tim Janik's avatar
Tim Janik committed
194 195 196 197 198 199
#endif	/* no implementation */

/* --- functions --- */
gboolean
g_module_supported (void)
{
200
  SUPPORT_OR_RETURN (FALSE);
201
  
Tim Janik's avatar
Tim Janik committed
202 203 204
  return TRUE;
}

205 206 207
static gchar*
parse_libtool_archive (const gchar* libtool_name)
{
Matthias Clasen's avatar
Matthias Clasen committed
208 209 210
  const guint TOKEN_DLNAME = G_TOKEN_LAST + 1;
  const guint TOKEN_INSTALLED = G_TOKEN_LAST + 2;
  const guint TOKEN_LIBDIR = G_TOKEN_LAST + 3;
211 212 213 214 215 216 217
  gchar *lt_dlname = NULL;
  gboolean lt_installed = TRUE;
  gchar *lt_libdir = NULL;
  gchar *name;
  GTokenType token;
  GScanner *scanner;
  
218
  int fd = g_open (libtool_name, O_RDONLY, 0);
219 220
  if (fd < 0)
    {
221 222 223
      gchar *display_libtool_name = g_filename_display_name (libtool_name);
      g_module_set_error_unduped (g_strdup_printf ("failed to open libtool archive \"%s\"", display_libtool_name));
      g_free (display_libtool_name);
224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246
      return NULL;
    }
  /* search libtool's dlname specification  */
  scanner = g_scanner_new (NULL);
  g_scanner_input_file (scanner, fd);
  scanner->config->symbol_2_token = TRUE;
  g_scanner_scope_add_symbol (scanner, 0, "dlname", 
			      GUINT_TO_POINTER (TOKEN_DLNAME));
  g_scanner_scope_add_symbol (scanner, 0, "installed", 
			      GUINT_TO_POINTER (TOKEN_INSTALLED));
  g_scanner_scope_add_symbol (scanner, 0, "libdir", 
			      GUINT_TO_POINTER (TOKEN_LIBDIR));
  while (!g_scanner_eof (scanner))
    {
      token = g_scanner_get_next_token (scanner);
      if (token == TOKEN_DLNAME || token == TOKEN_INSTALLED || 
	  token == TOKEN_LIBDIR)
	{
	  if (g_scanner_get_next_token (scanner) != '=' ||
	      g_scanner_get_next_token (scanner) != 
	      (token == TOKEN_INSTALLED ? 
	       G_TOKEN_IDENTIFIER : G_TOKEN_STRING))
	    {
247 248 249
	      gchar *display_libtool_name = g_filename_display_name (libtool_name);
	      g_module_set_error_unduped (g_strdup_printf ("unable to parse libtool archive \"%s\"", display_libtool_name));
	      g_free (display_libtool_name);
250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281

	      g_free (lt_dlname);
	      g_free (lt_libdir);
	      g_scanner_destroy (scanner);
	      close (fd);

	      return NULL;
	    }
	  else
	    {
	      if (token == TOKEN_DLNAME)
		{
		  g_free (lt_dlname);
		  lt_dlname = g_strdup (scanner->value.v_string);
		}
	      else if (token == TOKEN_INSTALLED)
		lt_installed = 
		  strcmp (scanner->value.v_identifier, "yes") == 0;
	      else /* token == TOKEN_LIBDIR */
		{
		  g_free (lt_libdir);
		  lt_libdir = g_strdup (scanner->value.v_string);
		}
	    }
	}      
    }

  if (!lt_installed)
    {
      gchar *dir = g_path_get_dirname (libtool_name);
      g_free (lt_libdir);
      lt_libdir = g_strconcat (dir, G_DIR_SEPARATOR_S ".libs", NULL);
Michael Meeks's avatar
Michael Meeks committed
282
      g_free (dir);
283 284
    }

285
  name = g_strconcat (lt_libdir, G_DIR_SEPARATOR_S, lt_dlname, NULL);
286 287 288 289 290 291 292 293 294 295
  
  g_free (lt_dlname);
  g_free (lt_libdir);
  g_scanner_destroy (scanner);
  close (fd);

  return name;
}

static inline gboolean
296 297
str_check_suffix (const gchar* string,
		  const gchar* suffix)
298
{
299 300
  gsize string_len = strlen (string);    
  gsize suffix_len = strlen (suffix);    
301 302 303 304 305

  return string_len >= suffix_len && 
    strcmp (string + string_len - suffix_len, suffix) == 0;
}

306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328
enum
{
  G_MODULE_DEBUG_RESIDENT_MODULES = 1 << 0,
  G_MODULE_DEBUG_BIND_NOW_MODULES = 1 << 1
};

static void
_g_module_debug_init (void)
{
  const GDebugKey keys[] = {
    { "resident-modules", G_MODULE_DEBUG_RESIDENT_MODULES },
    { "bind-now-modules", G_MODULE_DEBUG_BIND_NOW_MODULES }
  };
  const gchar *env;

  env = g_getenv ("G_DEBUG");

  module_debug_flags =
    !env ? 0 : g_parse_debug_string (env, keys, G_N_ELEMENTS (keys));

  module_debug_initialized = TRUE;
}

329 330
static GStaticRecMutex g_module_global_lock = G_STATIC_REC_MUTEX_INIT;

Tim Janik's avatar
Tim Janik committed
331 332 333 334 335
GModule*
g_module_open (const gchar    *file_name,
	       GModuleFlags    flags)
{
  GModule *module;
336 337
  gpointer handle = NULL;
  gchar *name = NULL;
338
  
339
  SUPPORT_OR_RETURN (NULL);
340
  
341
  g_static_rec_mutex_lock (&g_module_global_lock);
342 343 344 345 346 347 348

  if (G_UNLIKELY (!module_debug_initialized))
    _g_module_debug_init ();

  if (module_debug_flags & G_MODULE_DEBUG_BIND_NOW_MODULES)
    flags &= ~G_MODULE_BIND_LAZY;

Tim Janik's avatar
Tim Janik committed
349
  if (!file_name)
350
    {      
Tim Janik's avatar
Tim Janik committed
351 352 353 354 355 356 357
      if (!main_module)
	{
	  handle = _g_module_self ();
	  if (handle)
	    {
	      main_module = g_new (GModule, 1);
	      main_module->file_name = NULL;
Tor Lillqvist's avatar
Tor Lillqvist committed
358
#if defined (G_OS_WIN32) && !defined(_WIN64)
359 360
	      main_module->cp_file_name = NULL;
#endif
361
	      main_module->handle = handle;
Tim Janik's avatar
Tim Janik committed
362
	      main_module->ref_count = 1;
363
	      main_module->is_resident = TRUE;
364
	      main_module->unload = NULL;
Tim Janik's avatar
Tim Janik committed
365 366 367
	      main_module->next = NULL;
	    }
	}
368 369
      else
	main_module->ref_count++;
370

371
      g_static_rec_mutex_unlock (&g_module_global_lock);
Tim Janik's avatar
Tim Janik committed
372 373
      return main_module;
    }
374
  
Tim Janik's avatar
Tim Janik committed
375 376 377 378 379
  /* we first search the module list by name */
  module = g_module_find_by_name (file_name);
  if (module)
    {
      module->ref_count++;
380
      
381
      g_static_rec_mutex_unlock (&g_module_global_lock);
Tim Janik's avatar
Tim Janik committed
382 383
      return module;
    }
384

385
  /* check whether we have a readable file right away */
386
  if (g_file_test (file_name, G_FILE_TEST_IS_REGULAR))
387 388 389
    name = g_strdup (file_name);
  /* try completing file name with standard library suffix */
  if (!name)
390
    {
391
      name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL);
392
      if (!g_file_test (name, G_FILE_TEST_IS_REGULAR))
393 394
	{
	  g_free (name);
395
	  name = NULL;
396 397
	}
    }
398
  /* try completing by appending libtool suffix */
399
  if (!name)
400
    {
401
      name = g_strconcat (file_name, ".la", NULL);
402
      if (!g_file_test (name, G_FILE_TEST_IS_REGULAR))
403 404 405 406
	{
	  g_free (name);
	  name = NULL;
	}
407
    }
408 409 410 411
  /* we can't access() the file, lets hope the platform backends finds
   * it via library paths
   */
  if (!name)
412 413 414 415 416 417 418 419 420 421
    {
      gchar *dot = strrchr (file_name, '.');
      gchar *slash = strrchr (file_name, G_DIR_SEPARATOR);
      
      /* make sure the name has a suffix */
      if (!dot || dot < slash)
	name = g_strconcat (file_name, "." G_MODULE_SUFFIX, NULL);
      else
	name = g_strdup (file_name);
    }
422

423 424
  /* ok, try loading the module */
  if (name)
425
    {
426 427
      /* if it's a libtool archive, figure library file to load */
      if (str_check_suffix (name, ".la")) /* libtool archive? */
428
	{
429 430 431
	  gchar *real_name = parse_libtool_archive (name);

	  /* real_name might be NULL, but then module error is already set */
432 433 434 435 436
	  if (real_name)
	    {
	      g_free (name);
	      name = real_name;
            }
437
	}
438
      if (name)
439 440
	handle = _g_module_open (name, (flags & G_MODULE_BIND_LAZY) != 0,
			(flags & G_MODULE_BIND_LOCAL) != 0);
441
    }
442
  else
443 444 445 446 447
    {
      gchar *display_file_name = g_filename_display_name (file_name);
      g_module_set_error_unduped (g_strdup_printf ("unable to access file \"%s\"", display_file_name));
      g_free (display_file_name);
    }
448
  g_free (name);
449

Tim Janik's avatar
Tim Janik committed
450 451
  if (handle)
    {
452 453
      gchar *saved_error;
      GModuleCheckInit check_init;
454
      const gchar *check_failed = NULL;
455
      
Tim Janik's avatar
Tim Janik committed
456 457 458 459
      /* search the module list by handle, since file names are not unique */
      module = g_module_find_by_handle (handle);
      if (module)
	{
460
	  _g_module_close (module->handle, TRUE);
Tim Janik's avatar
Tim Janik committed
461 462
	  module->ref_count++;
	  g_module_set_error (NULL);
463
	  
464
	  g_static_rec_mutex_unlock (&g_module_global_lock);
Tim Janik's avatar
Tim Janik committed
465 466
	  return module;
	}
467
      
468
      saved_error = g_strdup (g_module_error ());
469
      g_module_set_error (NULL);
470
      
Tim Janik's avatar
Tim Janik committed
471 472
      module = g_new (GModule, 1);
      module->file_name = g_strdup (file_name);
Tor Lillqvist's avatar
Tor Lillqvist committed
473
#if defined (G_OS_WIN32) && !defined(_WIN64)
474 475 476
      module->cp_file_name = g_locale_from_utf8 (file_name, -1,
						 NULL, NULL, NULL);
#endif
Tim Janik's avatar
Tim Janik committed
477
      module->handle = handle;
478
      module->ref_count = 1;
479
      module->is_resident = FALSE;
480
      module->unload = NULL;
481 482
      module->next = modules;
      modules = module;
483
      
484
      /* check initialization */
485
      if (g_module_symbol (module, "g_module_check_init", (gpointer) &check_init) && check_init != NULL)
Tim Janik's avatar
Tim Janik committed
486
	check_failed = check_init (module);
487
      
488
      /* we don't call unload() if the initialization check failed. */
489
      if (!check_failed)
490
	g_module_symbol (module, "g_module_unload", (gpointer) &module->unload);
491
      
492 493
      if (check_failed)
	{
494 495
	  gchar *error;

496 497 498 499
	  error = g_strconcat ("GModule (", 
                               file_name ? file_name : "NULL", 
                               ") initialization check failed: ", 
                               check_failed, NULL);
500 501
	  g_module_close (module);
	  module = NULL;
502 503
	  g_module_set_error (error);
	  g_free (error);
504 505 506
	}
      else
	g_module_set_error (saved_error);
507

508
      g_free (saved_error);
Tim Janik's avatar
Tim Janik committed
509
    }
510

511 512 513 514
  if (module != NULL &&
      (module_debug_flags & G_MODULE_DEBUG_RESIDENT_MODULES))
    g_module_make_resident (module);

515
  g_static_rec_mutex_unlock (&g_module_global_lock);
516
  return module;
Tim Janik's avatar
Tim Janik committed
517 518
}

Tor Lillqvist's avatar
Tor Lillqvist committed
519
#if defined (G_OS_WIN32) && !defined(_WIN64)
520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536

#undef g_module_open

GModule*
g_module_open (const gchar    *file_name,
	       GModuleFlags    flags)
{
  gchar *utf8_file_name = g_locale_to_utf8 (file_name, -1, NULL, NULL, NULL);
  GModule *retval = g_module_open_utf8 (utf8_file_name, flags);

  g_free (utf8_file_name);

  return retval;
}

#endif

Tim Janik's avatar
Tim Janik committed
537
gboolean
538
g_module_close (GModule	       *module)
Tim Janik's avatar
Tim Janik committed
539
{
540
  SUPPORT_OR_RETURN (FALSE);
541
  
Tim Janik's avatar
Tim Janik committed
542 543
  g_return_val_if_fail (module != NULL, FALSE);
  g_return_val_if_fail (module->ref_count > 0, FALSE);
544
  
545 546
  g_static_rec_mutex_lock (&g_module_global_lock);

547
  module->ref_count--;
548
  
549
  if (!module->ref_count && !module->is_resident && module->unload)
550
    {
551
      GModuleUnload unload;
552

553 554 555
      unload = module->unload;
      module->unload = NULL;
      unload (module);
556 557 558
    }

  if (!module->ref_count && !module->is_resident)
Tim Janik's avatar
Tim Janik committed
559 560 561
    {
      GModule *last;
      GModule *node;
562
      
Tim Janik's avatar
Tim Janik committed
563
      last = NULL;
564
      
Tim Janik's avatar
Tim Janik committed
565 566 567 568 569 570 571 572 573 574 575 576 577 578
      node = modules;
      while (node)
	{
	  if (node == module)
	    {
	      if (last)
		last->next = node->next;
	      else
		modules = node->next;
	      break;
	    }
	  last = node;
	  node = last->next;
	}
579
      module->next = NULL;
580 581
      
      _g_module_close (module->handle, FALSE);
582
      g_free (module->file_name);
Tor Lillqvist's avatar
Tor Lillqvist committed
583
#if defined (G_OS_WIN32) && !defined(_WIN64)
584 585
      g_free (module->cp_file_name);
#endif
Tim Janik's avatar
Tim Janik committed
586 587
      g_free (module);
    }
588
  
589
  g_static_rec_mutex_unlock (&g_module_global_lock);
590
  return g_module_error() == NULL;
Tim Janik's avatar
Tim Janik committed
591 592
}

593 594 595 596 597 598 599 600
void
g_module_make_resident (GModule *module)
{
  g_return_if_fail (module != NULL);

  module->is_resident = TRUE;
}

Havoc Pennington's avatar
Havoc Pennington committed
601
G_CONST_RETURN gchar*
Tim Janik's avatar
Tim Janik committed
602 603
g_module_error (void)
{
604
  return g_static_private_get (&module_error_private);
Tim Janik's avatar
Tim Janik committed
605 606 607
}

gboolean
608 609
g_module_symbol (GModule	*module,
		 const gchar	*symbol_name,
610
		 gpointer	*symbol)
Tim Janik's avatar
Tim Janik committed
611
{
Havoc Pennington's avatar
Havoc Pennington committed
612
  const gchar *module_error;
613

Tim Janik's avatar
Tim Janik committed
614 615
  if (symbol)
    *symbol = NULL;
616
  SUPPORT_OR_RETURN (FALSE);
617
  
Tim Janik's avatar
Tim Janik committed
618 619 620
  g_return_val_if_fail (module != NULL, FALSE);
  g_return_val_if_fail (symbol_name != NULL, FALSE);
  g_return_val_if_fail (symbol != NULL, FALSE);
621
  
622 623
  g_static_rec_mutex_lock (&g_module_global_lock);

Tim Janik's avatar
Tim Janik committed
624
#ifdef	G_MODULE_NEED_USCORE
625 626 627 628 629 630 631
  {
    gchar *name;

    name = g_strconcat ("_", symbol_name, NULL);
    *symbol = _g_module_symbol (module->handle, name);
    g_free (name);
  }
Tim Janik's avatar
Tim Janik committed
632
#else	/* !G_MODULE_NEED_USCORE */
633
  *symbol = _g_module_symbol (module->handle, symbol_name);
Tim Janik's avatar
Tim Janik committed
634
#endif	/* !G_MODULE_NEED_USCORE */
635
  
Tim Janik's avatar
Tim Janik committed
636
  module_error = g_module_error ();
637
  if (module_error)
Tim Janik's avatar
Tim Janik committed
638
    {
639 640 641 642 643
      gchar *error;

      error = g_strconcat ("`", symbol_name, "': ", module_error, NULL);
      g_module_set_error (error);
      g_free (error);
Tim Janik's avatar
Tim Janik committed
644 645
      *symbol = NULL;
    }
646
  
647 648
  g_static_rec_mutex_unlock (&g_module_global_lock);
  return !module_error;
Tim Janik's avatar
Tim Janik committed
649 650
}

Havoc Pennington's avatar
Havoc Pennington committed
651
G_CONST_RETURN gchar*
Tim Janik's avatar
Tim Janik committed
652 653 654
g_module_name (GModule *module)
{
  g_return_val_if_fail (module != NULL, NULL);
655
  
Tim Janik's avatar
Tim Janik committed
656 657
  if (module == main_module)
    return "main";
658
  
Tim Janik's avatar
Tim Janik committed
659 660
  return module->file_name;
}
661

Tor Lillqvist's avatar
Tor Lillqvist committed
662
#if defined (G_OS_WIN32) && !defined(_WIN64)
663 664 665 666 667 668 669 670 671 672 673 674 675 676 677 678

#undef g_module_name

G_CONST_RETURN gchar*
g_module_name (GModule *module)
{
  g_return_val_if_fail (module != NULL, NULL);
  
  if (module == main_module)
    return "main";
  
  return module->cp_file_name;
}

#endif

679 680 681 682 683 684 685 686
gchar*
g_module_build_path (const gchar *directory,
		     const gchar *module_name)
{
  g_return_val_if_fail (module_name != NULL, NULL);
  
  return _g_module_build_path (directory, module_name);
}