1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Dynamic library management
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
20 #if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
21 # pragma implementation "dynlib.h"
24 #include "wx/wxprec.h"
30 #if wxUSE_DYNLIB_CLASS
32 #if defined(__WINDOWS__)
33 #include "wx/msw/wrapwin.h"
36 #include "wx/dynlib.h"
37 #include "wx/filefn.h"
41 #include "wx/filename.h" // for SplitPath()
43 #include "wx/apptrait.h"
45 #if defined(__WXMAC__)
46 #include "wx/mac/private.h"
50 // ============================================================================
52 // ============================================================================
54 #if defined(__DARWIN__)
55 // ---------------------------------------------------------------------------
56 // For Darwin/Mac OS X
57 // supply the sun style dlopen functions in terms of Darwin NS*
58 // ---------------------------------------------------------------------------
61 * The dlopen port is a port from dl_next.xs by Anno Siegel.
62 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
63 * The method used here is just to supply the sun style dlopen etc.
64 * functions in terms of Darwin NS*.
68 #include <mach-o/dyld.h>
70 static char dl_last_error
[1024];
73 void TranslateError(const char *path
, int number
)
76 static char *OFIErrorStrings
[] =
78 "%s(%d): Object Image Load Failure\n",
79 "%s(%d): Object Image Load Success\n",
80 "%s(%d): Not an recognisable object file\n",
81 "%s(%d): No valid architecture\n",
82 "%s(%d): Object image has an invalid format\n",
83 "%s(%d): Invalid access (permissions?)\n",
84 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
86 #define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
89 if (index
> NUM_OFI_ERRORS
- 1) {
90 index
= NUM_OFI_ERRORS
- 1;
92 sprintf(dl_last_error
, OFIErrorStrings
[index
], path
, number
);
100 void *dlopen(const char *path
, int WXUNUSED(mode
) /* mode is ignored */)
103 NSObjectFileImage ofile
;
104 NSModule handle
= NULL
;
106 dyld_result
= NSCreateObjectFileImageFromFile(path
, &ofile
);
107 if (dyld_result
!= NSObjectFileImageSuccess
)
109 TranslateError(path
, dyld_result
);
113 // NSLinkModule will cause the run to abort on any link error's
114 // not very friendly but the error recovery functionality is limited.
115 handle
= NSLinkModule(ofile
, path
, NSLINKMODULE_OPTION_BINDNOW
);
121 int dlclose(void *handle
)
123 NSUnLinkModule( handle
, NSUNLINKMODULE_OPTION_NONE
);
127 void *dlsym(void *handle
, const char *symbol
)
129 // as on many other systems, C symbols have prepended underscores under
130 // Darwin but unlike the normal dlopen(), NSLookupSymbolInModule() is not
132 NSSymbol nsSymbol
= NSLookupSymbolInModule( handle
,
133 wxString(_T('_')) + symbol
);
134 return nsSymbol
? NSAddressOfSymbol(nsSymbol
) : NULL
;
137 #endif // defined(__DARWIN__)
140 // ---------------------------------------------------------------------------
142 // ---------------------------------------------------------------------------
144 //FIXME: This class isn't really common at all, it should be moved into
145 // platform dependent files.
147 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
148 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".dll");
149 #elif defined(__WXMAC__) && !defined(__DARWIN__)
150 const wxChar
*wxDynamicLibrary::ms_dllext
= _T("");
151 #elif defined(__UNIX__)
152 #if defined(__HPUX__)
153 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".sl");
154 #elif defined(__DARWIN__)
155 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".bundle");
157 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".so");
161 wxDllType
wxDynamicLibrary::GetProgramHandle()
163 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
164 return dlopen(0, RTLD_LAZY
);
165 #elif defined (HAVE_SHL_LOAD)
168 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
173 bool wxDynamicLibrary::Load(wxString libname
, int flags
)
175 wxASSERT_MSG(m_handle
== 0, _T("Library already loaded."));
177 // add the proper extension for the DLL ourselves unless told not to
178 if ( !(flags
& wxDL_VERBATIM
) )
180 // and also check that the libname doesn't already have it
182 wxFileName::SplitPath(libname
, NULL
, NULL
, &ext
);
185 libname
+= GetDllExt();
189 // different ways to load a shared library
191 // FIXME: should go to the platform-specific files!
192 #if defined(__WXMAC__) && !defined(__DARWIN__)
197 wxMacFilename2FSSpec( libname
, &myFSSpec
);
199 if( GetDiskFragment( &myFSSpec
,
206 myErrName
) != noErr
)
208 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
210 wxMacMakeStringFromPascal( myErrName
).c_str() );
214 #elif defined(__WXPM__) || defined(__EMX__)
216 DosLoadModule(err
, sizeof(err
), libname
.c_str(), &m_handle
);
218 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
220 #if defined(__VMS) || defined(__DARWIN__)
221 m_handle
= dlopen(libname
.fn_str(), 0); // The second parameter is ignored
222 #else // !__VMS && !__DARWIN__
225 if ( flags
& wxDL_LAZY
)
227 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
228 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
230 rtldFlags
|= RTLD_LAZY
;
232 wxLogDebug(_T("wxDL_LAZY is not supported on this platform"));
235 else if ( flags
& wxDL_NOW
)
238 rtldFlags
|= RTLD_NOW
;
240 wxLogDebug(_T("wxDL_NOW is not supported on this platform"));
244 if ( flags
& wxDL_GLOBAL
)
247 rtldFlags
|= RTLD_GLOBAL
;
249 wxLogDebug(_T("RTLD_GLOBAL is not supported on this platform."));
253 m_handle
= dlopen(libname
.fn_str(), rtldFlags
);
254 #endif // __VMS || __DARWIN__ ?
256 #elif defined(HAVE_SHL_LOAD)
259 if( flags
& wxDL_LAZY
)
261 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
262 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
263 shlFlags
|= BIND_DEFERRED
;
265 else if( flags
& wxDL_NOW
)
267 shlFlags
|= BIND_IMMEDIATE
;
269 m_handle
= shl_load(libname
.fn_str(), BIND_DEFERRED
, 0);
271 #elif defined(__WINDOWS__)
272 m_handle
= ::LoadLibrary(libname
.c_str());
274 #error "runtime shared lib support not implemented on this platform"
279 wxString
msg(_("Failed to load shared library '%s'"));
280 #if defined(HAVE_DLERROR) && !defined(__EMX__)
283 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
284 const wxChar
*err
= buffer
;
286 const wxChar
*err
= dlerror();
290 wxLogError( msg
, err
);
292 wxLogSysError( msg
, libname
.c_str() );
300 void wxDynamicLibrary::Unload(wxDllType handle
)
302 #if defined(__WXPM__) || defined(__EMX__)
303 DosFreeModule( handle
);
304 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
306 #elif defined(HAVE_SHL_LOAD)
307 shl_unload( handle
);
308 #elif defined(__WINDOWS__)
309 ::FreeLibrary( handle
);
310 #elif defined(__WXMAC__) && !defined(__DARWIN__)
311 CloseConnection( (CFragConnectionID
*) &handle
);
313 #error "runtime shared lib support not implemented"
317 void *wxDynamicLibrary::DoGetSymbol(const wxString
&name
, bool *success
) const
319 wxCHECK_MSG( IsLoaded(), NULL
,
320 _T("Can't load symbol from unloaded library") );
325 #if defined(__WXMAC__) && !defined(__DARWIN__)
327 CFragSymbolClass symClass
;
330 c2pstrcpy( (StringPtr
) symName
, name
.fn_str() );
332 strcpy( (char *)symName
, name
.fn_str() );
333 c2pstr( (char *)symName
);
335 if( FindSymbol( m_handle
, symName
, &symAddress
, &symClass
) == noErr
)
336 symbol
= (void *)symAddress
;
338 #elif defined(__WXPM__) || defined(__EMX__)
339 DosQueryProcAddr( m_handle
, 1L, name
.c_str(), (PFN
*)symbol
);
341 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
342 symbol
= dlsym( m_handle
, name
.fn_str() );
344 #elif defined(HAVE_SHL_LOAD)
345 // use local variable since shl_findsym modifies the handle argument
346 // to indicate where the symbol was found (GD)
347 wxDllType the_handle
= m_handle
;
348 if( shl_findsym( &the_handle
, name
.fn_str(), TYPE_UNDEFINED
, &symbol
) != 0 )
351 #elif defined(__WINDOWS__)
353 symbol
= (void*) ::GetProcAddress( m_handle
, name
);
355 symbol
= (void*) ::GetProcAddress( m_handle
, name
.mb_str() );
359 #error "runtime shared lib support not implemented"
363 *success
= symbol
!= NULL
;
368 void *wxDynamicLibrary::GetSymbol(const wxString
& name
, bool *success
) const
370 void *symbol
= DoGetSymbol(name
, success
);
373 #if defined(HAVE_DLERROR) && !defined(__EMX__)
376 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
377 const wxChar
*err
= buffer
;
379 const wxChar
*err
= dlerror();
384 wxLogError(wxT("%s"), err
);
387 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
397 wxDynamicLibrary::CanonicalizeName(const wxString
& name
,
398 wxDynamicLibraryCategory
403 #endif // __UNIX__/!__UNIX__
406 wxString nameCanonic
;
408 // under Unix the library names usually start with "lib" prefix, add it
413 wxFAIL_MSG( _T("unknown wxDynamicLibraryCategory value") );
417 // don't do anything for modules, their names are arbitrary
421 // library names should start with "lib" under Unix
422 nameCanonic
= _T("lib");
427 nameCanonic
<< name
<< GetDllExt();
432 wxString
wxDynamicLibrary::CanonicalizePluginName(const wxString
& name
,
433 wxPluginCategory cat
)
436 if ( cat
== wxDL_PLUGIN_GUI
)
438 wxAppTraits
*traits
= wxAppConsole::GetInstance() ?
439 wxAppConsole::GetInstance()->GetTraits() : NULL
;
440 wxASSERT_MSG( traits
,
441 _("can't query for GUI plugins name in console applications") );
442 suffix
= traits
->GetToolkitInfo().shortName
;
451 if ( !suffix
.empty() )
452 suffix
= wxString(_T("_")) + suffix
;
454 #define WXSTRINGIZE(x) #x
456 #if (wxMINOR_VERSION % 2) == 0
457 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
459 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
462 #if (wxMINOR_VERSION % 2) == 0
463 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y)
465 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z)
469 suffix
<< wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION
, wxMINOR_VERSION
,
475 // Add compiler identification:
476 #if defined(__GNUG__)
477 suffix
<< _T("_gcc");
478 #elif defined(__VISUALC__)
480 #elif defined(__WATCOMC__)
481 suffix
<< _T("_wat");
482 #elif defined(__BORLANDC__)
483 suffix
<< _T("_bcc");
487 return CanonicalizeName(name
+ suffix
, wxDL_MODULE
);
491 wxString
wxDynamicLibrary::GetPluginsDirectory()
494 wxString format
= wxGetInstallPrefix();
496 format
<< wxFILE_SEP_PATH
497 << wxT("lib") << wxFILE_SEP_PATH
498 << wxT("wx") << wxFILE_SEP_PATH
499 #if (wxMINOR_VERSION % 2) == 0
501 dir
.Printf(format
.c_str(), wxMAJOR_VERSION
, wxMINOR_VERSION
);
504 dir
.Printf(format
.c_str(),
505 wxMAJOR_VERSION
, wxMINOR_VERSION
, wxRELEASE_NUMBER
);
510 return wxEmptyString
;
515 #endif // wxUSE_DYNLIB_CLASS