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 #include "wx/dynlib.h"
33 #include "wx/filefn.h"
37 #include "wx/filename.h" // for SplitPath()
39 #include "wx/apptrait.h"
41 #include "wx/arrimpl.cpp"
43 #if defined(__WXMAC__)
44 #include "wx/mac/private.h"
47 WX_DEFINE_USER_EXPORTED_OBJARRAY(wxDynamicLibraryDetailsArray
);
49 // ============================================================================
51 // ============================================================================
53 #if defined(__DARWIN__)
54 // ---------------------------------------------------------------------------
55 // For Darwin/Mac OS X
56 // supply the sun style dlopen functions in terms of Darwin NS*
57 // ---------------------------------------------------------------------------
60 * The dlopen port is a port from dl_next.xs by Anno Siegel.
61 * dl_next.xs is itself a port from dl_dlopen.xs by Paul Marquess.
62 * The method used here is just to supply the sun style dlopen etc.
63 * functions in terms of Darwin NS*.
67 #include <mach-o/dyld.h>
69 static char dl_last_error
[1024];
72 void TranslateError(const char *path
, int number
)
75 static char *OFIErrorStrings
[] =
77 "%s(%d): Object Image Load Failure\n",
78 "%s(%d): Object Image Load Success\n",
79 "%s(%d): Not an recognisable object file\n",
80 "%s(%d): No valid architecture\n",
81 "%s(%d): Object image has an invalid format\n",
82 "%s(%d): Invalid access (permissions?)\n",
83 "%s(%d): Unknown error code from NSCreateObjectFileImageFromFile\n",
85 #define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
88 if (index
> NUM_OFI_ERRORS
- 1) {
89 index
= NUM_OFI_ERRORS
- 1;
91 sprintf(dl_last_error
, OFIErrorStrings
[index
], path
, number
);
99 void *dlopen(const char *path
, int WXUNUSED(mode
) /* mode is ignored */)
101 NSObjectFileImage ofile
;
102 NSModule handle
= NULL
;
104 int dyld_result
= NSCreateObjectFileImageFromFile(path
, &ofile
);
105 if ( dyld_result
!= NSObjectFileImageSuccess
)
111 handle
= NSLinkModule
115 NSLINKMODULE_OPTION_BINDNOW
|
116 NSLINKMODULE_OPTION_RETURN_ON_ERROR
121 TranslateError(path
, dyld_result
);
126 int dlclose(void *handle
)
128 NSUnLinkModule( handle
, NSUNLINKMODULE_OPTION_NONE
);
132 void *dlsym(void *handle
, const char *symbol
)
134 // as on many other systems, C symbols have prepended underscores under
135 // Darwin but unlike the normal dlopen(), NSLookupSymbolInModule() is not
137 wxCharBuffer
buf(strlen(symbol
) + 1);
138 char *p
= buf
.data();
140 strcpy(p
+ 1, symbol
);
142 NSSymbol nsSymbol
= NSLookupSymbolInModule( handle
, p
);
143 return nsSymbol
? NSAddressOfSymbol(nsSymbol
) : NULL
;
146 #endif // defined(__DARWIN__)
149 // ---------------------------------------------------------------------------
151 // ---------------------------------------------------------------------------
153 //FIXME: This class isn't really common at all, it should be moved into
154 // platform dependent files (already done for Windows)
156 #if defined(__WXPM__) || defined(__EMX__)
157 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".dll");
158 #elif defined(__WXMAC__) && !defined(__DARWIN__)
159 const wxChar
*wxDynamicLibrary::ms_dllext
= _T("");
160 #elif defined(__UNIX__)
161 #if defined(__HPUX__)
162 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".sl");
163 #elif defined(__DARWIN__)
164 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".bundle");
166 const wxChar
*wxDynamicLibrary::ms_dllext
= _T(".so");
170 wxDllType
wxDynamicLibrary::GetProgramHandle()
172 #if defined( HAVE_DLOPEN ) && !defined(__EMX__)
173 return dlopen(0, RTLD_LAZY
);
174 #elif defined (HAVE_SHL_LOAD)
177 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
182 bool wxDynamicLibrary::Load(const wxString
& libnameOrig
, int flags
)
184 wxASSERT_MSG(m_handle
== 0, _T("Library already loaded."));
186 // add the proper extension for the DLL ourselves unless told not to
187 wxString libname
= libnameOrig
;
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
= RawLoad(libname
);
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() );
312 void wxDynamicLibrary::Unload(wxDllType handle
)
314 #if defined(__WXPM__) || defined(__EMX__)
315 DosFreeModule( handle
);
316 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
318 #elif defined(HAVE_SHL_LOAD)
319 shl_unload( handle
);
320 #elif defined(__WXMAC__) && !defined(__DARWIN__)
321 CloseConnection( (CFragConnectionID
*) &handle
);
323 #error "runtime shared lib support not implemented"
329 void *wxDynamicLibrary::DoGetSymbol(const wxString
&name
, bool *success
) const
331 wxCHECK_MSG( IsLoaded(), NULL
,
332 _T("Can't load symbol from unloaded library") );
337 #if defined(__WXMAC__) && !defined(__DARWIN__)
339 CFragSymbolClass symClass
;
342 c2pstrcpy( (StringPtr
) symName
, name
.fn_str() );
344 strcpy( (char *)symName
, name
.fn_str() );
345 c2pstr( (char *)symName
);
347 if( FindSymbol( m_handle
, symName
, &symAddress
, &symClass
) == noErr
)
348 symbol
= (void *)symAddress
;
350 #elif defined(__WXPM__) || defined(__EMX__)
351 DosQueryProcAddr( m_handle
, 1L, name
.c_str(), (PFN
*)symbol
);
353 #elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
354 symbol
= dlsym( m_handle
, name
.fn_str() );
356 #elif defined(HAVE_SHL_LOAD)
357 // use local variable since shl_findsym modifies the handle argument
358 // to indicate where the symbol was found (GD)
359 wxDllType the_handle
= m_handle
;
360 if( shl_findsym( &the_handle
, name
.fn_str(), TYPE_UNDEFINED
, &symbol
) != 0 )
363 #elif defined(__WINDOWS__)
364 symbol
= RawGetSymbol(m_handle
, name
);
366 #error "runtime shared lib support not implemented"
370 *success
= symbol
!= NULL
;
375 void *wxDynamicLibrary::GetSymbol(const wxString
& name
, bool *success
) const
377 void *symbol
= DoGetSymbol(name
, success
);
380 #if defined(HAVE_DLERROR) && !defined(__EMX__)
383 wxWCharBuffer buffer
= wxConvLocal
.cMB2WC( dlerror() );
384 const wxChar
*err
= buffer
;
386 const wxChar
*err
= dlerror();
391 wxLogError(wxT("%s"), err
);
394 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
402 // ----------------------------------------------------------------------------
403 // informational methods
404 // ----------------------------------------------------------------------------
408 wxDynamicLibrary::CanonicalizeName(const wxString
& name
,
409 wxDynamicLibraryCategory
414 #endif // __UNIX__/!__UNIX__
417 wxString nameCanonic
;
419 // under Unix the library names usually start with "lib" prefix, add it
424 wxFAIL_MSG( _T("unknown wxDynamicLibraryCategory value") );
428 // don't do anything for modules, their names are arbitrary
432 // library names should start with "lib" under Unix
433 nameCanonic
= _T("lib");
438 nameCanonic
<< name
<< GetDllExt();
443 wxString
wxDynamicLibrary::CanonicalizePluginName(const wxString
& name
,
444 wxPluginCategory cat
)
447 if ( cat
== wxDL_PLUGIN_GUI
)
449 wxAppTraits
*traits
= wxAppConsole::GetInstance() ?
450 wxAppConsole::GetInstance()->GetTraits() : NULL
;
451 wxASSERT_MSG( traits
,
452 _("can't query for GUI plugins name in console applications") );
453 suffix
= traits
->GetToolkitInfo().shortName
;
462 if ( !suffix
.empty() )
463 suffix
= wxString(_T("_")) + suffix
;
465 #define WXSTRINGIZE(x) #x
467 #if (wxMINOR_VERSION % 2) == 0
468 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
470 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
473 #if (wxMINOR_VERSION % 2) == 0
474 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y)
476 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z)
480 suffix
<< wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION
, wxMINOR_VERSION
,
486 // Add compiler identification:
487 #if defined(__GNUG__)
488 suffix
<< _T("_gcc");
489 #elif defined(__VISUALC__)
491 #elif defined(__WATCOMC__)
492 suffix
<< _T("_wat");
493 #elif defined(__BORLANDC__)
494 suffix
<< _T("_bcc");
498 return CanonicalizeName(name
+ suffix
, wxDL_MODULE
);
502 wxString
wxDynamicLibrary::GetPluginsDirectory()
505 wxString format
= wxGetInstallPrefix();
507 format
<< wxFILE_SEP_PATH
508 << wxT("lib") << wxFILE_SEP_PATH
509 << wxT("wx") << wxFILE_SEP_PATH
510 #if (wxMINOR_VERSION % 2) == 0
512 dir
.Printf(format
.c_str(), wxMAJOR_VERSION
, wxMINOR_VERSION
);
515 dir
.Printf(format
.c_str(),
516 wxMAJOR_VERSION
, wxMINOR_VERSION
, wxRELEASE_NUMBER
);
521 return wxEmptyString
;
526 #endif // wxUSE_DYNLIB_CLASS