4
4
// Author: Vadim Zeitlin
6
6
// Created: 2005-01-16 (extracted from common/dynlib.cpp)
7
// RCS-ID: $Id: dlunix.cpp 40455 2006-08-04 22:32:08Z VZ $
7
// RCS-ID: $Id: dlunix.cpp 51903 2008-02-19 01:13:48Z DE $
8
8
// Copyright: (c) 2000-2005 Vadim Zeitlin <vadim@wxwindows.org>
9
9
// Licence: wxWindows licence
10
10
/////////////////////////////////////////////////////////////////////////////
33
33
#include "wx/log.h"
36
// only Mac OS X 10.3+ has dlfcn.h, and it is simpler to always provide our own
37
// wrappers using the native functions instead of doing checks for OS version
41
#include <AvailabilityMacros.h>
42
44
// if some flags are not supported, just ignore them
44
46
#define RTLD_LAZY 0
79
81
// ----------------------------------------------------------------------------
80
82
// dlxxx() emulation for Darwin
83
// Only useful if the OS X version could be < 10.3 at runtime
81
84
// ----------------------------------------------------------------------------
83
#if defined(__DARWIN__)
86
#if defined(__DARWIN__) && (MAC_OS_X_VERSION_MIN_REQUIRED < MAC_OS_X_VERSION_10_3)
84
87
// ---------------------------------------------------------------------------
85
88
// For Darwin/Mac OS X
86
89
// supply the sun style dlopen functions in terms of Darwin NS*
99
102
static char dl_last_error[1024];
101
const char *dlerror()
104
static const char *wx_darwin_dlerror()
103
106
return dl_last_error;
106
void *dlopen(const char *path, int WXUNUSED(mode) /* mode is ignored */)
109
static void *wx_darwin_dlopen(const char *path, int WXUNUSED(mode) /* mode is ignored */)
108
111
NSObjectFileImage ofile;
109
112
NSModule handle = NULL;
162
int dlclose(void *handle)
165
static int wx_darwin_dlclose(void *handle)
164
167
NSUnLinkModule((NSModule)handle, NSUNLINKMODULE_OPTION_NONE);
168
void *dlsym(void *handle, const char *symbol)
171
static void *wx_darwin_dlsym(void *handle, const char *symbol)
170
173
// as on many other systems, C symbols have prepended underscores under
171
174
// Darwin but unlike the normal dlopen(), NSLookupSymbolInModule() is not
179
182
return nsSymbol ? NSAddressOfSymbol(nsSymbol) : NULL;
185
// Add the weak linking attribute to dlopen's declaration
186
extern void * dlopen(const char * __path, int __mode) AVAILABLE_MAC_OS_X_VERSION_10_3_AND_LATER;
188
// For all of these methods we test dlopen since all of the dl functions we use were added
189
// to OS X at the same time. This also ensures we don't dlopen with the real function then
190
// dlclose with the internal implementation.
192
static inline void *wx_dlopen(const char *__path, int __mode)
196
return dlopen(__path, __mode);
199
return wx_darwin_dlopen(__path, __mode);
202
static inline int wx_dlclose(void *__handle)
206
return dlclose(__handle);
209
return wx_darwin_dlclose(__handle);
212
static inline const char *wx_dlerror()
219
return wx_darwin_dlerror();
222
static inline void *wx_dlsym(void *__handle, const char *__symbol)
226
return dlsym(__handle, __symbol);
229
return wx_darwin_dlsym(__handle, __symbol);
232
#else // __DARWIN__/!__DARWIN__
234
// Use preprocessor definitions for non-Darwin or OS X >= 10.3
235
#define wx_dlopen(__path,__mode) dlopen(__path,__mode)
236
#define wx_dlclose(__handle) dlclose(__handle)
237
#define wx_dlerror() dlerror()
238
#define wx_dlsym(__handle,__symbol) dlsym(__handle,__symbol)
182
240
#endif // defined(__DARWIN__)
184
242
// ----------------------------------------------------------------------------
188
246
wxDllType wxDynamicLibrary::GetProgramHandle()
190
248
#ifdef USE_POSIX_DL_FUNCS
191
return dlopen(0, RTLD_LAZY);
249
return wx_dlopen(0, RTLD_LAZY);
193
251
return PROG_HANDLE;
209
267
if ( flags & wxDL_GLOBAL )
210
268
rtldFlags |= RTLD_GLOBAL;
212
return dlopen(libname.fn_str(), rtldFlags);
270
return wx_dlopen(libname.fn_str(), rtldFlags);
213
271
#else // !USE_POSIX_DL_FUNCS
214
272
int shlFlags = 0;
253
311
#ifdef USE_POSIX_DL_FUNCS
254
symbol = dlsym(handle, name.fn_str());
312
symbol = wx_dlsym(handle, name.fn_str());
255
313
#else // !USE_POSIX_DL_FUNCS
256
314
// note that shl_findsym modifies the handle argument to indicate where the
257
315
// symbol was found, but it's ok to modify the local handle copy here
272
330
void wxDynamicLibrary::Error()
274
332
#if wxUSE_UNICODE
275
wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
333
wxWCharBuffer buffer = wxConvLocal.cMB2WC( wx_dlerror() );
276
334
const wxChar *err = buffer;
278
const wxChar *err = dlerror();
336
const wxChar *err = wx_dlerror();
281
339
wxLogError(wxT("%s"), err ? err : _("Unknown dynamic library error"));