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 */)
102 NSObjectFileImage ofile
;
103 NSModule handle
= NULL
;
105 int dyld_result
= NSCreateObjectFileImageFromFile(path
, &ofile
);
106 if ( dyld_result
!= NSObjectFileImageSuccess
)
112 handle
= NSLinkModule
116 NSLINKMODULE_OPTION_BINDNOW
|
117 NSLINKMODULE_OPTION_RETURN_ON_ERROR
122 TranslateError(path
, dyld_result
);
127 int dlclose(void *handle
)
129 NSUnLinkModule( handle
, NSUNLINKMODULE_OPTION_NONE
);
133 void *dlsym(void *handle
, const char *symbol
)
135 // as on many other systems, C symbols have prepended underscores under
136 // Darwin but unlike the normal dlopen(), NSLookupSymbolInModule() is not
138 wxCharBuffer
buf(strlen(symbol
) + 1);
139 char *p
= buf
.data();
141 strcpy(p
+ 1, symbol
);
143 NSSymbol nsSymbol
= NSLookupSymbolInModule( handle
, p
);
144 return nsSymbol
? NSAddressOfSymbol(nsSymbol
) : NULL
;
147 #endif // defined(__DARWIN__)
150 // ---------------------------------------------------------------------------
152 // ---------------------------------------------------------------------------
154 //FIXME: This class isn't really common at all, it should be moved into
155 // platform dependent files.
157 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
158 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".dll");
159 #elif defined(__WXMAC__) && !defined(__DARWIN__)
160 const wxChar
*wxDynamicLibrary::ms_dllext
= _T("");
161 #elif defined(__UNIX__)
162 #if defined(__HPUX__)
163 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".sl");
164 #elif defined(__DARWIN__)
165 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".bundle");
167 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".so");
171 wxDllType
wxDynamicLibrary::GetProgramHandle()
173 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
174 return dlopen(0, RTLD_LAZY
);
175 #elif defined (HAVE_SHL_LOAD)
178 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
183 bool wxDynamicLibrary::Load(wxString libname
, int flags
)
185 wxASSERT_MSG(m_handle
== 0, _T("Library already loaded."));
187 // add the proper extension for the DLL ourselves unless told not to
188 if ( !(flags
& wxDL_VERBATIM
) )
190 // and also check that the libname doesn't already have it
192 wxFileName::SplitPath(libname
, NULL
, NULL
, &ext
);
195 libname
+= GetDllExt();
199 // different ways to load a shared library
201 // FIXME: should go to the platform-specific files!
202 #if defined(__WXMAC__) && !defined(__DARWIN__)
207 wxMacFilename2FSSpec( libname
, &myFSSpec
);
209 if( GetDiskFragment( &myFSSpec
,
216 myErrName
) != noErr
)
218 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
220 wxMacMakeStringFromPascal( myErrName
).c_str() );
224 #elif defined(__WXPM__) || defined(__EMX__)
226 DosLoadModule(err
, sizeof(err
), libname
.c_str(), &m_handle
);
228 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
230 #if defined(__VMS) || defined(__DARWIN__)
231 m_handle
= dlopen(libname
.fn_str(), 0); // The second parameter is ignored
232 #else // !__VMS && !__DARWIN__
235 if ( flags
& wxDL_LAZY
)
237 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
238 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
240 rtldFlags
|= RTLD_LAZY
;
242 wxLogDebug(_T("wxDL_LAZY is not supported on this platform"));
245 else if ( flags
& wxDL_NOW
)
248 rtldFlags
|= RTLD_NOW
;
250 wxLogDebug(_T("wxDL_NOW is not supported on this platform"));
254 if ( flags
& wxDL_GLOBAL
)
257 rtldFlags
|= RTLD_GLOBAL
;
259 wxLogDebug(_T("RTLD_GLOBAL is not supported on this platform."));
263 m_handle
= dlopen(libname
.fn_str(), rtldFlags
);
264 #endif // __VMS || __DARWIN__ ?
266 #elif defined(HAVE_SHL_LOAD)
269 if( flags
& wxDL_LAZY
)
271 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
272 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
273 shlFlags
|= BIND_DEFERRED
;
275 else if( flags
& wxDL_NOW
)
277 shlFlags
|= BIND_IMMEDIATE
;
279 m_handle
= shl_load(libname
.fn_str(), BIND_DEFERRED
, 0);
281 #elif defined(__WINDOWS__)
282 m_handle
= ::LoadLibrary(libname
.c_str());
284 #error "runtime shared lib support not implemented on this platform"
289 wxString
msg(_("Failed to load shared library '%s'"));
290 #if defined(HAVE_DLERROR) && !defined(__EMX__)
293 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
294 const wxChar
*err
= buffer
;
296 const wxChar
*err
= dlerror();
300 wxLogError( msg
, err
);
302 wxLogSysError( msg
, libname
.c_str() );
310 void wxDynamicLibrary::Unload(wxDllType handle
)
312 #if defined(__WXPM__) || defined(__EMX__)
313 DosFreeModule( handle
);
314 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
316 #elif defined(HAVE_SHL_LOAD)
317 shl_unload( handle
);
318 #elif defined(__WINDOWS__)
319 ::FreeLibrary( handle
);
320 #elif defined(__WXMAC__) && !defined(__DARWIN__)
321 CloseConnection( (CFragConnectionID
*) &handle
);
323 #error "runtime shared lib support not implemented"
327 void *wxDynamicLibrary::DoGetSymbol(const wxString
&name
, bool *success
) const
329 wxCHECK_MSG( IsLoaded(), NULL
,
330 _T("Can't load symbol from unloaded library") );
335 #if defined(__WXMAC__) && !defined(__DARWIN__)
337 CFragSymbolClass symClass
;
340 c2pstrcpy( (StringPtr
) symName
, name
.fn_str() );
342 strcpy( (char *)symName
, name
.fn_str() );
343 c2pstr( (char *)symName
);
345 if( FindSymbol( m_handle
, symName
, &symAddress
, &symClass
) == noErr
)
346 symbol
= (void *)symAddress
;
348 #elif defined(__WXPM__) || defined(__EMX__)
349 DosQueryProcAddr( m_handle
, 1L, name
.c_str(), (PFN
*)symbol
);
351 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
352 symbol
= dlsym( m_handle
, name
.fn_str() );
354 #elif defined(HAVE_SHL_LOAD)
355 // use local variable since shl_findsym modifies the handle argument
356 // to indicate where the symbol was found (GD)
357 wxDllType the_handle
= m_handle
;
358 if( shl_findsym( &the_handle
, name
.fn_str(), TYPE_UNDEFINED
, &symbol
) != 0 )
361 #elif defined(__WINDOWS__)
363 symbol
= (void*) ::GetProcAddress( m_handle
, name
);
365 symbol
= (void*) ::GetProcAddress( m_handle
, name
.mb_str() );
369 #error "runtime shared lib support not implemented"
373 *success
= symbol
!= NULL
;
378 void *wxDynamicLibrary::GetSymbol(const wxString
& name
, bool *success
) const
380 void *symbol
= DoGetSymbol(name
, success
);
383 #if defined(HAVE_DLERROR) && !defined(__EMX__)
386 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
387 const wxChar
*err
= buffer
;
389 const wxChar
*err
= dlerror();
394 wxLogError(wxT("%s"), err
);
397 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
407 wxDynamicLibrary::CanonicalizeName(const wxString
& name
,
408 wxDynamicLibraryCategory
413 #endif // __UNIX__/!__UNIX__
416 wxString nameCanonic
;
418 // under Unix the library names usually start with "lib" prefix, add it
423 wxFAIL_MSG( _T("unknown wxDynamicLibraryCategory value") );
427 // don't do anything for modules, their names are arbitrary
431 // library names should start with "lib" under Unix
432 nameCanonic
= _T("lib");
437 nameCanonic
<< name
<< GetDllExt();
442 wxString
wxDynamicLibrary::CanonicalizePluginName(const wxString
& name
,
443 wxPluginCategory cat
)
446 if ( cat
== wxDL_PLUGIN_GUI
)
448 wxAppTraits
*traits
= wxAppConsole::GetInstance() ?
449 wxAppConsole::GetInstance()->GetTraits() : NULL
;
450 wxASSERT_MSG( traits
,
451 _("can't query for GUI plugins name in console applications") );
452 suffix
= traits
->GetToolkitInfo().shortName
;
461 if ( !suffix
.empty() )
462 suffix
= wxString(_T("_")) + suffix
;
464 #define WXSTRINGIZE(x) #x
466 #if (wxMINOR_VERSION % 2) == 0
467 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
469 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
472 #if (wxMINOR_VERSION % 2) == 0
473 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y)
475 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z)
479 suffix
<< wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION
, wxMINOR_VERSION
,
485 // Add compiler identification:
486 #if defined(__GNUG__)
487 suffix
<< _T("_gcc");
488 #elif defined(__VISUALC__)
490 #elif defined(__WATCOMC__)
491 suffix
<< _T("_wat");
492 #elif defined(__BORLANDC__)
493 suffix
<< _T("_bcc");
497 return CanonicalizeName(name
+ suffix
, wxDL_MODULE
);
501 wxString
wxDynamicLibrary::GetPluginsDirectory()
504 wxString format
= wxGetInstallPrefix();
506 format
<< wxFILE_SEP_PATH
507 << wxT("lib") << wxFILE_SEP_PATH
508 << wxT("wx") << wxFILE_SEP_PATH
509 #if (wxMINOR_VERSION % 2) == 0
511 dir
.Printf(format
.c_str(), wxMAJOR_VERSION
, wxMINOR_VERSION
);
514 dir
.Printf(format
.c_str(),
515 wxMAJOR_VERSION
, wxMINOR_VERSION
, wxRELEASE_NUMBER
);
520 return wxEmptyString
;
525 #endif // wxUSE_DYNLIB_CLASS