1 /////////////////////////////////////////////////////////////////////////////
3 // Purpose: Dynamic library management
4 // Author: Guilhem Lavaux
8 // Copyright: (c) Guilhem Lavaux
9 // Licence: wxWindows licence
10 /////////////////////////////////////////////////////////////////////////////
12 // ============================================================================
14 // ============================================================================
16 // ----------------------------------------------------------------------------
18 // ----------------------------------------------------------------------------
21 # pragma implementation "dynlib.h"
24 #include "wx/wxprec.h"
30 #if wxUSE_DYNLIB_CLASS
32 #if defined(__WINDOWS__)
33 #include "wx/msw/private.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
)
131 NSSymbol nsSymbol
= NSLookupSymbolInModule( handle
, symbol
) ;
135 addr
= NSAddressOfSymbol(nsSymbol
);
144 #endif // defined(__DARWIN__)
147 // ---------------------------------------------------------------------------
149 // ---------------------------------------------------------------------------
151 //FIXME: This class isn't really common at all, it should be moved into
152 // platform dependent files.
154 #if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
155 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".dll");
156 #elif defined(__UNIX__)
157 #if defined(__HPUX__)
158 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".sl");
160 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".so");
164 wxDllType
wxDynamicLibrary::GetProgramHandle()
166 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
167 return dlopen(0, RTLD_LAZY
);
168 #elif defined (HAVE_SHL_LOAD)
171 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
176 bool wxDynamicLibrary::Load(wxString libname
, int flags
)
178 wxASSERT_MSG(m_handle
== 0, _T("Library already loaded."));
180 // add the proper extension for the DLL ourselves unless told not to
181 if ( !(flags
& wxDL_VERBATIM
) )
183 // and also check that the libname doesn't already have it
185 wxFileName::SplitPath(libname
, NULL
, NULL
, &ext
);
188 libname
+= GetDllExt();
192 // different ways to load a shared library
194 // FIXME: should go to the platform-specific files!
195 #if defined(__WXMAC__) && !defined(__DARWIN__)
200 wxMacFilename2FSSpec( libname
, &myFSSpec
);
202 if( GetDiskFragment( &myFSSpec
,
209 myErrName
) != noErr
)
212 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
218 #elif defined(__WXPM__) || defined(__EMX__)
220 DosLoadModule(err
, sizeof(err
), libname
.c_str(), &m_handle
);
222 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
224 #if defined(__VMS) || defined(__DARWIN__)
225 m_handle
= dlopen(libname
.c_str(), 0); // The second parameter is ignored
226 #else // !__VMS && !__DARWIN__
229 if ( flags
& wxDL_LAZY
)
231 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
232 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
234 rtldFlags
|= RTLD_LAZY
;
236 wxLogDebug(_T("wxDL_LAZY is not supported on this platform"));
239 else if ( flags
& wxDL_NOW
)
242 rtldFlags
|= RTLD_NOW
;
244 wxLogDebug(_T("wxDL_NOW is not supported on this platform"));
248 if ( flags
& wxDL_GLOBAL
)
251 rtldFlags
|= RTLD_GLOBAL
;
253 wxLogDebug(_T("RTLD_GLOBAL is not supported on this platform."));
257 m_handle
= dlopen(libname
.fn_str(), rtldFlags
);
258 #endif // __VMS || __DARWIN__ ?
260 #elif defined(HAVE_SHL_LOAD)
263 if( flags
& wxDL_LAZY
)
265 wxASSERT_MSG( (flags
& wxDL_NOW
) == 0,
266 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
267 shlFlags
|= BIND_DEFERRED
;
269 else if( flags
& wxDL_NOW
)
271 shlFlags
|= BIND_IMMEDIATE
;
273 m_handle
= shl_load(libname
.fn_str(), BIND_DEFERRED
, 0);
275 #elif defined(__WINDOWS__)
276 m_handle
= ::LoadLibrary(libname
.c_str());
278 #error "runtime shared lib support not implemented on this platform"
283 wxString
msg(_("Failed to load shared library '%s'"));
284 #if defined(HAVE_DLERROR) && !defined(__EMX__)
287 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
288 const wxChar
*err
= buffer
;
290 const wxChar
*err
= dlerror();
294 wxLogError( msg
, err
);
296 wxLogSysError( msg
, libname
.c_str() );
303 void wxDynamicLibrary::Unload()
307 #if defined(__WXPM__) || defined(__EMX__)
308 DosFreeModule( m_handle
);
309 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
311 #elif defined(HAVE_SHL_LOAD)
312 shl_unload( m_handle
);
313 #elif defined(__WINDOWS__)
314 ::FreeLibrary( m_handle
);
315 #elif defined(__WXMAC__) && !defined(__DARWIN__)
316 CloseConnection( (CFragConnectionID
*) &m_handle
);
318 #error "runtime shared lib support not implemented"
324 void *wxDynamicLibrary::GetSymbol(const wxString
&name
, bool *success
) const
326 wxCHECK_MSG( IsLoaded(), NULL
,
327 _T("Can't load symbol from unloaded library") );
332 #if defined(__WXMAC__) && !defined(__DARWIN__)
334 CFragSymbolClass symClass
;
337 c2pstrcpy( (StringPtr
) symName
, name
);
339 strcpy( (char *)symName
, name
);
340 c2pstr( (char *)symName
);
342 if( FindSymbol( dllHandle
, symName
, &symAddress
, &symClass
) == noErr
)
343 symbol
= (void *)symAddress
;
345 #elif defined(__WXPM__) || defined(__EMX__)
346 DosQueryProcAddr( m_handle
, 1L, name
.c_str(), (PFN
*)symbol
);
348 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
349 symbol
= dlsym( m_handle
, name
.fn_str() );
351 #elif defined(HAVE_SHL_LOAD)
352 // use local variable since shl_findsym modifies the handle argument
353 // to indicate where the symbol was found (GD)
354 wxDllType the_handle
= m_handle
;
355 if( shl_findsym( &the_handle
, name
.fn_str(), TYPE_UNDEFINED
, &symbol
) != 0 )
358 #elif defined(__WINDOWS__)
359 symbol
= (void*) ::GetProcAddress( m_handle
, name
.mb_str() );
362 #error "runtime shared lib support not implemented"
367 #if defined(HAVE_DLERROR) && !defined(__EMX__)
370 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
371 const wxChar
*err
= buffer
;
373 const wxChar
*err
= dlerror();
378 wxLogError(wxT("%s"), err
);
382 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
395 wxDynamicLibrary::CanonicalizeName(const wxString
& name
,
396 wxDynamicLibraryCategory
401 #endif // __UNIX__/!__UNIX__
404 wxString nameCanonic
;
406 // under Unix the library names usualyl start with "lib" prefix, add it
411 wxFAIL_MSG( _T("unknown wxDynamicLibraryCategory value") );
415 // don't do anything for modules, their names are arbitrary
419 // library names should start with "lib" under Unix
420 nameCanonic
= _T("lib");
425 nameCanonic
<< name
<< GetDllExt();
430 wxString
wxDynamicLibrary::CanonicalizePluginName(const wxString
& name
,
431 wxPluginCategory cat
)
434 if ( cat
== wxDL_PLUGIN_GUI
)
436 wxAppTraits
*traits
= wxAppConsole::GetInstance() ?
437 wxAppConsole::GetInstance()->GetTraits() : NULL
;
438 wxASSERT_MSG( traits
,
439 _("can't query for GUI plugins name in console applications") );
440 suffix
= traits
->GetToolkitInfo().shortName
;
449 if ( !suffix
.empty() )
450 suffix
= wxString(_T("_")) + suffix
;
452 #define WXSTRINGIZE(x) #x
454 #if (wxMINOR_VERSION % 2) == 0
455 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
457 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
460 #if (wxMINOR_VERSION % 2) == 0
461 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y)
463 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z)
467 suffix
<< wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION
, wxMINOR_VERSION
,
472 return CanonicalizeName(name
+ suffix
, wxDL_MODULE
);
476 wxString
wxDynamicLibrary::GetPluginsDirectory()
479 wxString format
= wxGetInstallPrefix();
480 format
<< wxFILE_SEP_PATH
481 << wxT("lib") << wxFILE_SEP_PATH
482 << wxT("wx") << wxFILE_SEP_PATH
485 dir
.Printf(format
.c_str(), wxMAJOR_VERSION
, wxMINOR_VERSION
);
488 return wxEmptyString
;
493 #endif // wxUSE_DYNLIB_CLASS