tests: Don’t use a potentially-existent timezone in a test for failure
[glib.git] / gmodule / gmodule-dyld.c
blob764ac83e6612ddf53ebec0e9ba6c00e5b2d4c458
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.1 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/>.
20 #include "config.h"
22 #include <mach-o/dyld.h>
24 static gpointer self_module = GINT_TO_POINTER (1);
26 static gpointer
27 _g_module_open (const gchar *file_name,
28 gboolean bind_lazy,
29 gboolean bind_local)
31 NSObjectFileImage image;
32 NSObjectFileImageReturnCode ret;
33 NSModule module;
34 unsigned long options;
35 char *msg;
37 ret = NSCreateObjectFileImageFromFile (file_name, &image);
38 if (ret != NSObjectFileImageSuccess)
40 switch (ret)
42 case NSObjectFileImageInappropriateFile:
43 case NSObjectFileImageFormat:
44 msg = g_strdup_printf ("%s is not a loadable module", file_name);
45 break;
47 case NSObjectFileImageArch:
48 msg = g_strdup_printf ("%s is not built for this architecture",
49 file_name);
50 break;
52 case NSObjectFileImageAccess:
53 if (access (file_name, F_OK) == 0)
54 msg = g_strdup_printf ("%s: permission denied", file_name);
55 else
56 msg = g_strdup_printf ("%s: no such file or directory", file_name);
57 break;
59 default:
60 msg = g_strdup_printf ("unknown error for %s", file_name);
61 break;
64 g_module_set_error (msg);
65 g_free (msg);
66 return NULL;
69 options = NSLINKMODULE_OPTION_RETURN_ON_ERROR;
70 if (bind_local)
71 options |= NSLINKMODULE_OPTION_PRIVATE;
72 if (!bind_lazy)
73 options |= NSLINKMODULE_OPTION_BINDNOW;
74 module = NSLinkModule (image, file_name, options);
75 NSDestroyObjectFileImage (image);
76 if (!module)
78 NSLinkEditErrors c;
79 int error_number;
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);
85 g_free (msg);
86 return NULL;
89 return module;
92 static gpointer
93 _g_module_self (void)
95 return &self_module;
98 static void
99 _g_module_close (gpointer handle,
100 gboolean is_unref)
102 if (handle == &self_module)
103 return;
105 if (!NSUnLinkModule (handle, 0))
106 g_module_set_error ("could not unlink module");
109 static gpointer
110 _g_module_symbol (gpointer handle,
111 const gchar *symbol_name)
113 NSSymbol sym;
114 char *msg;
116 if (handle == &self_module)
118 if (NSIsSymbolNameDefined (symbol_name))
119 sym = NSLookupAndBindSymbol (symbol_name);
120 else
121 sym = NULL;
123 else
124 sym = NSLookupSymbolInModule (handle, symbol_name);
126 if (!sym)
128 msg = g_strdup_printf ("no such symbol %s", symbol_name);
129 g_module_set_error (msg);
130 g_free (msg);
131 return NULL;
134 return NSAddressOfSymbol (sym);
137 static gchar*
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);
145 else
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);
150 else
151 return g_strconcat ("lib", module_name, "." G_MODULE_SUFFIX, NULL);