hook gvariant vectors up to kdbus
[platform/upstream/glib.git] / gmodule / gmodule-dyld.c
1 /* GMODULE - GLIB wrapper code for dynamic module loading
2  * Copyright (C) 1998, 2000 Tim Janik
3  *
4  * dyld (Darwin) GMODULE implementation
5  * Copyright (C) 2001 Dan Winship
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, see <http://www.gnu.org/licenses/>.
19  */
20 #include "config.h"
21
22 #include <mach-o/dyld.h>
23
24 static gpointer self_module = GINT_TO_POINTER (1);
25
26 static gpointer
27 _g_module_open (const gchar *file_name,
28                 gboolean     bind_lazy,
29                 gboolean     bind_local)
30 {
31   NSObjectFileImage image;
32   NSObjectFileImageReturnCode ret;
33   NSModule module;
34   unsigned long options;
35   char *msg;
36
37   ret = NSCreateObjectFileImageFromFile (file_name, &image);
38   if (ret != NSObjectFileImageSuccess)
39     {
40       switch (ret)
41         {
42         case NSObjectFileImageInappropriateFile:
43         case NSObjectFileImageFormat:
44           msg = g_strdup_printf ("%s is not a loadable module", file_name);
45           break;
46
47         case NSObjectFileImageArch:
48           msg = g_strdup_printf ("%s is not built for this architecture",
49                                  file_name);
50           break;
51
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;
58
59         default:
60           msg = g_strdup_printf ("unknown error for %s", file_name);
61           break;
62         }
63
64       g_module_set_error (msg);
65       g_free (msg);
66       return NULL;
67     }
68
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)
77     {
78       NSLinkEditErrors c;
79       int error_number;
80       const char *file, *error;
81
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;
87     }
88
89   return module;
90 }
91
92 static gpointer
93 _g_module_self (void)
94 {
95   return &self_module;
96 }
97
98 static void
99 _g_module_close (gpointer handle,
100                  gboolean is_unref)
101 {
102   if (handle == &self_module)
103     return;
104
105   if (!NSUnLinkModule (handle, 0))
106     g_module_set_error ("could not unlink module");
107 }
108
109 static gpointer
110 _g_module_symbol (gpointer     handle,
111                   const gchar *symbol_name)
112 {
113   NSSymbol sym;
114   char *msg;
115
116   if (handle == &self_module)
117     {
118       if (NSIsSymbolNameDefined (symbol_name))
119         sym = NSLookupAndBindSymbol (symbol_name);
120       else
121         sym = NULL;
122     }
123   else
124     sym = NSLookupSymbolInModule (handle, symbol_name);
125
126   if (!sym)
127     {
128       msg = g_strdup_printf ("no such symbol %s", symbol_name);
129       g_module_set_error (msg);
130       g_free (msg);
131       return NULL;
132     }
133
134   return NSAddressOfSymbol (sym);
135 }
136
137 static gchar*
138 _g_module_build_path (const gchar *directory,
139                       const gchar *module_name)
140 {
141   if (directory && *directory)
142     {
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);
147     }
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);
152 }