1 /* GMODULE - GLIB wrapper code for dynamic module loading
2 * Copyright (C) 1998, 2000 Tim Janik
4 * dyld (Darwin) GMODULE implementation
5 * Copyright (C) 2001 Dan Winship
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.
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.
17 * You should have received a copy of the GNU Lesser General Public
18 * License along with this library; if not, see <http://www.gnu.org/licenses/>.
22 #include <mach-o/dyld.h>
24 static gpointer self_module
= GINT_TO_POINTER (1);
27 _g_module_open (const gchar
*file_name
,
31 NSObjectFileImage image
;
32 NSObjectFileImageReturnCode ret
;
34 unsigned long options
;
37 ret
= NSCreateObjectFileImageFromFile (file_name
, &image
);
38 if (ret
!= NSObjectFileImageSuccess
)
42 case NSObjectFileImageInappropriateFile
:
43 case NSObjectFileImageFormat
:
44 msg
= g_strdup_printf ("%s is not a loadable module", file_name
);
47 case NSObjectFileImageArch
:
48 msg
= g_strdup_printf ("%s is not built for this architecture",
52 case NSObjectFileImageAccess
:
53 if (access (file_name
, F_OK
) == 0)
54 msg
= g_strdup_printf ("%s: permission denied", file_name
);
56 msg
= g_strdup_printf ("%s: no such file or directory", file_name
);
60 msg
= g_strdup_printf ("unknown error for %s", file_name
);
64 g_module_set_error (msg
);
69 options
= NSLINKMODULE_OPTION_RETURN_ON_ERROR
;
71 options
|= NSLINKMODULE_OPTION_PRIVATE
;
73 options
|= NSLINKMODULE_OPTION_BINDNOW
;
74 module
= NSLinkModule (image
, file_name
, options
);
75 NSDestroyObjectFileImage (image
);
80 const char *file
, *error
;
82 NSLinkEditError (&c
, &error_number
, &file
, &error
);
83 msg
= g_strdup_printf ("could not link %s: %s", file_name
, error
);
84 g_module_set_error (msg
);
99 _g_module_close (gpointer handle
,
102 if (handle
== &self_module
)
105 if (!NSUnLinkModule (handle
, 0))
106 g_module_set_error ("could not unlink module");
110 _g_module_symbol (gpointer handle
,
111 const gchar
*symbol_name
)
116 if (handle
== &self_module
)
118 if (NSIsSymbolNameDefined (symbol_name
))
119 sym
= NSLookupAndBindSymbol (symbol_name
);
124 sym
= NSLookupSymbolInModule (handle
, symbol_name
);
128 msg
= g_strdup_printf ("no such symbol %s", symbol_name
);
129 g_module_set_error (msg
);
134 return NSAddressOfSymbol (sym
);
138 _g_module_build_path (const gchar
*directory
,
139 const gchar
*module_name
)
141 if (directory
&& *directory
)
143 if (strncmp (module_name
, "lib", 3) == 0)
144 return g_strconcat (directory
, "/", module_name
, NULL
);
146 return g_strconcat (directory
, "/lib", module_name
, "." G_MODULE_SUFFIX
, NULL
);
148 else if (strncmp (module_name
, "lib", 3) == 0)
149 return g_strdup (module_name
);
151 return g_strconcat ("lib", module_name
, "." G_MODULE_SUFFIX
, NULL
);