Skip to content
This repository has been archived by the owner on Feb 11, 2021. It is now read-only.

Latest commit

 

History

History
1391 lines (1279 loc) · 42.3 KB

SDL_dlcompat.c

File metadata and controls

1391 lines (1279 loc) · 42.3 KB
 
1
2
/*
SDL - Simple DirectMedia Layer
Dec 8, 2008
Dec 8, 2008
3
Copyright (C) 1997-2009 Sam Lantinga
4
5
This library is free software; you can redistribute it and/or
Feb 1, 2006
Feb 1, 2006
6
modify it under the terms of the GNU Lesser General Public
7
License as published by the Free Software Foundation; either
Feb 1, 2006
Feb 1, 2006
8
version 2.1 of the License, or (at your option) any later version.
9
10
11
12
This library is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the GNU
Feb 1, 2006
Feb 1, 2006
13
Lesser General Public License for more details.
Feb 1, 2006
Feb 1, 2006
15
16
17
You should have received a copy of the GNU Lesser General Public
License along with this library; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA
18
19
20
21
Sam Lantinga
slouken@libsdl.org
*/
Feb 21, 2006
Feb 21, 2006
22
#include "SDL_config.h"
Apr 14, 2006
Apr 14, 2006
24
25
#ifdef SDL_LOADSO_DLCOMPAT
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
/* Please note that dlcompat apparently ships in current Mac OS X versions
* as a system library that provides compatibility with the Unix "dlopen"
* interface. In order to allow SDL to work on older OS X releases and also
* not conflict with the system lib on newer versions, we include dlcompat
* in SDL and change the symbols to prevent symbol clash with any existing
* system libraries. --ryan.
*/
/* here is the dlcompat license: */
/*
Copyright (c) 2002 Jorge Acereda <jacereda@users.sourceforge.net> &
Peter O'Gorman <ogorman@users.sourceforge.net>
Portions may be copyright others, see the AUTHORS file included with this
distribution.
Maintained by Peter O'Gorman <ogorman@users.sourceforge.net>
Bug Reports and other queries should go to <ogorman@users.sourceforge.net>
Permission is hereby granted, free of charge, to any person obtaining
a copy of this software and associated documentation files (the
"Software"), to deal in the Software without restriction, including
without limitation the rights to use, copy, modify, merge, publish,
distribute, sublicense, and/or sell copies of the Software, and to
permit persons to whom the Software is furnished to do so, subject to
the following conditions:
The above copyright notice and this permission notice shall be
included in all copies or substantial portions of the Software.
THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND
NONINFRINGEMENT. IN NO EVENT SHALL THE AUTHORS OR COPYRIGHT HOLDERS BE
LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN ACTION
OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN CONNECTION
WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
*/
#include <pthread.h>
#include <sys/types.h>
#include <sys/stat.h>
#include <stdarg.h>
#include <limits.h>
#include <mach-o/dyld.h>
#include <mach-o/nlist.h>
#include <mach-o/getsect.h>
Feb 10, 2006
Feb 10, 2006
76
#include "SDL_stdinc.h"
Feb 7, 2006
Feb 7, 2006
77
78
79
80
81
82
83
84
85
86
87
/* Just playing to see if it would compile with the freebsd headers, it does,
* but because of the different values for RTLD_LOCAL etc, it would break binary
* compat... oh well
*/
#ifndef __BSD_VISIBLE
#define __BSD_VISIBLE 1
#endif
/*include "dlfcn.h"*/
#ifdef __cplusplus
Jul 10, 2006
Jul 10, 2006
88
89
extern "C"
{
90
91
92
93
94
95
96
97
#endif
#if defined (__GNUC__) && __GNUC__ > 3
#define dl_restrict __restrict
#else
#define dl_restrict
#endif
May 9, 2006
May 9, 2006
98
#if 0
99
100
101
102
#ifndef _POSIX_SOURCE
/*
* Structure filled in by dladdr().
*/
Jul 10, 2006
Jul 10, 2006
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
typedef struct SDL_OSX_dl_info
{
const char *dli_fname; /* Pathname of shared object */
void *dli_fbase; /* Base address of shared object */
const char *dli_sname; /* Name of nearest symbol */
void *dli_saddr; /* Address of nearest symbol */
} SDL_OSX_Dl_info;
static int SDL_OSX_dladdr(const void *dl_restrict,
SDL_OSX_Dl_info * dl_restrict);
#endif /* ! _POSIX_SOURCE */
#endif /* 0 */
static int SDL_OSX_dlclose(void *handle);
static const char *SDL_OSX_dlerror(void);
static void *SDL_OSX_dlopen(const char *path, int mode);
static void *SDL_OSX_dlsym(void *dl_restrict handle,
const char *dl_restrict symbol);
121
122
123
124
125
126
127
128
129
130
131
132
133
#define RTLD_LAZY 0x1
#define RTLD_NOW 0x2
#define RTLD_LOCAL 0x4
#define RTLD_GLOBAL 0x8
#ifndef _POSIX_SOURCE
#define RTLD_NOLOAD 0x10
#define RTLD_NODELETE 0x80
/*
* Special handle arguments for SDL_OSX_dlsym().
*/
Jul 10, 2006
Jul 10, 2006
134
135
136
#define RTLD_NEXT ((void *) -1) /* Search subsequent objects. */
#define RTLD_DEFAULT ((void *) -2) /* Use default search algorithm. */
#endif /* ! _POSIX_SOURCE */
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
#ifdef __cplusplus
}
#endif
#ifndef dl_restrict
#define dl_restrict __restrict
#endif
/* This is not available on 10.1 */
#ifndef LC_LOAD_WEAK_DYLIB
#define LC_LOAD_WEAK_DYLIB (0x18 | LC_REQ_DYLD)
#endif
/* With this stuff here, this thing may actually compile/run on 10.0 systems
* Not that I have a 10.0 system to test it on anylonger
*/
#ifndef LC_REQ_DYLD
#define LC_REQ_DYLD 0x80000000
#endif
#ifndef NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED
#define NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED 0x4
#endif
#ifndef NSADDIMAGE_OPTION_RETURN_ON_ERROR
#define NSADDIMAGE_OPTION_RETURN_ON_ERROR 0x1
#endif
#ifndef NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
#define NSLOOKUPSYMBOLINIMAGE_OPTION_BIND 0x0
#endif
#ifndef NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR
#define NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR 0x4
#endif
/* These symbols will be looked for in dyld */
Jul 10, 2006
Jul 10, 2006
169
170
171
172
173
174
175
static const struct mach_header *(*dyld_NSAddImage) (const char *,
unsigned long) = 0;
static int (*dyld_NSIsSymbolNameDefinedInImage) (const struct mach_header *,
const char *) = 0;
static NSSymbol(*dyld_NSLookupSymbolInImage) (const struct mach_header *,
const char *, unsigned long) =
0;
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
/* Define this to make dlcompat reuse data block. This way in theory we save
* a little bit of overhead. However we then couldn't correctly catch excess
* calls to SDL_OSX_dlclose(). Hence we don't use this feature
*/
#undef REUSE_STATUS
/* Size of the internal error message buffer (used by dlerror()) */
#define ERR_STR_LEN 251
/* Maximum number of search paths supported by getSearchPath */
#define MAX_SEARCH_PATHS 32
#define MAGIC_DYLIB_OFI ((NSObjectFileImage) 'DYOF')
#define MAGIC_DYLIB_MOD ((NSModule) 'DYMO')
/* internal flags */
#define DL_IN_LIST 0x01
/* our mutex */
static pthread_mutex_t dlcompat_mutex;
/* Our thread specific storage
*/
static pthread_key_t dlerror_key;
struct dlthread
{
Jul 10, 2006
Jul 10, 2006
204
205
206
int lockcnt;
unsigned char errset;
char errstr[ERR_STR_LEN];
207
208
209
210
211
212
213
};
/* This is our central data structure. Whenever a module is loaded via
* SDL_OSX_dlopen(), we create such a struct.
*/
struct dlstatus
{
Jul 10, 2006
Jul 10, 2006
214
215
216
217
218
219
220
221
struct dlstatus *next; /* pointer to next element in the linked list */
NSModule module;
const struct mach_header *lib;
int refs; /* reference count */
int mode; /* mode in which this module was loaded */
dev_t device;
ino_t inode;
int flags; /* Any internal flags we may need */
222
223
224
};
/* Head node of the dlstatus list */
Jul 10, 2006
Jul 10, 2006
225
226
static struct dlstatus mainStatus =
{ 0, MAGIC_DYLIB_MOD, NULL, -1, RTLD_GLOBAL, 0, 0, 0 };
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
static struct dlstatus *stqueue = &mainStatus;
/* Storage for the last error message (used by dlerror()) */
/* static char err_str[ERR_STR_LEN]; */
/* static int err_filled = 0; */
/* Prototypes to internal functions */
static void debug(const char *fmt, ...);
static void error(const char *str, ...);
static const char *safegetenv(const char *s);
static const char *searchList(void);
static const char *getSearchPath(int i);
static const char *getFullPath(int i, const char *file);
static const struct stat *findFile(const char *file, const char **fullPath);
static int isValidStatus(struct dlstatus *status);
static inline int isFlagSet(int mode, int flag);
static struct dlstatus *lookupStatus(const struct stat *sbuf);
static void insertStatus(struct dlstatus *dls, const struct stat *sbuf);
static int promoteLocalToGlobal(struct dlstatus *dls);
static void *reference(struct dlstatus *dls, int mode);
Jul 10, 2006
Jul 10, 2006
248
249
static void *dlsymIntern(struct dlstatus *dls, const char *symbol,
int canSetError);
250
static struct dlstatus *allocStatus(void);
Jul 10, 2006
Jul 10, 2006
251
252
253
254
static struct dlstatus *loadModule(const char *path, const struct stat *sbuf,
int mode);
static NSSymbol search_linked_libs(const struct mach_header *mh,
const char *symbol);
255
static const char *get_lib_name(const struct mach_header *mh);
May 9, 2006
May 9, 2006
256
static const struct mach_header *get_mach_header_from_NSModule(NSModule mod);
257
258
259
260
261
262
263
264
static void dlcompat_init_func(void);
static inline void dlcompat_init_check(void);
static inline void dolock(void);
static inline void dounlock(void);
static void dlerrorfree(void *data);
static void resetdlerror(void);
static const struct mach_header *my_find_image(const char *name);
static const struct mach_header *image_for_address(const void *address);
Apr 22, 2006
Apr 22, 2006
265
static inline char *dyld_error_str(void);
266
267
268
269
270
271
272
#if FINK_BUILD
/* Two Global Functions */
static void *dlsym_prepend_underscore(void *handle, const char *symbol);
static void *dlsym_auto_underscore(void *handle, const char *symbol);
/* And their _intern counterparts */
Jul 10, 2006
Jul 10, 2006
273
274
static void *dlsym_prepend_underscore_intern(void *handle,
const char *symbol);
275
276
277
278
279
static void *dlsym_auto_underscore_intern(void *handle, const char *symbol);
#endif
/* Functions */
Jul 10, 2006
Jul 10, 2006
280
281
static void
debug(const char *fmt, ...)
282
283
{
#if DEBUG > 1
Jul 10, 2006
Jul 10, 2006
284
285
286
287
288
289
290
va_list arg;
va_start(arg, fmt);
fprintf(stderr, "DLDEBUG: ");
vfprintf(stderr, fmt, arg);
fprintf(stderr, "\n");
fflush(stderr);
va_end(arg);
291
292
293
#endif
}
Jul 10, 2006
Jul 10, 2006
294
295
static void
error(const char *str, ...)
Jul 10, 2006
Jul 10, 2006
297
298
299
300
301
302
303
304
305
306
307
va_list arg;
struct dlthread *tss;
char *err_str;
va_start(arg, str);
tss = pthread_getspecific(dlerror_key);
err_str = tss->errstr;
SDL_strlcpy(err_str, "dlcompat: ", ERR_STR_LEN);
vsnprintf(err_str + 10, ERR_STR_LEN - 10, str, arg);
va_end(arg);
debug("ERROR: %s\n", err_str);
tss->errset = 1;
Jul 10, 2006
Jul 10, 2006
310
311
static void
warning(const char *str)
312
313
{
#if DEBUG > 0
Jul 10, 2006
Jul 10, 2006
314
fprintf(stderr, "WARNING: dlcompat: %s\n", str);
315
316
317
#endif
}
Jul 10, 2006
Jul 10, 2006
318
319
static const char *
safegetenv(const char *s)
Jul 10, 2006
Jul 10, 2006
321
322
const char *ss = SDL_getenv(s);
return ss ? ss : "";
323
324
325
326
327
328
}
/* because this is only used for debugging and error reporting functions, we
* don't really care about how elegant it is... it could use the load
* commands to find the install name of the library, but...
*/
Jul 10, 2006
Jul 10, 2006
329
330
static const char *
get_lib_name(const struct mach_header *mh)
Jul 10, 2006
Jul 10, 2006
332
333
334
335
336
337
338
339
340
341
342
343
unsigned long count = _dyld_image_count();
unsigned long i;
const char *val = NULL;
if (mh) {
for (i = 0; i < count; i++) {
if (mh == _dyld_get_image_header(i)) {
val = _dyld_get_image_name(i);
break;
}
}
}
return val;
344
345
346
347
348
349
}
/* Returns the mach_header for the module bu going through all the loaded images
* and finding the one with the same name as the module. There really ought to be
* an api for doing this, would be faster, but there isn't one right now
*/
Jul 10, 2006
Jul 10, 2006
350
351
static const struct mach_header *
get_mach_header_from_NSModule(NSModule mod)
Jul 10, 2006
Jul 10, 2006
353
354
355
356
357
358
359
360
361
362
363
364
const char *mod_name = NSNameOfModule(mod);
const struct mach_header *mh = NULL;
unsigned long count = _dyld_image_count();
unsigned long i;
debug("Module name: %s", mod_name);
for (i = 0; i < count; i++) {
if (!SDL_strcmp(mod_name, _dyld_get_image_name(i))) {
mh = _dyld_get_image_header(i);
break;
}
}
return mh;
365
366
367
368
369
370
371
372
373
374
375
}
/* Compute and return a list of all directories that we should search when
* trying to locate a module. We first look at the values of LD_LIBRARY_PATH
* and DYLD_LIBRARY_PATH, and then finally fall back to looking into
* /usr/lib and /lib. Since both of the environments variables can contain a
* list of colon seperated paths, we simply concat them and the two other paths
* into one big string, which we then can easily parse.
* Splitting this string into the actual path list is done by getSearchPath()
*/
Jul 10, 2006
Jul 10, 2006
376
377
static const char *
searchList()
Jul 10, 2006
Jul 10, 2006
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
size_t buf_size;
static char *buf = NULL;
const char *ldlp = safegetenv("LD_LIBRARY_PATH");
const char *dyldlp = safegetenv("DYLD_LIBRARY_PATH");
const char *stdpath = SDL_getenv("DYLD_FALLBACK_LIBRARY_PATH");
if (!stdpath)
stdpath = "/usr/local/lib:/lib:/usr/lib";
if (!buf) {
buf_size =
SDL_strlen(ldlp) + SDL_strlen(dyldlp) + SDL_strlen(stdpath) + 4;
buf = SDL_malloc(buf_size);
SDL_snprintf(buf, buf_size, "%s%s%s%s%s%c", dyldlp,
(dyldlp[0] ? ":" : ""), ldlp, (ldlp[0] ? ":" : ""),
stdpath, '\0');
}
return buf;
395
396
397
}
/* Returns the ith search path from the list as computed by searchList() */
Jul 10, 2006
Jul 10, 2006
398
399
static const char *
getSearchPath(int i)
Jul 10, 2006
Jul 10, 2006
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
static const char *list = 0;
static char **path = (char **) 0;
static int end = 0;
static int numsize = MAX_SEARCH_PATHS;
static char **tmp;
/* So we can call SDL_free() in the "destructor" we use i=-1 to return the alloc'd array */
if (i == -1) {
return (const char *) path;
}
if (!path) {
path = (char **) SDL_calloc(MAX_SEARCH_PATHS, sizeof(char **));
}
if (!list && !end)
list = searchList();
if (i >= (numsize)) {
debug("Increasing size for long PATH");
tmp =
(char **) SDL_calloc((MAX_SEARCH_PATHS + numsize),
sizeof(char **));
if (tmp) {
SDL_memcpy(tmp, path, sizeof(char **) * numsize);
SDL_free(path);
path = tmp;
numsize += MAX_SEARCH_PATHS;
} else {
return 0;
}
}
while (!path[i] && !end) {
path[i] = strsep((char **) &list, ":");
if (path[i][0] == 0)
path[i] = 0;
end = (list == 0);
}
return path[i];
Jul 10, 2006
Jul 10, 2006
440
441
static const char *
getFullPath(int i, const char *file)
Jul 10, 2006
Jul 10, 2006
443
444
445
446
447
448
static char buf[PATH_MAX];
const char *path = getSearchPath(i);
if (path) {
SDL_snprintf(buf, PATH_MAX, "%s/%s", path, file);
}
return path ? buf : 0;
449
450
451
452
453
454
}
/* Given a file name, try to determine the full path for that file. Starts
* its search in the current directory, and then tries all paths in the
* search list in the order they are specified there.
*/
Jul 10, 2006
Jul 10, 2006
455
456
static const struct stat *
findFile(const char *file, const char **fullPath)
Jul 10, 2006
Jul 10, 2006
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
int i = 0;
static struct stat sbuf;
char *fileName;
debug("finding file %s", file);
*fullPath = file;
if (0 == stat(file, &sbuf))
return &sbuf;
if (SDL_strchr(file, '/'))
return 0; /* If the path had a / we don't look in env var places */
fileName = NULL;
if (!fileName)
fileName = (char *) file;
while ((*fullPath = getFullPath(i++, fileName))) {
if (0 == stat(*fullPath, &sbuf))
return &sbuf;
}
;
return 0;
476
477
478
}
/* Determine whether a given dlstatus is valid or not */
Jul 10, 2006
Jul 10, 2006
479
480
static int
isValidStatus(struct dlstatus *status)
Jul 10, 2006
Jul 10, 2006
482
483
484
485
486
487
488
489
490
491
492
/* Walk the list to verify status is contained in it */
struct dlstatus *dls = stqueue;
while (dls && status != dls)
dls = dls->next;
if (dls == 0)
error("invalid handle");
else if ((dls->module == 0) || (dls->refs == 0))
error("handle to closed library");
else
return TRUE;
return FALSE;
Jul 10, 2006
Jul 10, 2006
495
496
static inline int
isFlagSet(int mode, int flag)
Jul 10, 2006
Jul 10, 2006
498
return (mode & flag) == flag;
Jul 10, 2006
Jul 10, 2006
501
502
static struct dlstatus *
lookupStatus(const struct stat *sbuf)
Jul 10, 2006
Jul 10, 2006
504
505
506
507
508
509
510
struct dlstatus *dls = stqueue;
debug("looking for status");
while (dls && ( /* isFlagSet(dls->mode, RTLD_UNSHARED) */ 0
|| sbuf->st_dev != dls->device
|| sbuf->st_ino != dls->inode))
dls = dls->next;
return dls;
Jul 10, 2006
Jul 10, 2006
513
514
static void
insertStatus(struct dlstatus *dls, const struct stat *sbuf)
Jul 10, 2006
Jul 10, 2006
516
517
518
519
520
521
522
523
524
525
debug("inserting status");
dls->inode = sbuf->st_ino;
dls->device = sbuf->st_dev;
dls->refs = 0;
dls->mode = 0;
if ((dls->flags & DL_IN_LIST) == 0) {
dls->next = stqueue;
stqueue = dls;
dls->flags |= DL_IN_LIST;
}
Jul 10, 2006
Jul 10, 2006
528
529
static struct dlstatus *
allocStatus()
Jul 10, 2006
Jul 10, 2006
531
struct dlstatus *dls;
532
#ifdef REUSE_STATUS
Jul 10, 2006
Jul 10, 2006
533
534
535
536
dls = stqueue;
while (dls && dls->module)
dls = dls->next;
if (!dls)
Jul 10, 2006
Jul 10, 2006
538
539
dls = SDL_calloc(sizeof(*dls), 1);
return dls;
Jul 10, 2006
Jul 10, 2006
542
543
static int
promoteLocalToGlobal(struct dlstatus *dls)
Jul 10, 2006
Jul 10, 2006
545
546
547
548
549
static int (*p) (NSModule module) = 0;
debug("promoting");
if (!p)
_dyld_func_lookup("__dyld_NSMakePrivateModulePublic", (void **) &p);
return (dls->module == MAGIC_DYLIB_MOD) || (p && p(dls->module));
Jul 10, 2006
Jul 10, 2006
552
553
static void *
reference(struct dlstatus *dls, int mode)
Jul 10, 2006
Jul 10, 2006
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
if (dls) {
if (dls->module == MAGIC_DYLIB_MOD && isFlagSet(mode, RTLD_LOCAL)) {
warning("trying to open a .dylib with RTLD_LOCAL");
error("unable to open a .dylib with RTLD_LOCAL");
return NULL;
}
if (isFlagSet(mode, RTLD_GLOBAL) && !isFlagSet(dls->mode, RTLD_GLOBAL)
&& !promoteLocalToGlobal(dls)) {
error("unable to promote local module to global");
return NULL;
}
dls->mode |= mode;
dls->refs++;
} else
debug("reference called with NULL argument");
return dls;
Jul 10, 2006
Jul 10, 2006
574
575
static const struct mach_header *
my_find_image(const char *name)
Jul 10, 2006
Jul 10, 2006
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
const struct mach_header *mh = 0;
const char *id = NULL;
int i = _dyld_image_count();
int j;
mh = (struct mach_header *)
dyld_NSAddImage(name, NSADDIMAGE_OPTION_RETURN_ONLY_IF_LOADED |
NSADDIMAGE_OPTION_RETURN_ON_ERROR);
if (!mh) {
for (j = 0; j < i; j++) {
id = _dyld_get_image_name(j);
if (!SDL_strcmp(id, name)) {
mh = _dyld_get_image_header(j);
break;
}
}
}
return mh;
594
595
596
597
598
599
600
601
}
/*
* dyld adds libraries by first adding the directly dependant libraries in link order, and
* then adding the dependencies for those libraries, so we should do the same... but we don't
* bother adding the extra dependencies, if the symbols are neither in the loaded image nor
* any of it's direct dependencies, then it probably isn't there.
*/
Jul 10, 2006
Jul 10, 2006
602
603
static NSSymbol
search_linked_libs(const struct mach_header *mh, const char *symbol)
Jul 10, 2006
Jul 10, 2006
605
606
607
608
609
610
611
612
613
614
615
616
617
618
unsigned int n;
struct load_command *lc = 0;
struct mach_header *wh;
NSSymbol nssym = 0;
if (dyld_NSAddImage && dyld_NSIsSymbolNameDefinedInImage
&& dyld_NSLookupSymbolInImage) {
lc = (struct load_command *) ((char *) mh +
sizeof(struct mach_header));
for (n = 0; n < mh->ncmds;
n++, lc = (struct load_command *) ((char *) lc + lc->cmdsize)) {
if ((LC_LOAD_DYLIB == lc->cmd)
|| (LC_LOAD_WEAK_DYLIB == lc->cmd)) {
if ((wh = (struct mach_header *)
my_find_image((char
Jan 9, 2009
Jan 9, 2009
619
620
*) (((struct dylib_command *) lc)->
dylib.name.offset + (char *) lc)))) {
Jul 10, 2006
Jul 10, 2006
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
if (dyld_NSIsSymbolNameDefinedInImage(wh, symbol)) {
nssym = dyld_NSLookupSymbolInImage(wh,
symbol,
NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
|
NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
break;
}
}
}
}
if ((!nssym) && NSIsSymbolNameDefined(symbol)) {
/* I've never seen this debug message... */
debug("Symbol \"%s\" is defined but was not found", symbol);
}
}
return nssym;
Feb 7, 2006
Feb 7, 2006
640
/* Up to the caller to SDL_free() returned string */
Jul 10, 2006
Jul 10, 2006
641
642
static inline char *
dyld_error_str()
Jul 10, 2006
Jul 10, 2006
644
645
646
647
648
649
650
651
652
653
NSLinkEditErrors dylder;
int dylderno;
const char *dylderrstr;
const char *dyldfile;
char *retStr = NULL;
NSLinkEditError(&dylder, &dylderno, &dyldfile, &dylderrstr);
if (dylderrstr && *dylderrstr) {
retStr = SDL_strdup(dylderrstr);
}
return retStr;
Jul 10, 2006
Jul 10, 2006
656
657
static void *
dlsymIntern(struct dlstatus *dls, const char *symbol, int canSetError)
Jul 10, 2006
Jul 10, 2006
659
660
661
NSSymbol nssym = 0;
#ifdef __GCC__
void *caller = __builtin_return_address(1); /* Be *very* careful about inlining */
Jul 10, 2006
Jul 10, 2006
663
void *caller = NULL;
Jul 10, 2006
Jul 10, 2006
665
666
667
const struct mach_header *caller_mh = 0;
char *savedErrorStr = NULL;
resetdlerror();
668
669
670
#ifndef RTLD_SELF
#define RTLD_SELF ((void *) -3)
#endif
Jul 10, 2006
Jul 10, 2006
671
672
673
674
675
676
677
678
679
680
681
682
683
684
685
686
687
688
689
690
691
692
693
694
695
696
697
698
699
700
701
702
703
704
705
706
707
708
709
710
711
712
713
714
715
716
717
718
719
720
721
722
723
724
725
726
727
728
729
730
731
732
733
734
735
736
737
738
739
740
741
742
743
744
745
746
747
748
749
750
751
752
753
754
755
756
757
758
759
760
761
762
763
764
765
if (NULL == dls)
dls = RTLD_SELF;
if ((RTLD_NEXT == dls) || (RTLD_SELF == dls)) {
if (dyld_NSIsSymbolNameDefinedInImage && dyld_NSLookupSymbolInImage
&& caller) {
caller_mh = image_for_address(caller);
if (RTLD_SELF == dls) {
/* FIXME: We should be using the NSModule api, if SELF is an MH_BUNDLE
* But it appears to work anyway, and looking at the code in dyld_libfuncs.c
* this is acceptable.
*/
if (dyld_NSIsSymbolNameDefinedInImage(caller_mh, symbol)) {
nssym = dyld_NSLookupSymbolInImage(caller_mh,
symbol,
NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
|
NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
}
}
if (!nssym) {
if (RTLD_SELF == dls)
savedErrorStr = dyld_error_str();
nssym = search_linked_libs(caller_mh, symbol);
}
} else {
if (canSetError)
error("RTLD_SELF and RTLD_NEXT are not supported");
return NULL;
}
}
if (!nssym) {
if (RTLD_DEFAULT == dls) {
dls = &mainStatus;
}
if (!isValidStatus(dls))
return NULL;
if (dls->module != MAGIC_DYLIB_MOD) {
nssym = NSLookupSymbolInModule(dls->module, symbol);
if (!nssym && NSIsSymbolNameDefined(symbol)) {
debug("Searching dependencies");
savedErrorStr = dyld_error_str();
nssym =
search_linked_libs(get_mach_header_from_NSModule
(dls->module), symbol);
}
} else if (dls->lib && dyld_NSIsSymbolNameDefinedInImage
&& dyld_NSLookupSymbolInImage) {
if (dyld_NSIsSymbolNameDefinedInImage(dls->lib, symbol)) {
nssym = dyld_NSLookupSymbolInImage(dls->lib,
symbol,
NSLOOKUPSYMBOLINIMAGE_OPTION_BIND
|
NSLOOKUPSYMBOLINIMAGE_OPTION_RETURN_ON_ERROR);
} else if (NSIsSymbolNameDefined(symbol)) {
debug("Searching dependencies");
savedErrorStr = dyld_error_str();
nssym = search_linked_libs(dls->lib, symbol);
}
} else if (dls->module == MAGIC_DYLIB_MOD) {
/* Global context, use NSLookupAndBindSymbol */
if (NSIsSymbolNameDefined(symbol)) {
/* There doesn't seem to be a return on error option for this call???
this is potentially broken, if binding fails, it will improperly
exit the application. */
nssym = NSLookupAndBindSymbol(symbol);
} else {
if (savedErrorStr)
SDL_free(savedErrorStr);
savedErrorStr = SDL_malloc(256);
SDL_snprintf(savedErrorStr, 256,
"Symbol \"%s\" not in global context", symbol);
}
}
}
/* Error reporting */
if (!nssym) {
if (!savedErrorStr || !SDL_strlen(savedErrorStr)) {
if (savedErrorStr)
SDL_free(savedErrorStr);
savedErrorStr = SDL_malloc(256);
SDL_snprintf(savedErrorStr, 256, "Symbol \"%s\" not found",
symbol);
}
if (canSetError) {
error(savedErrorStr);
} else {
debug(savedErrorStr);
}
if (savedErrorStr)
SDL_free(savedErrorStr);
return NULL;
}
return NSAddressOfSymbol(nssym);
Jul 10, 2006
Jul 10, 2006
768
769
static struct dlstatus *
loadModule(const char *path, const struct stat *sbuf, int mode)
Jul 10, 2006
Jul 10, 2006
771
772
773
774
775
776
777
778
779
780
781
782
783
784
785
786
787
788
789
790
791
792
793
794
795
796
797
798
799
800
801
802
803
804
805
806
807
808
809
810
811
812
813
814
815
816
817
818
819
820
821
822
823
824
825
826
827
828
829
830
831
832
833
834
835
836
837
838
839
840
841
842
843
844
845
846
847
848
849
850
851
852
853
854
855
856
857
858
859
860
861
862
863
864
865
866
867
868
869
NSObjectFileImage ofi = 0;
NSObjectFileImageReturnCode ofirc;
struct dlstatus *dls;
NSLinkEditErrors ler;
int lerno;
const char *errstr;
const char *file;
void (*init) (void);
ofirc = NSCreateObjectFileImageFromFile(path, &ofi);
switch (ofirc) {
case NSObjectFileImageSuccess:
break;
case NSObjectFileImageInappropriateFile:
if (dyld_NSAddImage && dyld_NSIsSymbolNameDefinedInImage
&& dyld_NSLookupSymbolInImage) {
if (isFlagSet(mode, RTLD_LOCAL)) {
warning("trying to open a .dylib with RTLD_LOCAL");
error("unable to open this file with RTLD_LOCAL");
return NULL;
}
} else {
error("opening this file is unsupported on this system");
return NULL;
}
break;
case NSObjectFileImageFailure:
error("object file setup failure");
return NULL;
case NSObjectFileImageArch:
error("no object for this architecture");
return NULL;
case NSObjectFileImageFormat:
error("bad object file format");
return NULL;
case NSObjectFileImageAccess:
error("can't read object file");
return NULL;
default:
error("unknown error from NSCreateObjectFileImageFromFile()");
return NULL;
}
dls = lookupStatus(sbuf);
if (!dls) {
dls = allocStatus();
}
if (!dls) {
error("unable to allocate memory");
return NULL;
}
// dls->lib = 0;
if (ofirc == NSObjectFileImageInappropriateFile) {
if ((dls->lib =
dyld_NSAddImage(path, NSADDIMAGE_OPTION_RETURN_ON_ERROR))) {
debug("Dynamic lib loaded at %ld", dls->lib);
ofi = MAGIC_DYLIB_OFI;
dls->module = MAGIC_DYLIB_MOD;
ofirc = NSObjectFileImageSuccess;
/* Although it is possible with a bit of work to modify this so it works and
functions with RTLD_NOW, I don't deem it necessary at the moment */
}
if (!(dls->module)) {
NSLinkEditError(&ler, &lerno, &file, &errstr);
if (!errstr || (!SDL_strlen(errstr)))
error("Can't open this file type");
else
error(errstr);
if ((dls->flags & DL_IN_LIST) == 0) {
SDL_free(dls);
}
return NULL;
}
} else {
dls->module = NSLinkModule(ofi, path,
NSLINKMODULE_OPTION_RETURN_ON_ERROR |
NSLINKMODULE_OPTION_PRIVATE |
(isFlagSet(mode, RTLD_NOW) ?
NSLINKMODULE_OPTION_BINDNOW : 0));
NSDestroyObjectFileImage(ofi);
if (dls->module) {
dls->lib = get_mach_header_from_NSModule(dls->module);
}
}
if (!dls->module) {
NSLinkEditError(&ler, &lerno, &file, &errstr);
if ((dls->flags & DL_IN_LIST) == 0) {
SDL_free(dls);
}
error(errstr);
return NULL;
}
insertStatus(dls, sbuf);
dls = reference(dls, mode);
if ((init = dlsymIntern(dls, "__init", 0))) {
debug("calling _init()");
init();
}
return dls;
Jul 10, 2006
Jul 10, 2006
872
873
inline static void
dlcompat_init_check(void)
Jul 10, 2006
Jul 10, 2006
875
876
877
878
879
880
881
882
883
static pthread_mutex_t l = PTHREAD_MUTEX_INITIALIZER;
static int init_done = 0;
pthread_mutex_lock(&l);
if (!init_done) {
dlcompat_init_func();
init_done = 1;
}
pthread_mutex_unlock(&l);
Jul 10, 2006
Jul 10, 2006
886
887
static void
dlcompat_init_func(void)
Jul 10, 2006
Jul 10, 2006
889
890
891
892
893
894
895
896
897
_dyld_func_lookup("__dyld_NSAddImage", (void **) &dyld_NSAddImage);
_dyld_func_lookup("__dyld_NSIsSymbolNameDefinedInImage",
(void **) &dyld_NSIsSymbolNameDefinedInImage);
_dyld_func_lookup("__dyld_NSLookupSymbolInImage",
(void **) &dyld_NSLookupSymbolInImage);
if (pthread_mutex_init(&dlcompat_mutex, NULL))
exit(1);
if (pthread_key_create(&dlerror_key, &dlerrorfree))
exit(1);
Jul 10, 2006
Jul 10, 2006
900
901
static void
resetdlerror()
Jul 10, 2006
Jul 10, 2006
903
904
905
struct dlthread *tss;
tss = pthread_getspecific(dlerror_key);
tss->errset = 0;
Jul 10, 2006
Jul 10, 2006
908
909
static void
dlerrorfree(void *data)
Jul 10, 2006
Jul 10, 2006
911
SDL_free(data);
912
913
914
915
916
}
/* We kind of want a recursive lock here, but meet a little trouble
* because they are not available pre OS X 10.2, so we fake it
* using thread specific storage to keep a lock count
Jul 10, 2006
Jul 10, 2006
917
918
919
*/
static inline void
dolock(void)
Jul 10, 2006
Jul 10, 2006
921
922
923
924
925
926
927
928
929
930
931
932
933
934
935
936
937
938
int err = 0;
struct dlthread *tss;
dlcompat_init_check();
tss = pthread_getspecific(dlerror_key);
if (!tss) {
tss = SDL_malloc(sizeof(struct dlthread));
tss->lockcnt = 0;
tss->errset = 0;
if (pthread_setspecific(dlerror_key, tss)) {
fprintf(stderr, "dlcompat: pthread_setspecific failed\n");
exit(1);
}
}
if (!tss->lockcnt)
err = pthread_mutex_lock(&dlcompat_mutex);
tss->lockcnt = tss->lockcnt + 1;
if (err)
exit(err);
Jul 10, 2006
Jul 10, 2006
941
942
static inline void
dounlock(void)
Jul 10, 2006
Jul 10, 2006
944
945
946
947
948
949
950
951
int err = 0;
struct dlthread *tss;
tss = pthread_getspecific(dlerror_key);
tss->lockcnt = tss->lockcnt - 1;
if (!tss->lockcnt)
err = pthread_mutex_unlock(&dlcompat_mutex);
if (err)
exit(err);
Jul 10, 2006
Jul 10, 2006
954
955
static void *
SDL_OSX_dlopen(const char *path, int mode)
Jul 10, 2006
Jul 10, 2006
957
958
959
960
961
962
963
964
965
966
967
968
969
970
971
972
973
974
975
976
const struct stat *sbuf;
struct dlstatus *dls;
const char *fullPath;
dolock();
resetdlerror();
if (!path) {
dls = &mainStatus;
goto dlopenok;
}
if (!(sbuf = findFile(path, &fullPath))) {
error("file \"%s\" not found", path);
goto dlopenerror;
}
/* Now checks that it hasn't been closed already */
if ((dls = lookupStatus(sbuf)) && (dls->refs > 0)) {
/* debug("status found"); */
dls = reference(dls, mode);
goto dlopenok;
}
977
#ifdef RTLD_NOLOAD
Jul 10, 2006
Jul 10, 2006
978
979
980
981
if (isFlagSet(mode, RTLD_NOLOAD)) {
error("no existing handle and RTLD_NOLOAD specified");
goto dlopenerror;
}
Jul 10, 2006
Jul 10, 2006
983
984
985
986
987
988
if (isFlagSet(mode, RTLD_LAZY) && isFlagSet(mode, RTLD_NOW)) {
error("how can I load something both RTLD_LAZY and RTLD_NOW?");
goto dlopenerror;
}
dls = loadModule(fullPath, sbuf, mode);
989
dlopenok:
Jul 10, 2006
Jul 10, 2006
990
991
dounlock();
return (void *) dls;
992
dlopenerror:
Jul 10, 2006
Jul 10, 2006
993
994
dounlock();
return NULL;
995
996
997
}
#if !FINK_BUILD
Jul 10, 2006
Jul 10, 2006
998
999
static void *
SDL_OSX_dlsym(void *dl_restrict handle, const char *dl_restrict symbol)