Say we require autoconf 2.52 as that is what configure.in does.
[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, write to the
19  * Free Software Foundation, Inc., 59 Temple Place - Suite 330,
20  * Boston, MA 02111-1307, USA.
21  */
22
23 #include <mach-o/dyld.h>
24
25 static gpointer self_module = GINT_TO_POINTER (1);
26
27 static gpointer
28 _g_module_open (const gchar *file_name,
29                 gboolean     bind_lazy)
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 | NSLINKMODULE_OPTION_PRIVATE;
70   if (!bind_lazy)
71     options |= NSLINKMODULE_OPTION_BINDNOW;
72   module = NSLinkModule (image, file_name, options);
73   NSDestroyObjectFileImage (image);
74   if (!module)
75     {
76       NSLinkEditErrors c;
77       int error_number;
78       const char *file, *error;
79
80       NSLinkEditError (&c, &error_number, &file, &error);
81       msg = g_strdup_printf ("could not link %s: %s", file_name, error);
82       g_module_set_error (msg);
83       g_free (msg);
84       return NULL;
85     }
86
87   return module;
88 }
89
90 static gpointer
91 _g_module_self (void)
92 {
93   return &self_module;
94 }
95
96 static void
97 _g_module_close (gpointer handle,
98                  gboolean is_unref)
99 {
100   if (handle == &self_module)
101     return;
102
103   if (!NSUnLinkModule (handle, 0))
104     g_module_set_error ("could not unlink module");
105 }
106
107 static gpointer
108 _g_module_symbol (gpointer     handle,
109                   const gchar *symbol_name)
110 {
111   NSSymbol sym;
112   char *msg;
113
114   if (handle == &self_module)
115     {
116       if (NSIsSymbolNameDefined (symbol_name))
117         sym = NSLookupAndBindSymbol (symbol_name);
118       else
119         sym = NULL;
120     }
121   else
122     sym = NSLookupSymbolInModule (handle, symbol_name);
123
124   if (!sym)
125     {
126       msg = g_strdup_printf ("no such symbol %s", symbol_name);
127       g_module_set_error (msg);
128       g_free (msg);
129       return NULL;
130     }
131
132   return NSAddressOfSymbol (sym);
133 }
134
135 static gchar*
136 _g_module_build_path (const gchar *directory,
137                       const gchar *module_name)
138 {
139   if (directory && *directory)
140     {
141       if (strncmp (module_name, "lib", 3) == 0)
142         return g_strconcat (directory, "/", module_name, NULL);
143       else
144         return g_strconcat (directory, "/lib", module_name, "." G_MODULE_SUFFIX, NULL);
145     }
146   else if (strncmp (module_name, "lib", 3) == 0)
147     return g_strdup (module_name);
148   else
149     return g_strconcat ("lib", module_name, "." G_MODULE_SUFFIX, NULL);
150 }