~oif-team/ubuntu/natty/qt4-x11/xi2.1

« back to all changes in this revision

Viewing changes to src/3rdparty/dlcompat/dlfcn_simple.c

  • Committer: Bazaar Package Importer
  • Author(s): Adam Conrad
  • Date: 2005-08-24 04:09:09 UTC
  • Revision ID: james.westby@ubuntu.com-20050824040909-xmxe9jfr4a0w5671
Tags: upstream-4.0.0
ImportĀ upstreamĀ versionĀ 4.0.0

Show diffs side-by-side

added added

removed removed

Lines of Context:
 
1
/*
 
2
Copyright (c) 2002 Peter O'Gorman <ogorman@users.sourceforge.net>
 
3
 
 
4
Permission is hereby granted, free of charge, to any person obtaining
 
5
a copy of this software and associated documentation files (the
 
6
"Software"), to deal in the Software without restriction, including
 
7
without limitation the rights to use, copy, modify, merge, publish,
 
8
distribute, sublicense, and/or sell copies of the Software, and to
 
9
permit persons to whom the Software is furnished to do so, subject to
 
10
the following conditions:
 
11
 
 
12
The above copyright notice and this permission notice shall be
 
13
included in all copies or substantial portions of the Software.
 
14
 
 
15
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 
16
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 
17
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
 
18
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
 
19
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
 
20
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
 
21
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
 
22
*/
 
23
 
 
24
 
 
25
/* Just to prove that it isn't that hard to add Mac calls to your code :)
 
26
   This works with pretty much everything, including kde3 xemacs and the gimp,
 
27
   I'd guess that it'd work in at least 95% of cases, use this as your starting
 
28
   point, rather than the mess that is dlfcn.c, assuming that your code does not
 
29
   require ref counting or symbol lookups in dependent libraries
 
30
*/
 
31
 
 
32
#include <stdio.h>
 
33
#include <stdlib.h>
 
34
#include <string.h>
 
35
#include <sys/types.h>
 
36
#include <sys/stat.h>
 
37
#include <stdarg.h>
 
38
#include <limits.h>
 
39
#include <mach-o/dyld.h>
 
40
#include "dlfcn.h"
 
41
 
 
42
#define ERR_STR_LEN 256
 
43
static void *dlsymIntern(void *handle, const char *symbol);
 
44
static const char *error(int setget, const char *str, ...);
 
45
 
 
46
 
 
47
 
 
48
/* Set and get the error string for use by dlerror */
 
49
static const char *error(int setget, const char *str, ...)
 
50
{
 
51
        static char errstr[ERR_STR_LEN];
 
52
        static int err_filled = 0;
 
53
        const char *retval;
 
54
        NSLinkEditErrors ler;
 
55
        int lerno;
 
56
        const char *dylderrstr;
 
57
        const char *file;
 
58
        va_list arg;
 
59
        if (setget <= 0)
 
60
        {
 
61
                va_start(arg, str);
 
62
                strncpy(errstr, "dlsimple: ", ERR_STR_LEN);
 
63
                vsnprintf(errstr + 10, ERR_STR_LEN - 10, str, arg);
 
64
                va_end(arg);
 
65
        /* We prefer to use the dyld error string if getset is 1*/
 
66
                if (setget == 0) {
 
67
                        NSLinkEditError(&ler, &lerno, &file, &dylderrstr);
 
68
                        fprintf(stderr,"dyld: %s\n",dylderrstr);
 
69
                        if (dylderrstr && strlen(dylderrstr))
 
70
                                strncpy(errstr,dylderrstr,ERR_STR_LEN);
 
71
                }               
 
72
                err_filled = 1;
 
73
                retval = NULL;
 
74
        }
 
75
        else
 
76
        {
 
77
                if (!err_filled)
 
78
                        retval = NULL;
 
79
                else
 
80
                        retval = errstr;
 
81
                err_filled = 0;
 
82
        }
 
83
        return retval;
 
84
}
 
85
 
 
86
/* dlopen */
 
87
void *dlopen(const char *path, int mode)
 
88
{
 
89
        void *module = 0;
 
90
        NSObjectFileImage ofi = 0;
 
91
        NSObjectFileImageReturnCode ofirc;
 
92
        static int (*make_private_module_public) (NSModule module) = 0;
 
93
        unsigned int flags =  NSLINKMODULE_OPTION_RETURN_ON_ERROR | NSLINKMODULE_OPTION_PRIVATE;
 
94
 
 
95
        /* If we got no path, the app wants the global namespace, use -1 as the marker
 
96
           in this case */
 
97
        if (!path)
 
98
                return (void *)-1;
 
99
 
 
100
        /* Create the object file image, works for things linked with the -bundle arg to ld */
 
101
        ofirc = NSCreateObjectFileImageFromFile(path, &ofi);
 
102
        switch (ofirc)
 
103
        {
 
104
                case NSObjectFileImageSuccess:
 
105
                        /* It was okay, so use NSLinkModule to link in the image */
 
106
                        if (!(mode & RTLD_LAZY)) flags += NSLINKMODULE_OPTION_BINDNOW;
 
107
                        module = NSLinkModule(ofi, path,flags);
 
108
                        /* Don't forget to destroy the object file image, unless you like leaks */
 
109
                        NSDestroyObjectFileImage(ofi);
 
110
                        /* If the mode was global, then change the module, this avoids
 
111
                           multiply defined symbol errors to first load private then make
 
112
                           global. Silly, isn't it. */
 
113
                        if ((mode & RTLD_GLOBAL))
 
114
                        {
 
115
                          if (!make_private_module_public)
 
116
                          {
 
117
                            _dyld_func_lookup("__dyld_NSMakePrivateModulePublic", 
 
118
                                (unsigned long *)&make_private_module_public);
 
119
                          }
 
120
                          make_private_module_public(module);
 
121
                        }
 
122
                        break;
 
123
                case NSObjectFileImageInappropriateFile:
 
124
                        /* It may have been a dynamic library rather than a bundle, try to load it */
 
125
                        module = (void *)NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR);
 
126
                        break;
 
127
                case NSObjectFileImageFailure:
 
128
                        error(0,"Object file setup failure :  \"%s\"", path);
 
129
                        return 0;
 
130
                case NSObjectFileImageArch:
 
131
                        error(0,"No object for this architecture :  \"%s\"", path);
 
132
                        return 0;
 
133
                case NSObjectFileImageFormat:
 
134
                        error(0,"Bad object file format :  \"%s\"", path);
 
135
                        return 0;
 
136
                case NSObjectFileImageAccess:
 
137
                        error(0,"Can't read object file :  \"%s\"", path);
 
138
                        return 0;               
 
139
        }
 
140
        if (!module)
 
141
                error(0, "Can not open \"%s\"", path);
 
142
        return module;
 
143
}
 
144
 
 
145
/* dlsymIntern is used by dlsym to find the symbol */
 
146
void *dlsymIntern(void *handle, const char *symbol)
 
147
{
 
148
        NSSymbol *nssym = 0;
 
149
        /* If the handle is -1, if is the app global context */
 
150
        if (handle == (void *)-1)
 
151
        {
 
152
                /* Global context, use NSLookupAndBindSymbol */
 
153
                if (NSIsSymbolNameDefined(symbol))
 
154
                {
 
155
                        nssym = NSLookupAndBindSymbol(symbol);
 
156
                }
 
157
 
 
158
        }
 
159
        /* Now see if the handle is a struch mach_header* or not, use NSLookupSymbol in image
 
160
           for libraries, and NSLookupSymbolInModule for bundles */
 
161
        else
 
162
        {
 
163
                /* Check for both possible magic numbers depending on x86/ppc byte order */
 
164
                if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
 
165
                        (((struct mach_header *)handle)->magic == MH_CIGAM))
 
166
                {
 
167
                        if (NSIsSymbolNameDefinedInImage((struct mach_header *)handle, symbol))
 
168
                        {
 
169
                                nssym = NSLookupSymbolInImage((struct mach_header *)handle,
 
170
                                                                                          symbol,
 
171
                                                                                          NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
 
172
                                                                                          | NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
 
173
                        }
 
174
 
 
175
                }
 
176
                else
 
177
                {
 
178
                        nssym = NSLookupSymbolInModule(handle, symbol);
 
179
                }
 
180
        }
 
181
        if (!nssym)
 
182
        {
 
183
                error(0, "Symbol \"%s\" Not found", symbol);
 
184
                return NULL;
 
185
        }
 
186
        return NSAddressOfSymbol(nssym);
 
187
}
 
188
 
 
189
const char *dlerror(void)
 
190
{
 
191
        return error(1, (char *)NULL);
 
192
}
 
193
 
 
194
int dlclose(void *handle)
 
195
{
 
196
        if ((((struct mach_header *)handle)->magic == MH_MAGIC) ||
 
197
                (((struct mach_header *)handle)->magic == MH_CIGAM))
 
198
        {
 
199
                error(-1, "Can't remove dynamic libraries on darwin");
 
200
                return 0;
 
201
        }
 
202
        if (!NSUnLinkModule(handle, 0))
 
203
        {
 
204
                error(0, "unable to unlink module %s", NSNameOfModule(handle));
 
205
                return 1;
 
206
        }
 
207
        return 0;
 
208
}
 
209
 
 
210
 
 
211
/* dlsym, prepend the underscore and call dlsymIntern */
 
212
void *dlsym(void *handle, const char *symbol)
 
213
{
 
214
        static char undersym[257];      /* Saves calls to malloc(3) */
 
215
        int sym_len = strlen(symbol);
 
216
        void *value = NULL;
 
217
        char *malloc_sym = NULL;
 
218
 
 
219
        if (sym_len < 256)
 
220
        {
 
221
                snprintf(undersym, 256, "_%s", symbol);
 
222
                value = dlsymIntern(handle, undersym);
 
223
        }
 
224
        else
 
225
        {
 
226
                malloc_sym = malloc(sym_len + 2);
 
227
                if (malloc_sym)
 
228
                {
 
229
                        sprintf(malloc_sym, "_%s", symbol);
 
230
                        value = dlsymIntern(handle, malloc_sym);
 
231
                        free(malloc_sym);
 
232
                }
 
233
                else
 
234
                {
 
235
                        error(-1, "Unable to allocate memory");
 
236
                }
 
237
        }
 
238
        return value;
 
239
}