]> git.saurik.com Git - wxWidgets.git/blame - src/common/dynlib.cpp
Rebaked (build/bakefiles/common.bkl 1.95->1.96 change)
[wxWidgets.git] / src / common / dynlib.cpp
CommitLineData
7a4b9130
GL
1/////////////////////////////////////////////////////////////////////////////
2// Name: dynlib.cpp
3// Purpose: Dynamic library management
4// Author: Guilhem Lavaux
5// Modified by:
6// Created: 20/07/98
7// RCS-ID: $Id$
8// Copyright: (c) Guilhem Lavaux
65571936 9// Licence: wxWindows licence
7a4b9130
GL
10/////////////////////////////////////////////////////////////////////////////
11
7b0bfbb2
VZ
12// ============================================================================
13// declarations
14// ============================================================================
15
16// ----------------------------------------------------------------------------
17// headers
18// ----------------------------------------------------------------------------
19
14f355c2 20#if defined(__GNUG__) && !defined(NO_GCC_PRAGMA)
a585ca5c 21# pragma implementation "dynlib.h"
7a4b9130
GL
22#endif
23
0c32066b
JS
24#include "wx/wxprec.h"
25
2df7be7f
RR
26#ifdef __BORLANDC__
27 #pragma hdrstop
28#endif
f6bcfd97 29
1948bb32 30#if wxUSE_DYNLIB_CLASS
8a0d4cf6 31
f6bcfd97 32#if defined(__WINDOWS__)
5fccb5b4 33 #include "wx/msw/wrapwin.h"
f6bcfd97
BP
34#endif
35
7b0bfbb2
VZ
36#include "wx/dynlib.h"
37#include "wx/filefn.h"
38#include "wx/intl.h"
39#include "wx/log.h"
1948bb32
VS
40#include "wx/utils.h"
41#include "wx/filename.h" // for SplitPath()
a8eaaeb2
VS
42#include "wx/app.h"
43#include "wx/apptrait.h"
7b0bfbb2 44
76a5e5d2
SC
45#if defined(__WXMAC__)
46 #include "wx/mac/private.h"
47#endif
48
7b0bfbb2 49
1948bb32
VS
50// ============================================================================
51// implementation
52// ============================================================================
846e1424 53
1948bb32
VS
54#if defined(__DARWIN__)
55// ---------------------------------------------------------------------------
56// For Darwin/Mac OS X
57// supply the sun style dlopen functions in terms of Darwin NS*
58// ---------------------------------------------------------------------------
0b9ab0bd 59
f11bdd03
GD
60/* Porting notes:
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*.
65 */
7a4b9130 66
1948bb32
VS
67#include <stdio.h>
68#include <mach-o/dyld.h>
7a4b9130 69
1948bb32 70static char dl_last_error[1024];
123a7fdd 71
1948bb32
VS
72static
73void TranslateError(const char *path, int number)
7b0bfbb2 74{
1948bb32
VS
75 unsigned int index;
76 static char *OFIErrorStrings[] =
77 {
5fccb5b4
VZ
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",
1948bb32
VS
85 };
86#define NUM_OFI_ERRORS (sizeof(OFIErrorStrings) / sizeof(OFIErrorStrings[0]))
27529614 87
1948bb32
VS
88 index = number;
89 if (index > NUM_OFI_ERRORS - 1) {
90 index = NUM_OFI_ERRORS - 1;
91 }
92 sprintf(dl_last_error, OFIErrorStrings[index], path, number);
7b0bfbb2 93}
7a4b9130 94
1948bb32 95const char *dlerror()
7a4b9130 96{
1948bb32 97 return dl_last_error;
7a4b9130
GL
98}
99
1948bb32 100void *dlopen(const char *path, int WXUNUSED(mode) /* mode is ignored */)
7a4b9130 101{
1948bb32
VS
102 int dyld_result;
103 NSObjectFileImage ofile;
104 NSModule handle = NULL;
105
106 dyld_result = NSCreateObjectFileImageFromFile(path, &ofile);
107 if (dyld_result != NSObjectFileImageSuccess)
7b0bfbb2 108 {
5fccb5b4 109 TranslateError(path, dyld_result);
7b0bfbb2 110 }
1948bb32
VS
111 else
112 {
5fccb5b4
VZ
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);
1948bb32
VS
116 }
117
118 return handle;
7a4b9130
GL
119}
120
1948bb32 121int dlclose(void *handle)
7a4b9130 122{
1948bb32
VS
123 NSUnLinkModule( handle, NSUNLINKMODULE_OPTION_NONE);
124 return 0;
f4a8c29f
GL
125}
126
1948bb32 127void *dlsym(void *handle, const char *symbol)
f4a8c29f 128{
42a24523
VZ
129 // as on many other systems, C symbols have prepended underscores under
130 // Darwin but unlike the normal dlopen(), NSLookupSymbolInModule() is not
131 // aware of this
132 NSSymbol nsSymbol = NSLookupSymbolInModule( handle,
133 wxString(_T('_')) + symbol );
134 return nsSymbol ? NSAddressOfSymbol(nsSymbol) : NULL;
7a4b9130
GL
135}
136
1948bb32
VS
137#endif // defined(__DARWIN__)
138
7b0bfbb2 139
a585ca5c 140// ---------------------------------------------------------------------------
1948bb32 141// wxDynamicLibrary
a585ca5c 142// ---------------------------------------------------------------------------
7b0bfbb2 143
1948bb32
VS
144//FIXME: This class isn't really common at all, it should be moved into
145// platform dependent files.
f6bcfd97
BP
146
147#if defined(__WINDOWS__) || defined(__WXPM__) || defined(__EMX__)
1948bb32 148 const wxChar *wxDynamicLibrary::ms_dllext = _T(".dll");
87ec9b8f
GD
149#elif defined(__WXMAC__) && !defined(__DARWIN__)
150 const wxChar *wxDynamicLibrary::ms_dllext = _T("");
f6bcfd97 151#elif defined(__UNIX__)
1948bb32
VS
152 #if defined(__HPUX__)
153 const wxChar *wxDynamicLibrary::ms_dllext = _T(".sl");
87ec9b8f 154 #elif defined(__DARWIN__)
42a24523 155 const wxChar *wxDynamicLibrary::ms_dllext = _T(".bundle");
1948bb32
VS
156 #else
157 const wxChar *wxDynamicLibrary::ms_dllext = _T(".so");
158 #endif
f6bcfd97 159#endif
f6bcfd97 160
1948bb32 161wxDllType wxDynamicLibrary::GetProgramHandle()
0868079c 162{
1a787c5d 163#if defined( HAVE_DLOPEN ) && !defined(__EMX__)
1948bb32 164 return dlopen(0, RTLD_LAZY);
7742efff 165#elif defined (HAVE_SHL_LOAD)
1948bb32 166 return PROG_HANDLE;
0868079c 167#else
58c837a4 168 wxFAIL_MSG( wxT("This method is not implemented under Windows or OS/2"));
7742efff 169 return 0;
7cc98b3e 170#endif
0868079c
KB
171}
172
1948bb32 173bool wxDynamicLibrary::Load(wxString libname, int flags)
a585ca5c 174{
1948bb32
VS
175 wxASSERT_MSG(m_handle == 0, _T("Library already loaded."));
176
177 // add the proper extension for the DLL ourselves unless told not to
178 if ( !(flags & wxDL_VERBATIM) )
179 {
180 // and also check that the libname doesn't already have it
181 wxString ext;
182 wxFileName::SplitPath(libname, NULL, NULL, &ext);
183 if ( ext.empty() )
184 {
185 libname += GetDllExt();
186 }
187 }
7b0bfbb2 188
1948bb32
VS
189 // different ways to load a shared library
190 //
191 // FIXME: should go to the platform-specific files!
192#if defined(__WXMAC__) && !defined(__DARWIN__)
0b9ab0bd
RL
193 FSSpec myFSSpec;
194 Ptr myMainAddr;
195 Str255 myErrName;
196
197 wxMacFilename2FSSpec( libname , &myFSSpec );
198
199 if( GetDiskFragment( &myFSSpec,
200 0,
201 kCFragGoesToEOF,
202 "\p",
203 kPrivateCFragCopy,
1948bb32 204 &m_handle,
0b9ab0bd
RL
205 &myMainAddr,
206 myErrName ) != noErr )
7cc98b3e 207 {
0b9ab0bd
RL
208 wxLogSysError( _("Failed to load shared library '%s' Error '%s'"),
209 libname.c_str(),
fb44fc34 210 wxMacMakeStringFromPascal( myErrName ).c_str() );
1948bb32 211 m_handle = 0;
7cc98b3e 212 }
0b9ab0bd 213
1a787c5d 214#elif defined(__WXPM__) || defined(__EMX__)
1948bb32
VS
215 char err[256] = "";
216 DosLoadModule(err, sizeof(err), libname.c_str(), &m_handle);
217
218#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
0b9ab0bd 219
1948bb32 220#if defined(__VMS) || defined(__DARWIN__)
5f3f0f17 221 m_handle = dlopen(libname.fn_str(), 0); // The second parameter is ignored
1948bb32
VS
222#else // !__VMS && !__DARWIN__
223 int rtldFlags = 0;
224
225 if ( flags & wxDL_LAZY )
226 {
227 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
228 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
229#ifdef RTLD_LAZY
230 rtldFlags |= RTLD_LAZY;
231#else
232 wxLogDebug(_T("wxDL_LAZY is not supported on this platform"));
233#endif
234 }
235 else if ( flags & wxDL_NOW )
236 {
237#ifdef RTLD_NOW
238 rtldFlags |= RTLD_NOW;
0b9ab0bd 239#else
1948bb32
VS
240 wxLogDebug(_T("wxDL_NOW is not supported on this platform"));
241#endif
242 }
0b9ab0bd 243
1948bb32
VS
244 if ( flags & wxDL_GLOBAL )
245 {
246#ifdef RTLD_GLOBAL
247 rtldFlags |= RTLD_GLOBAL;
248#else
249 wxLogDebug(_T("RTLD_GLOBAL is not supported on this platform."));
0b9ab0bd 250#endif
1948bb32
VS
251 }
252
253 m_handle = dlopen(libname.fn_str(), rtldFlags);
254#endif // __VMS || __DARWIN__ ?
255
256#elif defined(HAVE_SHL_LOAD)
257 int shlFlags = 0;
a585ca5c 258
1948bb32
VS
259 if( flags & wxDL_LAZY )
260 {
261 wxASSERT_MSG( (flags & wxDL_NOW) == 0,
262 _T("wxDL_LAZY and wxDL_NOW are mutually exclusive.") );
263 shlFlags |= BIND_DEFERRED;
264 }
265 else if( flags & wxDL_NOW )
266 {
267 shlFlags |= BIND_IMMEDIATE;
268 }
269 m_handle = shl_load(libname.fn_str(), BIND_DEFERRED, 0);
270
271#elif defined(__WINDOWS__)
272 m_handle = ::LoadLibrary(libname.c_str());
273#else
274 #error "runtime shared lib support not implemented on this platform"
275#endif
276
277 if ( m_handle == 0 )
7cc98b3e 278 {
f6bcfd97 279 wxString msg(_("Failed to load shared library '%s'"));
1948bb32 280#if defined(HAVE_DLERROR) && !defined(__EMX__)
f6bcfd97 281
1948bb32
VS
282#if wxUSE_UNICODE
283 wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
284 const wxChar *err = buffer;
285#else
d0166913 286 const wxChar *err = dlerror();
1948bb32
VS
287#endif
288
0b9ab0bd 289 if( err )
0b9ab0bd 290 wxLogError( msg, err );
0b9ab0bd 291#else
0b9ab0bd
RL
292 wxLogSysError( msg, libname.c_str() );
293#endif
7cc98b3e
VZ
294 }
295
1948bb32 296 return IsLoaded();
a585ca5c
KB
297}
298
9d033af9
VZ
299/* static */
300void wxDynamicLibrary::Unload(wxDllType handle)
752c7d6b 301{
1948bb32 302#if defined(__WXPM__) || defined(__EMX__)
ff793cab 303 DosFreeModule( handle );
1948bb32 304#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
ff793cab 305 dlclose( handle );
1948bb32 306#elif defined(HAVE_SHL_LOAD)
ff793cab 307 shl_unload( handle );
1948bb32 308#elif defined(__WINDOWS__)
ff793cab 309 ::FreeLibrary( handle );
1948bb32 310#elif defined(__WXMAC__) && !defined(__DARWIN__)
ff793cab 311 CloseConnection( (CFragConnectionID*) &handle );
1948bb32 312#else
9d033af9 313 #error "runtime shared lib support not implemented"
1948bb32 314#endif
752c7d6b
KB
315}
316
a018a119 317void *wxDynamicLibrary::DoGetSymbol(const wxString &name, bool *success) const
a585ca5c 318{
1948bb32
VS
319 wxCHECK_MSG( IsLoaded(), NULL,
320 _T("Can't load symbol from unloaded library") );
321
0b9ab0bd 322 void *symbol = 0;
a585ca5c 323
999836aa 324 wxUnusedVar(symbol);
1948bb32 325#if defined(__WXMAC__) && !defined(__DARWIN__)
0b9ab0bd
RL
326 Ptr symAddress;
327 CFragSymbolClass symClass;
328 Str255 symName;
1948bb32 329#if TARGET_CARBON
f73bc315 330 c2pstrcpy( (StringPtr) symName, name.fn_str() );
1948bb32 331#else
f73bc315 332 strcpy( (char *)symName, name.fn_str() );
1948bb32
VS
333 c2pstr( (char *)symName );
334#endif
609533d5 335 if( FindSymbol( m_handle, symName, &symAddress, &symClass ) == noErr )
0b9ab0bd 336 symbol = (void *)symAddress;
7cc98b3e 337
0b9ab0bd 338#elif defined(__WXPM__) || defined(__EMX__)
1948bb32 339 DosQueryProcAddr( m_handle, 1L, name.c_str(), (PFN*)symbol );
0b9ab0bd 340
1948bb32
VS
341#elif defined(HAVE_DLOPEN) || defined(__DARWIN__)
342 symbol = dlsym( m_handle, name.fn_str() );
343
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 )
349 symbol = 0;
350
351#elif defined(__WINDOWS__)
ffcb4ee4
JS
352#ifdef __WXWINCE__
353 symbol = (void*) ::GetProcAddress( m_handle, name );
354#else
1948bb32 355 symbol = (void*) ::GetProcAddress( m_handle, name.mb_str() );
ffcb4ee4 356#endif
b7b35e50 357
1c193821 358#else
1948bb32 359#error "runtime shared lib support not implemented"
1c193821 360#endif
0b9ab0bd 361
a018a119
VZ
362 if ( success )
363 *success = symbol != NULL;
364
365 return symbol;
366}
367
368void *wxDynamicLibrary::GetSymbol(const wxString& name, bool *success) const
369{
370 void *symbol = DoGetSymbol(name, success);
7b0bfbb2
VZ
371 if ( !symbol )
372 {
1948bb32
VS
373#if defined(HAVE_DLERROR) && !defined(__EMX__)
374
375#if wxUSE_UNICODE
376 wxWCharBuffer buffer = wxConvLocal.cMB2WC( dlerror() );
377 const wxChar *err = buffer;
378#else
d0166913 379 const wxChar *err = dlerror();
1948bb32
VS
380#endif
381
0b9ab0bd
RL
382 if( err )
383 {
ee682a94 384 wxLogError(wxT("%s"), err);
0b9ab0bd
RL
385 }
386#else
7cc98b3e
VZ
387 wxLogSysError(_("Couldn't find symbol '%s' in a dynamic library"),
388 name.c_str());
0b9ab0bd 389#endif
7b0bfbb2 390 }
0b9ab0bd 391
7b0bfbb2 392 return symbol;
7a4b9130 393}
5fccb5b4 394
1948bb32 395/*static*/
350fffae
VZ
396wxString
397wxDynamicLibrary::CanonicalizeName(const wxString& name,
398 wxDynamicLibraryCategory
399#ifdef __UNIX__
400 cat
401#else // !__UNIX__
402 WXUNUSED(cat)
403#endif // __UNIX__/!__UNIX__
404 )
7a4b9130 405{
350fffae
VZ
406 wxString nameCanonic;
407
87ec9b8f 408 // under Unix the library names usually start with "lib" prefix, add it
1948bb32 409#ifdef __UNIX__
350fffae
VZ
410 switch ( cat )
411 {
412 default:
413 wxFAIL_MSG( _T("unknown wxDynamicLibraryCategory value") );
414 // fall through
415
416 case wxDL_MODULE:
417 // don't do anything for modules, their names are arbitrary
418 break;
419
420 case wxDL_LIBRARY:
421 // library names should start with "lib" under Unix
422 nameCanonic = _T("lib");
423 break;
424 }
425#endif // __UNIX__
426
427 nameCanonic << name << GetDllExt();
428 return nameCanonic;
7a4b9130 429}
8a0d4cf6 430
1948bb32
VS
431/*static*/
432wxString wxDynamicLibrary::CanonicalizePluginName(const wxString& name,
433 wxPluginCategory cat)
f11bdd03 434{
1948bb32
VS
435 wxString suffix;
436 if ( cat == wxDL_PLUGIN_GUI )
f11bdd03 437 {
a8eaaeb2
VS
438 wxAppTraits *traits = wxAppConsole::GetInstance() ?
439 wxAppConsole::GetInstance()->GetTraits() : NULL;
5fccb5b4 440 wxASSERT_MSG( traits,
a8eaaeb2 441 _("can't query for GUI plugins name in console applications") );
324899f6 442 suffix = traits->GetToolkitInfo().shortName;
f11bdd03 443 }
1948bb32
VS
444#if wxUSE_UNICODE
445 suffix << _T('u');
446#endif
447#ifdef __WXDEBUG__
448 suffix << _T('d');
449#endif
f11bdd03 450
1948bb32
VS
451 if ( !suffix.empty() )
452 suffix = wxString(_T("_")) + suffix;
f11bdd03 453
3546ffae 454#define WXSTRINGIZE(x) #x
1948bb32
VS
455#ifdef __UNIX__
456 #if (wxMINOR_VERSION % 2) == 0
3546ffae 457 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y)
1948bb32 458 #else
3546ffae 459 #define wxDLLVER(x,y,z) "-" WXSTRINGIZE(x) "." WXSTRINGIZE(y) "." WXSTRINGIZE(z)
1948bb32
VS
460 #endif
461#else
462 #if (wxMINOR_VERSION % 2) == 0
3546ffae 463 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y)
1948bb32 464 #else
3546ffae 465 #define wxDLLVER(x,y,z) WXSTRINGIZE(x) WXSTRINGIZE(y) WXSTRINGIZE(z)
1948bb32
VS
466 #endif
467#endif
350fffae 468
1948bb32
VS
469 suffix << wxString::FromAscii(wxDLLVER(wxMAJOR_VERSION, wxMINOR_VERSION,
470 wxRELEASE_NUMBER));
471#undef wxDLLVER
3546ffae 472#undef WXSTRINGIZE
f11bdd03 473
2c02ec05
VS
474#ifdef __WINDOWS__
475 // Add compiler identification:
476 #if defined(__GNUG__)
477 suffix << _T("_gcc");
478 #elif defined(__VISUALC__)
479 suffix << _T("_vc");
480 #elif defined(__WATCOMC__)
481 suffix << _T("_wat");
482 #elif defined(__BORLANDC__)
483 suffix << _T("_bcc");
484 #endif
485#endif
486
1948bb32 487 return CanonicalizeName(name + suffix, wxDL_MODULE);
f11bdd03 488}
5fccb5b4 489
1948bb32
VS
490/*static*/
491wxString wxDynamicLibrary::GetPluginsDirectory()
f11bdd03 492{
1948bb32
VS
493#ifdef __UNIX__
494 wxString format = wxGetInstallPrefix();
cb979fac 495 wxString dir;
1948bb32
VS
496 format << wxFILE_SEP_PATH
497 << wxT("lib") << wxFILE_SEP_PATH
498 << wxT("wx") << wxFILE_SEP_PATH
cb979fac 499#if (wxMINOR_VERSION % 2) == 0
1948bb32 500 << wxT("%i.%i");
1948bb32 501 dir.Printf(format.c_str(), wxMAJOR_VERSION, wxMINOR_VERSION);
1948bb32 502#else
cb979fac
VS
503 << wxT("%i.%i.%i");
504 dir.Printf(format.c_str(),
505 wxMAJOR_VERSION, wxMINOR_VERSION, wxRELEASE_NUMBER);
506#endif
507 return dir;
508
509#else // ! __UNIX__
1948bb32
VS
510 return wxEmptyString;
511#endif
f11bdd03
GD
512}
513
f11bdd03 514
1948bb32 515#endif // wxUSE_DYNLIB_CLASS